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.
This commit is contained in:
D. Berge
2020-08-25 17:52:17 +02:00
parent c3b0212798
commit 0c7b3146ae
2 changed files with 25 additions and 0 deletions

View File

@@ -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

View File

@@ -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")