From 81467ca503b419caaa6b1282cf9dfc6cc3d85946 Mon Sep 17 00:00:00 2001 From: Jochen Klar Date: Tue, 1 Oct 2024 16:50:00 +0200 Subject: [PATCH] Add check_file_id to insert_datasets --- isimip_publisher/commands.py | 4 ++++ isimip_publisher/utils/database.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/isimip_publisher/commands.py b/isimip_publisher/commands.py index e87e377..f5bc89d 100644 --- a/isimip_publisher/commands.py +++ b/isimip_publisher/commands.py @@ -213,6 +213,10 @@ def insert_datasets(): session = database.init_database_session(settings.DATABASE) + for dataset in store.datasets: + for file in dataset.files: + database.check_file_id(session, file.path, file.uuid) + for dataset in tqdm(store.datasets, desc='insert_datasets'.ljust(18)): database.insert_dataset(session, settings.VERSION, settings.RIGHTS, settings.RESTRICTED, dataset.name, dataset.path, dataset.size, dataset.specifiers) diff --git a/isimip_publisher/utils/database.py b/isimip_publisher/utils/database.py index 6e4e31b..a74966d 100644 --- a/isimip_publisher/utils/database.py +++ b/isimip_publisher/utils/database.py @@ -389,6 +389,12 @@ def retrieve_datasets(session, path, public=None, follow=False, like=True): return datasets +def check_file_id(session, path, uuid): + file = session.query(File).filter(File.id == uuid).one_or_none() + if file: + raise RuntimeError(f'File {path} has an id which already exists in the database ({uuid})') + + def insert_file(session, version, dataset_path, uuid, name, path, size, checksum, checksum_type, netcdf_header, specifiers): # get the dataset from the database