Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changes/newsfragments/5329.improved
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
In the QCoDeS DataSetProtocol newly created dataset now always have a captured_run_id / captured_counter
that matches the run_id / result_counter that they are assigned on creation. Previously these could be
out of sync if datasets measured elsewhere had been inserted into the database.
48 changes: 34 additions & 14 deletions qcodes/dataset/sqlite/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,12 +1338,22 @@ def _insert_run(
where_value=exp_id)
assert isinstance(run_counter, int)
assert isinstance(format_string, str)
newly_created_run = False

if (captured_run_id is not None and captured_counter is None) or (
captured_counter is not None and captured_run_id is None
):
raise ValueError(
"Either both of captured_counter and captured_run_id should be set or none of them."
)

run_counter += 1
if captured_counter is None:
newly_created_run = True
with atomic(conn) as conn:
query = """
SELECT
max(captured_counter)
max(result_counter)
FROM
runs
WHERE
Expand All @@ -1354,24 +1364,12 @@ def _insert_run(
captured_counter = existing_captured_counter + 1
else:
captured_counter = run_counter
formatted_name = format_table_name(format_string, "results", exp_id, run_counter)
table = "runs"

if description is None:
description = RunDescriber(InterDependencies_())
desc_str = serial.to_json_for_storage(description)
legacy_param_specs = new_to_old(description.interdeps).paramspecs
# we need to use legacy_param_specs here not because we need the features
# of legacy param specs but because they should be inserted into the layouts
# table and parameter column in the same order as data_set.add_parameter does
# specifically dependencies should come before the dependent parameters for links
# in the layout table to work correctly

if captured_run_id is None:
with atomic(conn) as conn:
query = """
SELECT
max(captured_run_id)
max(run_id)
FROM
runs"""
curr = transaction(conn, query)
Expand All @@ -1381,6 +1379,19 @@ def _insert_run(
else:
captured_run_id = 1

formatted_name = format_table_name(format_string, "results", exp_id, run_counter)
table = "runs"

if description is None:
description = RunDescriber(InterDependencies_())
desc_str = serial.to_json_for_storage(description)
legacy_param_specs = new_to_old(description.interdeps).paramspecs
# we need to use legacy_param_specs here not because we need the features
# of legacy param specs but because they should be inserted into the layouts
# table and parameter column in the same order as data_set.add_parameter does
# specifically dependencies should come before the dependent parameters for links
# in the layout table to work correctly

with atomic(conn) as conn:

if legacy_param_specs:
Expand Down Expand Up @@ -1456,6 +1467,15 @@ def _insert_run(
run_id = curr.lastrowid
if run_id is None:
raise RuntimeError(f"Creation of run with guid: {guid} failed")
if newly_created_run:
if captured_run_id != run_id:
warnings.warn(
"captured_run_id was not equal to run_id, this is unexpected and will be an error in the future."
)
if captured_counter != run_counter:
warnings.warn(
"captured_counter was not equal to result_counter, this is unexpected and will be an error in the future."
)
return run_counter, formatted_name, run_id


Expand Down
4 changes: 2 additions & 2 deletions qcodes/tests/dataset/test_database_extract_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,9 @@ def test_copy_datasets_and_add_new(
ds.mark_completed()

expected_run_ids = [4, 5, 6]
expected_captured_run_ids = [11, 12, 13]
expected_captured_run_ids = expected_run_ids
expected_counter = [4, 5, 6]
expected_captured_counter = [6, 7, 8]
expected_captured_counter = expected_counter

for ds, eri, ecri, ec, ecc in zip(new_datasets,
expected_run_ids,
Expand Down