From 0c7b3146ae18e127ab4e922bde30b982faabbea7 Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Tue, 25 Aug 2020 17:52:17 +0200 Subject: [PATCH] Update label definitions when updating survey config. It will not delete any labels that have been removed from the configuration, as those may be used, but it will add new labels and modify existing ones if they changed. --- bin/datastore.py | 23 +++++++++++++++++++++++ bin/import_survey_config.py | 2 ++ 2 files changed, 25 insertions(+) diff --git a/bin/datastore.py b/bin/datastore.py index 7afc26a..a680ae8 100644 --- a/bin/datastore.py +++ b/bin/datastore.py @@ -450,3 +450,26 @@ class Datastore: self.maybe_commit() # We do not commit if we've been passed a cursor, instead # we assume that we are in the middle of a transaction + + + def apply_survey_configuration(self, cursor = None): + if cursor is None: + cur = self.conn.cursor() + else: + cur = cursor + + qry = """ + INSERT INTO labels (name, data) + SELECT l.key, l.value + FROM file_data fd, + json_each(fd.data->'labels') l + WHERE fd.data::jsonb ? 'labels' + ON CONFLICT (name) DO UPDATE SET data = excluded.data; + """ + + cur.execute(qry) + + if cursor is None: + self.maybe_commit() + # We do not commit if we've been passed a cursor, instead + # we assume that we are in the middle of a transaction diff --git a/bin/import_survey_config.py b/bin/import_survey_config.py index 0ae0028..b0452f7 100755 --- a/bin/import_survey_config.py +++ b/bin/import_survey_config.py @@ -31,6 +31,8 @@ if __name__ == '__main__': if not db.file_in_db(filepath): print("Saving to DB") db.save_file_data(filepath, json.dumps(survey)) + print("Applying survey configuration") + db.apply_survey_configuration() else: print("Already in DB")