Skip to content

Commit 1aba9e7

Browse files
bors[bot]astafan8
authored andcommitted
Merge #3644
3644: Relax time stamp type assertions to allow ints in the database r=jenshnielsen a=astafan8 - [x] capture this with a test Co-authored-by: Mikhail Astafev <astafan8@gmail.com>
1 parent 9d7441e commit 1aba9e7

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Relax type assertions for DataSet timestamps so that if in the database they
2+
are written as integers, as opposed to floats, a DataSet can still be loaded

qcodes/dataset/sqlite/queries.py

+6
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,9 @@ def get_completed_timestamp_from_run_id(
866866
timestamp in seconds since the Epoch, or None
867867
"""
868868
ts = select_one_where(conn, "runs", "completed_timestamp", "run_id", run_id)
869+
# sometimes it happens that the timestamp is written to DB as an int
870+
if isinstance(ts, int):
871+
ts = float(ts)
869872
assert isinstance(ts, (float, type(None)))
870873
return ts
871874

@@ -1888,6 +1891,9 @@ def get_sample_name_from_experiment_id(conn: ConnectionPlus, exp_id: int) -> str
18881891
def get_run_timestamp_from_run_id(conn: ConnectionPlus,
18891892
run_id: int) -> Optional[float]:
18901893
time_stamp = select_one_where(conn, "runs", "run_timestamp", "run_id", run_id)
1894+
# sometimes it happens that the timestamp is saved as an integer in the database
1895+
if isinstance(time_stamp, int):
1896+
time_stamp = float(time_stamp)
18911897
assert isinstance(time_stamp, (float, type(None)))
18921898
return time_stamp
18931899

qcodes/tests/dataset/test_dataset_basic.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
from qcodes.dataset.descriptions.param_spec import ParamSpecBase
2222
from qcodes.dataset.descriptions.rundescriber import RunDescriber
2323
from qcodes.dataset.guids import parse_guid
24-
from qcodes.dataset.sqlite.connection import path_to_dbfile
24+
from qcodes.dataset.sqlite.connection import path_to_dbfile, atomic
2525
from qcodes.dataset.sqlite.database import get_DB_location
26-
from qcodes.dataset.sqlite.queries import _unicode_categories
26+
from qcodes.dataset.sqlite.queries import _unicode_categories, _rewrite_timestamps
2727
from qcodes.tests.common import error_caused_by
2828
from qcodes.tests.dataset.helper_functions import verify_data_dict
2929
from qcodes.tests.dataset.test_links import generate_some_links
@@ -198,6 +198,22 @@ def test_timestamps_are_none():
198198
assert isinstance(ds.run_timestamp(), str)
199199

200200

201+
@pytest.mark.usefixtures('experiment')
202+
def test_integer_timestamps_in_database_are_supported():
203+
ds = DataSet()
204+
205+
ds.mark_started()
206+
ds.mark_completed()
207+
208+
with atomic(ds.conn) as conn:
209+
_rewrite_timestamps(conn, ds.run_id, 42, 69)
210+
211+
assert isinstance(ds.run_timestamp_raw, float)
212+
assert isinstance(ds.completed_timestamp_raw, float)
213+
assert isinstance(ds.run_timestamp(), str)
214+
assert isinstance(ds.completed_timestamp(), str)
215+
216+
201217
def test_dataset_read_only_properties(dataset):
202218
read_only_props = ['run_id', 'path_to_db', 'name', 'table_name', 'guid',
203219
'number_of_results', 'counter', 'parameters',

0 commit comments

Comments
 (0)