Skip to content

Commit e5e53bd

Browse files
authored
fix: Resolve failing database tests on production environment (#4156)
## Description Prev PR #4143 ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 5de6) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
1 parent fbdf91a commit e5e53bd

File tree

5 files changed

+441
-38
lines changed

5 files changed

+441
-38
lines changed
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
BEGIN;
2+
SET LOCAL search_path = pgtap, public;
3+
-- Initialize the testing environment without planning any specific number of tests
4+
-- We are using SELECT no_plan() because we don't specify the exact number of tests upfront.
5+
SELECT no_plan();
6+
7+
-- =========================================
8+
-- Setup: Insert initial data for Users, Projects, Domains, ProjectDomains, and Builds
9+
-- =========================================
10+
11+
-- Insert a user into the "User" table
12+
INSERT INTO "public"."User" ("id", "createdAt", "email", "username")
13+
VALUES
14+
('user1', '2023-01-01 00:00:00+00', '[email protected]', 'user1');
15+
16+
-- Insert projects associated with the user into the "Project" table
17+
INSERT INTO "public"."Project" ("id", "title", "domain", "userId", "isDeleted", "createdAt")
18+
VALUES
19+
('project1', 'Project One', '517cce32-9af3-project1-domain1', 'user1', false, '2023-01-01 00:00:00+00'),
20+
('project2', 'Project Two', '517cce32-9af3-project2-domain1', 'user1', false, '2023-01-01 00:00:00+00');
21+
22+
-- Insert custom domains into the "Domain" table
23+
INSERT INTO "public"."Domain" ("id", "domain", "createdAt", "status", "updatedAt")
24+
VALUES
25+
('project-1-custom-domain-1', '517cce32-9af3-project-1-custom-domain-1.com', '2023-01-01 00:00:00+00', 'INITIALIZING', '2023-01-01 00:00:00+00'),
26+
('project-1-custom-domain-2', '517cce32-9af3-project-1-custom-domain-2.com', '2023-01-01 00:00:00+00', 'INITIALIZING', '2023-01-01 00:00:00+00'),
27+
('project-2-custom-domain-1', '517cce32-9af3-project-2-custom-domain-1.com', '2023-01-01 00:00:00+00', 'INITIALIZING', '2023-01-01 00:00:00+00'),
28+
('project-2-custom-domain-2', '517cce32-9af3-project-2-custom-domain-2.com', '2023-01-01 00:00:00+00', 'INITIALIZING', '2023-01-01 00:00:00+00');
29+
30+
-- Establish relationships between projects and custom domains in the "ProjectDomain" table
31+
INSERT INTO "public"."ProjectDomain" ("projectId", "domainId", "createdAt", "txtRecord", "cname")
32+
VALUES
33+
('project1', 'project-1-custom-domain-1', '2023-01-01 00:00:00+00', 'txtRecord1', 'cname1'),
34+
('project1', 'project-1-custom-domain-2', '2023-01-01 00:00:00+00', 'txtRecord2', 'cname2'),
35+
('project2', 'project-2-custom-domain-1', '2023-01-01 00:00:00+00', 'p2-txtRecord1', 'cname1'),
36+
('project2', 'project-2-custom-domain-2', '2023-01-01 00:00:00+00', 'p2-txtRecord2', 'cname2');
37+
38+
-- Insert initial builds into the "Build" table
39+
INSERT INTO "public"."Build" (
40+
"id",
41+
"createdAt",
42+
"pages",
43+
"projectId",
44+
"deployment",
45+
"updatedAt",
46+
"publishStatus"
47+
)
48+
VALUES
49+
-- Development Build for Project1
50+
(
51+
'build1-development',
52+
'1990-01-01 00:00:00+00',
53+
'home',
54+
'project1',
55+
NULL,
56+
'1990-01-01 00:00:00+00',
57+
'PENDING'
58+
),
59+
-- Development Build for Project2
60+
(
61+
'project2-build1-development',
62+
'1990-01-01 00:00:00+00',
63+
'home',
64+
'project2',
65+
NULL,
66+
'1990-01-01 00:00:00+00',
67+
'PENDING'
68+
),
69+
-- Custom Domain Build for Project2
70+
(
71+
'project2-build1-for-custom-domain-1',
72+
'1990-01-02 00:00:00+00',
73+
'home',
74+
'project2',
75+
'{"domains": ["517cce32-9af3-project-2-custom-domain-1.com"]}'::text,
76+
'1990-01-02 00:00:00+00',
77+
'PUBLISHED'
78+
),
79+
-- Project Domain Build for Project2
80+
(
81+
'project2-build1-for-project-domain-1',
82+
'1990-01-01 00:00:00+00',
83+
'home',
84+
'project2',
85+
'{"domains": ["517cce32-9af3-project2-domain1"]}'::text,
86+
'1990-01-01 00:00:00+00',
87+
'PUBLISHED'
88+
),
89+
-- Custom Domain Build for Project1
90+
(
91+
'build1-for-custom-domain-1',
92+
'1990-01-02 00:00:00+00',
93+
'home',
94+
'project1',
95+
'{"domains": ["517cce32-9af3-project-1-custom-domain-1.com"]}'::text,
96+
'1990-01-02 00:00:00+00',
97+
'PUBLISHED'
98+
),
99+
-- Project Domain Build for Project1
100+
(
101+
'build1-for-project-domain-1',
102+
'1990-01-01 00:00:00+00',
103+
'home',
104+
'project1',
105+
'{"domains": ["517cce32-9af3-project1-domain1"]}'::text,
106+
'1990-01-01 00:00:00+00',
107+
'PUBLISHED'
108+
);
109+
110+
-- =========================================
111+
-- Test 1: Verify that initial cleanup does not clean any builds
112+
-- =========================================
113+
114+
-- Run the database cleanup function for builds created in 1990
115+
SELECT database_cleanup('1990-01-01 00:00:00', '1990-12-31 23:59:59');
116+
117+
-- Assert that no builds have been cleaned up initially
118+
SELECT is(
119+
(SELECT count(*)::integer FROM "Build" WHERE "createdAt" BETWEEN '1990-01-01' AND '1990-12-31' AND "isCleaned" = TRUE),
120+
0,
121+
'Test 1: No builds should be cleaned up on initial cleanup'
122+
);
123+
124+
-- =========================================
125+
-- Test 2: Insert a new project domain build and verify cleanup
126+
-- =========================================
127+
128+
-- Insert a new build for the project domain
129+
INSERT INTO "public"."Build" (
130+
"id",
131+
"createdAt",
132+
"pages",
133+
"projectId",
134+
"deployment",
135+
"updatedAt",
136+
"publishStatus"
137+
)
138+
VALUES
139+
-- New Project Domain Build for Project1
140+
(
141+
'build2-for-project-domain-1',
142+
'1990-01-02 00:00:00+00', -- A later date than the previous build
143+
'home',
144+
'project1',
145+
'{"domains": ["517cce32-9af3-project1-domain1"]}'::text,
146+
'1990-01-02 00:00:00+00',
147+
'PUBLISHED'
148+
);
149+
150+
-- Run the database cleanup function again
151+
SELECT database_cleanup('1990-01-01 00:00:00', '1990-12-31 23:59:59');
152+
153+
-- Assert that one build has been cleaned (the previous project domain build)
154+
SELECT is(
155+
(SELECT count(*)::integer FROM "Build" WHERE "createdAt" BETWEEN '1990-01-01' AND '1990-12-31' AND "isCleaned" = TRUE),
156+
1,
157+
'Test 2: Previous project domain build should be cleaned up after new build is published'
158+
);
159+
160+
-- Assert that the specific build cleaned is 'build1-for-project-domain-1'
161+
SELECT is(
162+
(SELECT id FROM "Build" WHERE "createdAt" BETWEEN '1990-01-01' AND '1990-12-31' AND "isCleaned" = TRUE),
163+
'build1-for-project-domain-1',
164+
'Test 2: Build "build1-for-project-domain-1" should be marked as cleaned'
165+
);
166+
167+
-- =========================================
168+
-- Test 3: Insert a new custom domain build and verify cleanup
169+
-- =========================================
170+
171+
-- Insert a new build for the custom domain
172+
INSERT INTO "public"."Build" (
173+
"id",
174+
"createdAt",
175+
"pages",
176+
"projectId",
177+
"deployment",
178+
"updatedAt",
179+
"publishStatus"
180+
)
181+
VALUES
182+
-- New Custom Domain Build for Project1
183+
(
184+
'build2-for-custom-domain-1',
185+
'1990-01-03 00:00:00+00', -- A later date than the previous build
186+
'home',
187+
'project1',
188+
'{"domains": ["517cce32-9af3-project-1-custom-domain-1.com"]}'::text,
189+
'1990-01-03 00:00:00+00',
190+
'PUBLISHED'
191+
);
192+
193+
-- Run the database cleanup function again
194+
SELECT database_cleanup('1990-01-01 00:00:00', '1990-12-31 23:59:59');
195+
196+
-- Assert that two builds have been cleaned (the previous project domain and custom domain builds)
197+
SELECT is(
198+
(SELECT count(*)::integer FROM "Build" WHERE "createdAt" BETWEEN '1990-01-01' AND '1990-12-31' AND "isCleaned" = TRUE),
199+
2,
200+
'Test 3: Previous custom domain build should be cleaned up after new build is published'
201+
);
202+
203+
-- Assert that the builds cleaned are 'build1-for-custom-domain-1' and 'build1-for-project-domain-1'
204+
SELECT results_eq(
205+
$$
206+
SELECT id FROM "Build" WHERE "createdAt" BETWEEN '1990-01-01' AND '1990-12-31' AND "isCleaned" = TRUE ORDER BY id
207+
$$,
208+
$$
209+
SELECT * FROM (
210+
VALUES
211+
('build1-for-custom-domain-1'),
212+
('build1-for-project-domain-1')
213+
) AS expected(id)
214+
ORDER BY "id"
215+
$$,
216+
'Test 3: Builds "build1-for-custom-domain-1" and "build1-for-project-domain-1" should be marked as cleaned'
217+
);
218+
219+
-- =========================================
220+
-- Finish the test
221+
-- =========================================
222+
223+
-- Finish the test by calling the finish() function, which outputs the test summary
224+
SELECT finish();
225+
226+
-- Rollback the transaction to ensure no changes are persisted in the database
227+
ROLLBACK;

packages/postgrest/supabase/tests/latest-builds-domains.sql

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ SELECT no_plan();
99
-- Insert a new user into the User table
1010
INSERT INTO "public"."User" ("id", "createdAt", "email", "username")
1111
VALUES
12-
('user1', '2023-01-01 00:00:00+00', '[email protected]', 'user1');
12+
('user1', '2023-01-01 00:00:00+00', 'user1@517cce32-9af3-example.com', 'user1');
1313

1414
-- Insert projects associated with the user
1515
INSERT INTO "public"."Project" ("id", "title", "domain", "userId", "isDeleted", "createdAt")
1616
VALUES
17-
('project1', 'Project One', 'project1-domain1', 'user1', false, '2023-01-01 00:00:00+00'),
18-
('project2', 'Project Two', 'project2-domain1', 'user1', false, '2023-01-01 00:00:00+00');
17+
('project1', 'Project One', '517cce32-9af3-project1-domain1', 'user1', false, '2023-01-01 00:00:00+00'),
18+
('project2', 'Project Two', '517cce32-9af3-project2-domain1', 'user1', false, '2023-01-01 00:00:00+00');
1919

2020
-- Insert custom domains into the Domain table
2121
INSERT INTO "public"."Domain" ("id", "domain", "createdAt", "status", "updatedAt")
2222
VALUES
23-
('project-1-custom-domain-1', 'project-1-custom-domain-1.com', '2023-01-01 00:00:00+00', 'INITIALIZING', '2023-01-01 00:00:00+00'),
24-
('project-1-custom-domain-2', 'project-1-custom-domain-2.com', '2023-01-01 00:00:00+00', 'INITIALIZING', '2023-01-01 00:00:00+00');
23+
('project-1-custom-domain-1', '517cce32-9af3-project-1-custom-domain-1.com', '2023-01-01 00:00:00+00', 'INITIALIZING', '2023-01-01 00:00:00+00'),
24+
('project-1-custom-domain-2', '517cce32-9af3-project-1-custom-domain-2.com', '2023-01-01 00:00:00+00', 'INITIALIZING', '2023-01-01 00:00:00+00');
2525

2626
-- Establish relationships between projects and custom domains
2727
INSERT INTO "public"."ProjectDomain" ("projectId", "domainId", "createdAt", "txtRecord", "cname")
@@ -76,7 +76,7 @@ VALUES
7676
'2023-01-02 00:00:00+00',
7777
'home',
7878
'project1',
79-
'{"domains": ["some-other-domain.com", "project-1-custom-domain-1.com"]}'::text,
79+
'{"domains": ["some-other-domain.com", "517cce32-9af3-project-1-custom-domain-1.com"]}'::text,
8080
'2023-01-02 00:00:00+00',
8181
'PUBLISHED'
8282
);
@@ -113,7 +113,7 @@ VALUES
113113
'2024-01-02 00:00:00+00',
114114
'home',
115115
'project1',
116-
'{"domains": ["some-other-domain.com", "project-1-custom-domain-1.com"]}'::text,
116+
'{"domains": ["some-other-domain.com", "517cce32-9af3-project-1-custom-domain-1.com"]}'::text,
117117
'2024-01-02 00:00:00+00',
118118
'PUBLISHED'
119119
);
@@ -150,7 +150,7 @@ VALUES
150150
'2024-01-03 00:00:00+00',
151151
'home',
152152
'project1',
153-
'{"domains": ["some-other-domain.com", "project-1-custom-domain-2.com"]}'::text,
153+
'{"domains": ["some-other-domain.com", "517cce32-9af3-project-1-custom-domain-2.com"]}'::text,
154154
'2024-01-03 00:00:00+00',
155155
'PUBLISHED'
156156
);
@@ -187,7 +187,7 @@ VALUES
187187
'2024-01-04 00:00:00+00',
188188
'home',
189189
'project1',
190-
'{"domains": ["some-other-domain.com", "project-1-custom-domain-2.com", "project-1-custom-domain-1.com"]}'::text,
190+
'{"domains": ["some-other-domain.com", "517cce32-9af3-project-1-custom-domain-2.com", "517cce32-9af3-project-1-custom-domain-1.com"]}'::text,
191191
'2024-01-04 00:00:00+00',
192192
'PUBLISHED'
193193
);
@@ -230,7 +230,7 @@ VALUES
230230
'2025-01-04 00:00:00+00',
231231
'home',
232232
'project2',
233-
'{"domains": ["some-other-domain.com", "project-1-custom-domain-2.com", "project-1-custom-domain-1.com"]}'::text,
233+
'{"domains": ["some-other-domain.com", "517cce32-9af3-project-1-custom-domain-2.com", "517cce32-9af3-project-1-custom-domain-1.com"]}'::text,
234234
'2025-01-04 00:00:00+00',
235235
'PUBLISHED'
236236
);

0 commit comments

Comments
 (0)