You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/prisma-client/prisma/migrations/20251129093846_add_updated_at_to_latest_build_virtual/migration.sql
+47-19Lines changed: 47 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,9 @@
1
1
-- Add updatedAt field to latestBuildVirtual virtual table (type definition)
2
2
-- and update both functions to include Build's updatedAt timestamp
3
-
-- Add updatedAt column to the virtual table type definition (only if it doesn't exist)
4
-
DO $$
5
-
BEGIN
6
-
IF NOT EXISTS (
7
-
SELECT1
8
-
FROMinformation_schema.columns
9
-
WHERE table_schema ='public'
10
-
AND table_name ='latestBuildVirtual'
11
-
AND column_name ='updatedAt'
12
-
) THEN
13
-
ALTERTABLE"latestBuildVirtual"
14
-
ADD COLUMN "updatedAt"timestamp(3) with time zoneNOT NULL;
15
-
END IF;
16
-
END $$;
3
+
-- Add updatedAt column to the virtual table type definition (uses IF NOT EXISTS for idempotency)
4
+
-- Note: This uses PostgreSQL 9.6+ syntax for IF NOT EXISTS
5
+
ALTERTABLE IF EXISTS "latestBuildVirtual"
6
+
ADD COLUMN IF NOT EXISTS "updatedAt"timestamp(3) with time zoneNOT NULL DEFAULT NOW();
17
7
18
8
COMMENT ON COLUMN "latestBuildVirtual"."updatedAt" IS 'Timestamp indicating when the Build was last updated';
19
9
@@ -39,7 +29,7 @@ FROM
39
29
LEFT JOIN"ProjectDomain" pd ON pd."projectId"=p.id
40
30
LEFT JOIN"Domain" d ONd.id= pd."domainId"
41
31
WHERE
42
-
b."projectId"= $1.id
32
+
b."projectId"= $1.id
43
33
ANDb.deploymentIS NOT NULL-- 'destination' IS NULL for backward compatibility; 'destination' = 'saas' for non-static builds
44
34
AND (
45
35
(b.deployment :: jsonb ->>'destination') IS NULL
@@ -74,9 +64,9 @@ SELECT
74
64
b."updatedAt"
75
65
FROM
76
66
"Build" b
77
-
JOIN"Domain" d ONd.id= $1."domainId"
67
+
JOIN"Domain" d ONd.id= $1."domainId"
78
68
WHERE
79
-
b."projectId"= $1."projectId"
69
+
b."projectId"= $1."projectId"
80
70
ANDb.deploymentIS NOT NULL
81
71
AND (b.deployment :: jsonb ->'domains') @ > to_jsonb(array [d.domain])
82
72
ORDER BY
@@ -105,7 +95,7 @@ FROM
105
95
JOIN"Project" p ON b."projectId"=p.id
106
96
LEFT JOIN"ProjectDomain" pd ON pd."projectId"=p.id
107
97
WHERE
108
-
b."projectId"= $1.id
98
+
b."projectId"= $1.id
109
99
ANDb.deploymentIS NOT NULL
110
100
AND (
111
101
(b.deployment :: jsonb ->>'destination') IS NULL
@@ -122,4 +112,42 @@ LIMIT
122
112
123
113
$$ STABLE LANGUAGE sql;
124
114
125
-
COMMENT ON FUNCTION "latestProjectDomainBuildVirtual"("Project") IS 'This function computes the latest build for a project domain, ensuring it is a production (non-static) build, where the domain matches either the Project.domain field or exists in the related Domain table. It provides backward compatibility for older records with a missing "destination" field.';
115
+
COMMENT ON FUNCTION "latestProjectDomainBuildVirtual"("Project") IS 'This function computes the latest build for a project domain, ensuring it is a production (non-static) build, where the domain matches either the Project.domain field or exists in the related Domain table. It provides backward compatibility for older records with a missing "destination" field.';
116
+
117
+
-- Add latestBuildVirtual function overload for DashboardProject view
118
+
-- This is needed because PostgREST computed fields require a function
119
+
-- that matches the source table/view type. DashboardProject is a view
120
+
-- over Project, so we need this wrapper function.
121
+
CREATE
122
+
OR REPLACE FUNCTION "latestBuildVirtual"("DashboardProject") RETURNS SETOF "latestBuildVirtual" ROWS 1AS $$
123
+
SELECT
124
+
*
125
+
FROM
126
+
"latestBuildVirtual"(
127
+
(
128
+
SELECT
129
+
p
130
+
FROM
131
+
"Project" p
132
+
WHERE
133
+
p.id= $1.id
134
+
)
135
+
);
136
+
137
+
$$ STABLE LANGUAGE sql;
138
+
139
+
COMMENT ON FUNCTION "latestBuildVirtual"("DashboardProject") IS 'Wrapper function to make latestBuildVirtual work with DashboardProject view for PostgREST computed fields.';
140
+
141
+
-- Grant execute permissions to all PostgREST roles
142
+
-- Uses DO block to check if roles exist before granting (prevents errors if roles are missing)
143
+
DO $$
144
+
DECLARE
145
+
role_name TEXT;
146
+
BEGIN
147
+
FOREACH role_name IN ARRAY ARRAY['anon', 'authenticated', 'service_role']
148
+
LOOP
149
+
IF EXISTS (SELECT1FROM pg_roles WHERE rolname = role_name) THEN
150
+
EXECUTE format('GRANT EXECUTE ON FUNCTION "latestBuildVirtual"("DashboardProject") TO %I', role_name);
Copy file name to clipboardExpand all lines: packages/prisma-client/prisma/migrations/20251130131728_add_latest_build_virtual_for_dashboard_project/migration.sql
+10-27Lines changed: 10 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -22,33 +22,16 @@ $$ STABLE LANGUAGE sql;
22
22
23
23
COMMENT ON FUNCTION "latestBuildVirtual"("DashboardProject") IS 'Wrapper function to make latestBuildVirtual work with DashboardProject view for PostgREST computed fields.';
24
24
25
-
-- Grant execute permissions to all PostgREST roles (only if they exist)
25
+
-- Grant execute permissions to all PostgREST roles
26
+
-- Uses DO block to check if roles exist before granting (prevents errors if roles are missing)
26
27
DO $$
28
+
DECLARE
29
+
role_name TEXT;
27
30
BEGIN
28
-
-- Grant to anon if role exists
29
-
IF EXISTS (
30
-
SELECT1
31
-
FROM pg_roles
32
-
WHERE rolname ='anon'
33
-
) THEN
34
-
GRANT EXECUTE ON FUNCTION "latestBuildVirtual"("DashboardProject") TO anon;
35
-
END IF;
36
-
37
-
-- Grant to authenticated if role exists
38
-
IF EXISTS (
39
-
SELECT1
40
-
FROM pg_roles
41
-
WHERE rolname ='authenticated'
42
-
) THEN
43
-
GRANT EXECUTE ON FUNCTION "latestBuildVirtual"("DashboardProject") TO authenticated;
44
-
END IF;
45
-
46
-
-- Grant to service_role if role exists
47
-
IF EXISTS (
48
-
SELECT1
49
-
FROM pg_roles
50
-
WHERE rolname ='service_role'
51
-
) THEN
52
-
GRANT EXECUTE ON FUNCTION "latestBuildVirtual"("DashboardProject") TO service_role;
53
-
END IF;
31
+
FOREACH role_name IN ARRAY ARRAY['anon', 'authenticated', 'service_role']
32
+
LOOP
33
+
IF EXISTS (SELECT1FROM pg_roles WHERE rolname = role_name) THEN
34
+
EXECUTE format('GRANT EXECUTE ON FUNCTION "latestBuildVirtual"("DashboardProject") TO %I', role_name);
0 commit comments