Skip to content

Commit f157dbf

Browse files
committed
Pre-lock tables in clean.sql
1 parent 210ceb0 commit f157dbf

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

transcripts/sql/clean.sql

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
\set ON_ERROR_STOP true
22

3+
BEGIN ;
4+
35
DO $$
46
BEGIN
57
IF NOT EXISTS (
@@ -10,6 +12,43 @@ BEGIN
1012
END IF;
1113
END $$;
1214

15+
-- Pre-lock all tables so that share background jobs can't sneak in and lock things mid transaction.
16+
-- Lock all tables in a single statement to prevent deadlocks
17+
LOCK TABLE
18+
users,
19+
superadmins,
20+
role_memberships,
21+
loose_code_roots,
22+
org_members,
23+
tours,
24+
projects,
25+
project_branches,
26+
project_releases,
27+
project_favorites,
28+
catalog_categories,
29+
project_categories,
30+
oauth_clients,
31+
contributions,
32+
contribution_status_events,
33+
comments,
34+
comment_revisions,
35+
tickets,
36+
teams,
37+
team_members,
38+
orgs,
39+
subjects,
40+
resources,
41+
causal_diff_queue,
42+
namespace_diffs,
43+
personal_keys,
44+
history_comments,
45+
history_comment_revisions,
46+
history_comment_revisions_project_discovery,
47+
namespace_ownership,
48+
causal_ownership,
49+
public.cloud_subscribers
50+
IN ACCESS EXCLUSIVE MODE;
51+
1352
-- Resets most relevant tables, useful to run between tests.
1453
-- Doesn't clean codebase tables since that just slows things down, but does clean out codebase ownership.
1554
SET client_min_messages TO WARNING;
@@ -26,11 +65,6 @@ TRUNCATE TABLE project_favorites CASCADE;
2665
TRUNCATE TABLE catalog_categories CASCADE;
2766
TRUNCATE TABLE project_categories CASCADE;
2867
TRUNCATE TABLE oauth_clients CASCADE;
29-
TRUNCATE TABLE oauth_clients CASCADE;
30-
TRUNCATE TABLE contributions CASCADE;
31-
TRUNCATE TABLE contribution_status_events CASCADE;
32-
TRUNCATE TABLE comments CASCADE;
33-
TRUNCATE TABLE comment_revisions CASCADE;
3468
TRUNCATE TABLE contributions CASCADE;
3569
TRUNCATE TABLE contribution_status_events CASCADE;
3670
TRUNCATE TABLE tickets CASCADE;
@@ -57,3 +91,5 @@ TRUNCATE TABLE causal_ownership CASCADE;
5791
-- TRUNCATE TABLE causals CASCADE;
5892

5993
TRUNCATE TABLE public.cloud_subscribers CASCADE;
94+
95+
COMMIT;

0 commit comments

Comments
 (0)