Skip to content

Commit

Permalink
fix: SQL 'DO UPDATE' syntax for data sync script (#2333)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Oct 20, 2023
1 parent f7b84f3 commit c608dd8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
12 changes: 10 additions & 2 deletions scripts/seed-database/write/flows.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- insert flows skipping conflicts
-- insert flows overwriting conflicts
CREATE TEMPORARY TABLE sync_flows (
id uuid,
team_id int,
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion scripts/seed-database/write/published_flows.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- insert published_flows overwriting conflicts
CREATE TEMPORARY TABLE sync_published_flows (
id int,
data jsonb,
Expand Down Expand Up @@ -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;
9 changes: 7 additions & 2 deletions scripts/seed-database/write/team_members.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
-- insert team_members overwriting conflicts
CREATE TEMPORARY TABLE sync_team_members (
id uuid,
user_id integer,
team_id integer,
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;
sync_team_members ON CONFLICT (id) DO UPDATE
SET
user_id = EXCLUDED.user_id,
team_id = EXCLUDED.team_id,
role = EXCLUDED.role;
11 changes: 9 additions & 2 deletions scripts/seed-database/write/teams.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- insert teams skipping conflicts
-- insert teams overwriting conflicts
CREATE TEMPORARY TABLE sync_teams (
id integer,
name text,
Expand Down Expand Up @@ -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;
9 changes: 7 additions & 2 deletions scripts/seed-database/write/users.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- insert users skipping conflicts
-- insert users overwriting conflicts
CREATE TEMPORARY TABLE sync_users (
id integer,
first_name text,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c608dd8

Please sign in to comment.