ensure db get before query

This commit is contained in:
Justus Kuhlmann 2025-11-21 09:42:40 +01:00
commit 2940ee9055
Signed by: jkuhl
GPG key ID: 00ED992DD79B85A6
4 changed files with 18 additions and 10 deletions

View file

@ -143,7 +143,6 @@ def find_record(path, ensemble, correlator_name, code, project=None, parameters=
db = path + '/backlogger.db' db = path + '/backlogger.db'
if code not in codes: if code not in codes:
raise ValueError("Code " + code + "unknown, take one of the following:" + ", ".join(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) 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": if code == "sfcf":
@ -152,12 +151,14 @@ def find_record(path, ensemble, correlator_name, code, project=None, parameters=
return results.reset_index() 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) return _project_lookup_by_alias(db, name)
def list_projects(path): def list_projects(path):
db = path + '/backlogger.db' db = path + '/backlogger.db'
dl.get(db, dataset=path)
conn = sqlite3.connect(db) conn = sqlite3.connect(db)
c = conn.cursor() c = conn.cursor()
c.execute("SELECT id,aliases FROM projects") c.execute("SELECT id,aliases FROM projects")

View file

@ -24,13 +24,15 @@ def create_project(path: str, uuid: str, owner: Union[str, None]=None, tags: Uni
code: str (optional) code: str (optional)
The code that was used to create the measurements. 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() c = conn.cursor()
known_projects = c.execute("SELECT * FROM projects WHERE id=?", (uuid,)) known_projects = c.execute("SELECT * FROM projects WHERE id=?", (uuid,))
if known_projects.fetchone(): if known_projects.fetchone():
raise ValueError("Project already imported, use update_project() instead.") raise ValueError("Project already imported, use update_project() instead.")
dl.unlock(path + "/backlogger.db", dataset=path) dl.unlock(db, dataset=path)
alias_str = None alias_str = None
if aliases is not None: if aliases is not None:
alias_str = list2str(aliases) 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)) 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.commit()
conn.close() 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) conn = sqlite3.connect(db)
c = conn.cursor() c = conn.cursor()
c.execute(f"UPDATE projects SET '{prop}' = '{value}' WHERE id == '{uuid}'") 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]): def update_aliases(path: str, uuid: str, aliases: list[str]):
db = os.path.join(path, "backlogger.db") db = os.path.join(path, "backlogger.db")
dl.get(db, dataset=path)
known_data = _project_lookup_by_id(db, uuid)[0] known_data = _project_lookup_by_id(db, uuid)[0]
known_aliases = known_data[1] 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: if not uuid:
raise ValueError("The dataset does not have a uuid!") raise ValueError("The dataset does not have a uuid!")
if not os.path.exists(path + "/projects/" + 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) create_project(path, uuid, owner, tags, aliases, code)
move_submodule(path, 'projects/tmp', 'projects/' + uuid) move_submodule(path, 'projects/tmp', 'projects/' + uuid)
os.mkdir(path + '/import_scripts/' + 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: else:
dl.drop(tmp_path, reckless='kill') dl.drop(tmp_path, reckless='kill')
shutil.rmtree(tmp_path) shutil.rmtree(tmp_path)

View file

@ -28,6 +28,7 @@ def write_measurement(path, ensemble, measurement, uuid, code, parameter_file=No
The uuid of the project. The uuid of the project.
""" """
db = os.path.join(path, 'backlogger.db') db = os.path.join(path, 'backlogger.db')
dl.get(db, ds=path)
dl.unlock(db, dataset=path) dl.unlock(db, dataset=path)
conn = sqlite3.connect(db) conn = sqlite3.connect(db)
c = conn.cursor() c = conn.cursor()
@ -176,6 +177,7 @@ def drop_record(path: str, meas_path: str):
file_in_archive = meas_path.split("::")[0] file_in_archive = meas_path.split("::")[0]
file = os.path.join(path, file_in_archive) file = os.path.join(path, file_in_archive)
db = os.path.join(path, 'backlogger.db') db = os.path.join(path, 'backlogger.db')
dl.get(db, ds=path)
sub_key = meas_path.split("::")[1] sub_key = meas_path.split("::")[1]
dl.unlock(db, dataset=path) dl.unlock(db, dataset=path)
conn = sqlite3.connect(db) conn = sqlite3.connect(db)

View file

@ -1,6 +1,5 @@
def str2list(string): def str2list(string):
return string.split(",") return string.split(",")