From 2940ee9055236a05d26d369b806a67bedfcd9aac Mon Sep 17 00:00:00 2001 From: Justus Kuhlmann Date: Fri, 21 Nov 2025 09:42:40 +0100 Subject: [PATCH] ensure db get before query --- corrlib/find.py | 7 ++++--- corrlib/main.py | 18 ++++++++++++------ corrlib/meas_io.py | 2 ++ corrlib/tools.py | 1 - 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/corrlib/find.py b/corrlib/find.py index 2b9973f..e0772a1 100644 --- a/corrlib/find.py +++ b/corrlib/find.py @@ -143,8 +143,7 @@ def find_record(path, ensemble, correlator_name, code, project=None, parameters= db = path + '/backlogger.db' if code not in codes: raise ValueError("Code " + code + "unknown, take one of the following:" + ", ".join(codes)) - if os.path.exists(db): - dl.get(db, dataset=path) + dl.get(db, dataset=path) results = _db_lookup(db, ensemble, correlator_name,code, project, parameters=parameters, created_before=created_before, created_after=created_after, updated_before=updated_before, updated_after=updated_after, revision=revision) if code == "sfcf": results = sfcf_filter(results, **kwargs) @@ -152,12 +151,14 @@ def find_record(path, ensemble, correlator_name, code, project=None, parameters= return results.reset_index() -def find_project(db, name): +def find_project(path, db, name): + dl.get(db, dataset=path) return _project_lookup_by_alias(db, name) def list_projects(path): db = path + '/backlogger.db' + dl.get(db, dataset=path) conn = sqlite3.connect(db) c = conn.cursor() c.execute("SELECT id,aliases FROM projects") diff --git a/corrlib/main.py b/corrlib/main.py index 460ed8b..ebc923e 100644 --- a/corrlib/main.py +++ b/corrlib/main.py @@ -24,13 +24,15 @@ def create_project(path: str, uuid: str, owner: Union[str, None]=None, tags: Uni code: str (optional) The code that was used to create the measurements. """ - conn = sqlite3.connect(path + "/backlogger.db") + db = path + "/backlogger.db" + dl.get(db, dataset=path) + conn = sqlite3.connect(db) c = conn.cursor() known_projects = c.execute("SELECT * FROM projects WHERE id=?", (uuid,)) if known_projects.fetchone(): raise ValueError("Project already imported, use update_project() instead.") - dl.unlock(path + "/backlogger.db", dataset=path) + dl.unlock(db, dataset=path) alias_str = None if aliases is not None: alias_str = list2str(aliases) @@ -40,10 +42,11 @@ def create_project(path: str, uuid: str, owner: Union[str, None]=None, tags: Uni c.execute("INSERT INTO projects (id, aliases, customTags, owner, code, created_at, updated_at) VALUES (?, ?, ?, ?, ?, datetime('now'), datetime('now'))", (uuid, alias_str, tag_str, owner, code)) conn.commit() conn.close() - dl.save(path + "/backlogger.db", message="Added entry for project " + uuid + " to database", dataset=path) + dl.save(db, message="Added entry for project " + uuid + " to database", dataset=path) -def update_project_data(db, uuid, prop, value = None): +def update_project_data(path, db, uuid, prop, value = None): + dl.get(db, dataset=path) conn = sqlite3.connect(db) c = conn.cursor() c.execute(f"UPDATE projects SET '{prop}' = '{value}' WHERE id == '{uuid}'") @@ -54,6 +57,7 @@ def update_project_data(db, uuid, prop, value = None): def update_aliases(path: str, uuid: str, aliases: list[str]): db = os.path.join(path, "backlogger.db") + dl.get(db, dataset=path) known_data = _project_lookup_by_id(db, uuid)[0] known_aliases = known_data[1] @@ -117,11 +121,13 @@ def import_project(path: str, url: str, owner: Union[str, None]=None, tags: Unio if not uuid: raise ValueError("The dataset does not have a uuid!") if not os.path.exists(path + "/projects/" + uuid): - dl.unlock(path + "/backlogger.db", dataset=path) + db = path + "/backlogger.db" + dl.get(db, ds=path) + dl.unlock(db, dataset=path) create_project(path, uuid, owner, tags, aliases, code) move_submodule(path, 'projects/tmp', 'projects/' + uuid) os.mkdir(path + '/import_scripts/' + uuid) - dl.save([path + "/backlogger.db", path + '/projects/' + uuid], message="Import project from " + url, dataset=path) + dl.save([db, path + '/projects/' + uuid], message="Import project from " + url, dataset=path) else: dl.drop(tmp_path, reckless='kill') shutil.rmtree(tmp_path) diff --git a/corrlib/meas_io.py b/corrlib/meas_io.py index 1a06dac..b98eb6e 100644 --- a/corrlib/meas_io.py +++ b/corrlib/meas_io.py @@ -28,6 +28,7 @@ def write_measurement(path, ensemble, measurement, uuid, code, parameter_file=No The uuid of the project. """ db = os.path.join(path, 'backlogger.db') + dl.get(db, ds=path) dl.unlock(db, dataset=path) conn = sqlite3.connect(db) c = conn.cursor() @@ -176,6 +177,7 @@ def drop_record(path: str, meas_path: str): file_in_archive = meas_path.split("::")[0] file = os.path.join(path, file_in_archive) db = os.path.join(path, 'backlogger.db') + dl.get(db, ds=path) sub_key = meas_path.split("::")[1] dl.unlock(db, dataset=path) conn = sqlite3.connect(db) diff --git a/corrlib/tools.py b/corrlib/tools.py index 3ac8bfe..da5acdd 100644 --- a/corrlib/tools.py +++ b/corrlib/tools.py @@ -1,6 +1,5 @@ - def str2list(string): return string.split(",")