mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 12:47:08 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user