diff --git a/api/archive_db.py b/api/archive_db.py index 5c7e2e9a..7a6a15ae 100644 --- a/api/archive_db.py +++ b/api/archive_db.py @@ -92,7 +92,6 @@ def fetch_triggers(breeder_name=None): @staticmethod def create_procedure(procedure_name=None, probability=1.0, source_table_name=None, target_table_name=None): query = f""" - CREATE OR REPLACE FUNCTION {procedure_name}() RETURNS TRIGGER AS $$ DECLARE random_value real; @@ -102,17 +101,16 @@ def create_procedure(procedure_name=None, probability=1.0, source_table_name=Non IF random_value < {probability} THEN INSERT INTO {target_table_name} AS target_table (setting_id, setting_full, setting_result) - SELECT setting_id, setting_full, setting_result FROM {source_table_name} AS source_table - ON CONFLICT - DO UPDATE SET target_table.setting_result = source_table.setting_result WHERE (source_table.setting_result->>0 > target_table.setting_result->>0 AND - source_table.setting_result->>1 < target_table.setting_result->>1); + SELECT setting_id, setting_full, setting_result FROM {source_table_name} + ON CONFLICT (setting_id) + DO UPDATE SET setting_result = excluded.setting_result WHERE (excluded.setting_result->>0 < target_table.setting_result->>0 AND + excluded.setting_result->>1 > target_table.setting_result->>1); END IF; RETURN NULL; END; $$ LANGUAGE plpgsql; - """ return query