From c608dd8b01ecd529378784beaf79232b00335337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Fri, 20 Oct 2023 11:49:35 +0100 Subject: [PATCH] fix: SQL 'DO UPDATE' syntax for data sync script (#2333) --- scripts/seed-database/write/flows.sql | 12 ++++++++++-- scripts/seed-database/write/published_flows.sql | 9 ++++++++- scripts/seed-database/write/team_members.sql | 9 +++++++-- scripts/seed-database/write/teams.sql | 11 +++++++++-- scripts/seed-database/write/users.sql | 9 +++++++-- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/scripts/seed-database/write/flows.sql b/scripts/seed-database/write/flows.sql index e6f431f5ca..948bb2238a 100644 --- a/scripts/seed-database/write/flows.sql +++ b/scripts/seed-database/write/flows.sql @@ -1,4 +1,4 @@ --- insert flows skipping conflicts +-- insert flows overwriting conflicts CREATE TEMPORARY TABLE sync_flows ( id uuid, team_id int, @@ -34,7 +34,15 @@ SELECT settings, copied_from FROM sync_flows -ON CONFLICT (id) DO UPDATE; +ON CONFLICT (id) DO UPDATE +SET + team_id = EXCLUDED.team_id, + slug = EXCLUDED.slug, + creator_id = EXCLUDED.creator_id, + data = EXCLUDED.data, + version = EXCLUDED.version, + settings = EXCLUDED.settings, + copied_from = EXCLUDED.copied_from; -- ensure that original flows.version is overwritten to match new operation inserted below, else sharedb will fail UPDATE flows SET version = 1; diff --git a/scripts/seed-database/write/published_flows.sql b/scripts/seed-database/write/published_flows.sql index 2cf9d0a48c..5e2d7c5972 100644 --- a/scripts/seed-database/write/published_flows.sql +++ b/scripts/seed-database/write/published_flows.sql @@ -1,3 +1,4 @@ +-- insert published_flows overwriting conflicts CREATE TEMPORARY TABLE sync_published_flows ( id int, data jsonb, @@ -25,4 +26,10 @@ SELECT publisher_id, created_at FROM sync_published_flows -ON CONFLICT (id) DO UPDATE; +ON CONFLICT (id) DO UPDATE +SET + data = EXCLUDED.data, + flow_id = EXCLUDED.flow_id, + summary = EXCLUDED.summary, + publisher_id = EXCLUDED.publisher_id, + created_at = EXCLUDED.created_at; diff --git a/scripts/seed-database/write/team_members.sql b/scripts/seed-database/write/team_members.sql index 6f1a2dfeb1..a74226ed8b 100644 --- a/scripts/seed-database/write/team_members.sql +++ b/scripts/seed-database/write/team_members.sql @@ -1,3 +1,4 @@ +-- insert team_members overwriting conflicts CREATE TEMPORARY TABLE sync_team_members ( id uuid, user_id integer, @@ -5,11 +6,15 @@ CREATE TEMPORARY TABLE sync_team_members ( role text ); -\copy team_members FROM '/tmp/team_members.csv' WITH (FORMAT csv, DELIMITER ';'); +\copy sync_team_members FROM '/tmp/team_members.csv' WITH (FORMAT csv, DELIMITER ';'); INSERT INTO team_members (id, user_id, team_id, role) SELECT id, user_id, team_id, role FROM - sync_team_members ON CONFLICT (id) DO UPDATE; \ No newline at end of file + sync_team_members ON CONFLICT (id) DO UPDATE +SET + user_id = EXCLUDED.user_id, + team_id = EXCLUDED.team_id, + role = EXCLUDED.role; \ No newline at end of file diff --git a/scripts/seed-database/write/teams.sql b/scripts/seed-database/write/teams.sql index a798aaff8f..0eaabb0bb6 100644 --- a/scripts/seed-database/write/teams.sql +++ b/scripts/seed-database/write/teams.sql @@ -1,4 +1,4 @@ --- insert teams skipping conflicts +-- insert teams overwriting conflicts CREATE TEMPORARY TABLE sync_teams ( id integer, name text, @@ -33,6 +33,13 @@ SELECT notify_personalisation, boundary FROM sync_teams -ON CONFLICT (id) DO UPDATE; +ON CONFLICT (id) DO UPDATE +SET + name = EXCLUDED.name, + slug = EXCLUDED.slug, + theme = EXCLUDED.theme, + settings = EXCLUDED.settings, + notify_personalisation = EXCLUDED.notify_personalisation, + boundary = EXCLUDED.boundary; SELECT setval('teams_id_seq', max(id)) FROM teams; diff --git a/scripts/seed-database/write/users.sql b/scripts/seed-database/write/users.sql index ee4444ae94..8d2215f1ea 100644 --- a/scripts/seed-database/write/users.sql +++ b/scripts/seed-database/write/users.sql @@ -1,4 +1,4 @@ --- insert users skipping conflicts +-- insert users overwriting conflicts CREATE TEMPORARY TABLE sync_users ( id integer, first_name text, @@ -30,7 +30,12 @@ SELECT email, is_platform_admin FROM sync_users -ON CONFLICT (id) DO UPDATE; +ON CONFLICT (id) DO UPDATE +SET + first_name = EXCLUDED.first_name, + last_name = EXCLUDED.last_name, + email = EXCLUDED.email, + is_platform_admin = EXCLUDED.is_platform_admin; ALTER TABLE users ENABLE TRIGGER grant_new_user_template_team_access;