Skip to content

Commit 7c2e907

Browse files
committed
fixing migrations ::migrate::
1 parent 64100cd commit 7c2e907

File tree

2 files changed

+57
-46
lines changed
  • packages/prisma-client/prisma/migrations
    • 20251129093846_add_updated_at_to_latest_build_virtual
    • 20251130131728_add_latest_build_virtual_for_dashboard_project

2 files changed

+57
-46
lines changed

packages/prisma-client/prisma/migrations/20251129093846_add_updated_at_to_latest_build_virtual/migration.sql

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
-- Add updatedAt field to latestBuildVirtual virtual table (type definition)
22
-- 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-
SELECT 1
8-
FROM information_schema.columns
9-
WHERE table_schema = 'public'
10-
AND table_name = 'latestBuildVirtual'
11-
AND column_name = 'updatedAt'
12-
) THEN
13-
ALTER TABLE "latestBuildVirtual"
14-
ADD COLUMN "updatedAt" timestamp(3) with time zone NOT 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+
ALTER TABLE IF EXISTS "latestBuildVirtual"
6+
ADD COLUMN IF NOT EXISTS "updatedAt" timestamp(3) with time zone NOT NULL DEFAULT NOW();
177

188
COMMENT ON COLUMN "latestBuildVirtual"."updatedAt" IS 'Timestamp indicating when the Build was last updated';
199

@@ -39,7 +29,7 @@ FROM
3929
LEFT JOIN "ProjectDomain" pd ON pd."projectId" = p.id
4030
LEFT JOIN "Domain" d ON d.id = pd."domainId"
4131
WHERE
42-
b."projectId" = $ 1.id
32+
b."projectId" = $1.id
4333
AND b.deployment IS NOT NULL -- 'destination' IS NULL for backward compatibility; 'destination' = 'saas' for non-static builds
4434
AND (
4535
(b.deployment :: jsonb ->> 'destination') IS NULL
@@ -74,9 +64,9 @@ SELECT
7464
b."updatedAt"
7565
FROM
7666
"Build" b
77-
JOIN "Domain" d ON d.id = $ 1."domainId"
67+
JOIN "Domain" d ON d.id = $1."domainId"
7868
WHERE
79-
b."projectId" = $ 1."projectId"
69+
b."projectId" = $1."projectId"
8070
AND b.deployment IS NOT NULL
8171
AND (b.deployment :: jsonb -> 'domains') @ > to_jsonb(array [d.domain])
8272
ORDER BY
@@ -105,7 +95,7 @@ FROM
10595
JOIN "Project" p ON b."projectId" = p.id
10696
LEFT JOIN "ProjectDomain" pd ON pd."projectId" = p.id
10797
WHERE
108-
b."projectId" = $ 1.id
98+
b."projectId" = $1.id
10999
AND b.deployment IS NOT NULL
110100
AND (
111101
(b.deployment :: jsonb ->> 'destination') IS NULL
@@ -122,4 +112,42 @@ LIMIT
122112

123113
$$ STABLE LANGUAGE sql;
124114

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 1 AS $$
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 (SELECT 1 FROM pg_roles WHERE rolname = role_name) THEN
150+
EXECUTE format('GRANT EXECUTE ON FUNCTION "latestBuildVirtual"("DashboardProject") TO %I', role_name);
151+
END IF;
152+
END LOOP;
153+
END $$;

packages/prisma-client/prisma/migrations/20251130131728_add_latest_build_virtual_for_dashboard_project/migration.sql

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,16 @@ $$ STABLE LANGUAGE sql;
2222

2323
COMMENT ON FUNCTION "latestBuildVirtual"("DashboardProject") IS 'Wrapper function to make latestBuildVirtual work with DashboardProject view for PostgREST computed fields.';
2424

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)
2627
DO $$
28+
DECLARE
29+
role_name TEXT;
2730
BEGIN
28-
-- Grant to anon if role exists
29-
IF EXISTS (
30-
SELECT 1
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-
SELECT 1
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-
SELECT 1
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 (SELECT 1 FROM pg_roles WHERE rolname = role_name) THEN
34+
EXECUTE format('GRANT EXECUTE ON FUNCTION "latestBuildVirtual"("DashboardProject") TO %I', role_name);
35+
END IF;
36+
END LOOP;
5437
END $$;

0 commit comments

Comments
 (0)