Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

chore: make migrations idempotent (#5476)

Oleg Isonen1412d868baa9
diff --git a/.github/workflows/migrate.yaml b/.github/workflows/migrate.yaml
index a48fdeb236..f818995321 100644
--- a/.github/workflows/migrate.yaml
+++ b/.github/workflows/migrate.yaml
@@ -57,8 +57,9 @@ jobs:
env:
DIRECT_URL: ${{ secrets.DIRECT_URL }}
- # Execute db tests (@todo: can be done only after applying the migrations, now always())
+ # Execute db tests (runs after migrations, even if they fail)
db-tests:
+ # Always run tests to see failures on CI, but only after migrate job completes
if: always()
needs: [migrate]
@@ -75,7 +76,7 @@ jobs:
- uses: pnpm/action-setup@v4
- - name: pnpm instal
+ - name: Run database tests
run: pnpm -r db-test
env:
DIRECT_URL: ${{ secrets.DIRECT_URL }}
diff --git a/.prettierignore b/.prettierignore
index ee62e080a4..265f84a383 100644
--- a/.prettierignore
+++ b/.prettierignore
@@ -1,5 +1,3 @@
pnpm-lock.yaml
packages/prisma-client/prisma/migrations/*/client
-packages/prisma-client/**/*.d.ts
-packages/prisma-client/prisma/migrations/**/*.sql
-packages/postgrest/supabase/tests/*.sql
+packages/prisma-client/**/*.d.ts
\ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 25fa6215fd..cb59ebc7c2 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -1,3 +1,16 @@
{
- "typescript.tsdk": "node_modules/typescript/lib"
+ "typescript.tsdk": "node_modules/typescript/lib",
+ // Disable formatting for SQL files
+ "[sql]": {
+ "editor.formatOnSave": false,
+ "editor.formatOnPaste": false,
+ "editor.formatOnType": false
+ },
+ // Disable Prettier for SQL files specifically
+ "prettier.enable": true,
+ "prettier.ignorePath": ".prettierignore",
+ // Exclude SQL files from auto-formatting
+ "files.associations": {
+ "*.sql": "sql"
+ }
}
diff --git a/apps/builder/app/dashboard/projects/project-card.tsx b/apps/builder/app/dashboard/projects/project-card.tsx
index ddd5a89d88..339a294f69 100644
--- a/apps/builder/app/dashboard/projects/project-card.tsx
+++ b/apps/builder/app/dashboard/projects/project-card.tsx
@@ -222,7 +222,7 @@ export const ProjectCard = ({
</>
)}
<br />
- {latestBuildVirtual?.publishStatus === "PUBLISHED" ? (
+ {isPublished && latestBuildVirtual ? (
<>Published: {formatDate(latestBuildVirtual.createdAt)}</>
) : (
<>Not published</>
diff --git a/packages/prisma-client/prisma/migrations/20251129093846_add_updated_at_to_latest_build_virtual/migration.sql b/packages/prisma-client/prisma/migrations/20251129093846_add_updated_at_to_latest_build_virtual/migration.sql
index 66105c60d1..ca56c95870 100644
--- a/packages/prisma-client/prisma/migrations/20251129093846_add_updated_at_to_latest_build_virtual/migration.sql
+++ b/packages/prisma-client/prisma/migrations/20251129093846_add_updated_at_to_latest_build_virtual/migration.sql
@@ -1,12 +1,17 @@
-- Add updatedAt field to latestBuildVirtual virtual table (type definition)
-- and update both functions to include Build's updatedAt timestamp
+
+-- First, drop existing functions that depend on the old table structure
+DROP FUNCTION IF EXISTS "latestBuildVirtual"("Project");
+DROP FUNCTION IF EXISTS "latestBuildVirtual"("domainsVirtual");
+DROP FUNCTION IF EXISTS "latestProjectDomainBuildVirtual"("Project");
+DROP FUNCTION IF EXISTS "latestBuildVirtual"("DashboardProject");
+
-- Add updatedAt column to the virtual table type definition
-ALTER TABLE
- "latestBuildVirtual"
-ADD
- COLUMN "updatedAt" timestamp(3) with time zone NOT NULL;
+ALTER TABLE "latestBuildVirtual"
+ ADD COLUMN "updatedAt" timestamp(3) with time zone NOT NULL DEFAULT NOW();
-COMMENT ON COLUMN "latestBuildVirtual"."updatedAt" IS 'Timestamp indicating when the Build was last updated';
+COMMENT ON COLUMN "public"."latestBuildVirtual"."updatedAt" IS 'Timestamp indicating when the Build was last updated';
-- Recreate the function for Project with updatedAt field
CREATE
@@ -18,7 +23,7 @@ SELECT
-- Use CASE to determine which domain to select based on conditions
CASE
WHEN (b.deployment :: jsonb ->> 'projectDomain') = p.domain
- OR (b.deployment :: jsonb -> 'domains') @ > to_jsonb(array [p.domain]) THEN p.domain
+ OR (b.deployment :: jsonb -> 'domains') @> to_jsonb(array [p.domain]) THEN p.domain
ELSE d.domain
END AS "domain",
b."createdAt",
@@ -30,7 +35,7 @@ FROM
LEFT JOIN "ProjectDomain" pd ON pd."projectId" = p.id
LEFT JOIN "Domain" d ON d.id = pd."domainId"
WHERE
- b."projectId" = $ 1.id
+ b."projectId" = $1.id
AND b.deployment IS NOT NULL -- 'destination' IS NULL for backward compatibility; 'destination' = 'saas' for non-static builds
AND (
(b.deployment :: jsonb ->> 'destination') IS NULL
@@ -39,8 +44,8 @@ WHERE
AND (
-- Check if 'projectDomain' matches p.domain
(b.deployment :: jsonb ->> 'projectDomain') = p.domain -- Check if 'domains' contains p.domain or d.domain
- OR (b.deployment :: jsonb -> 'domains') @ > to_jsonb(array [p.domain])
- OR (b.deployment :: jsonb -> 'domains') @ > to_jsonb(array [d.domain])
+ OR (b.deployment :: jsonb -> 'domains') @> to_jsonb(array [p.domain])
+ OR (b.deployment :: jsonb -> 'domains') @> to_jsonb(array [d.domain])
)
ORDER BY
b."createdAt" DESC
@@ -65,11 +70,11 @@ SELECT
b."updatedAt"
FROM
"Build" b
- JOIN "Domain" d ON d.id = $ 1."domainId"
+ JOIN "Domain" d ON d.id = $1."domainId"
WHERE
- b."projectId" = $ 1."projectId"
+ b."projectId" = $1."projectId"
AND b.deployment IS NOT NULL
- AND (b.deployment :: jsonb -> 'domains') @ > to_jsonb(array [d.domain])
+ AND (b.deployment :: jsonb -> 'domains') @> to_jsonb(array [d.domain])
ORDER BY
b."createdAt" DESC
LIMIT
@@ -96,7 +101,7 @@ FROM
JOIN "Project" p ON b."projectId" = p.id
LEFT JOIN "ProjectDomain" pd ON pd."projectId" = p.id
WHERE
- b."projectId" = $ 1.id
+ b."projectId" = $1.id
AND b.deployment IS NOT NULL
AND (
(b.deployment :: jsonb ->> 'destination') IS NULL
@@ -104,7 +109,7 @@ WHERE
)
AND (
(b.deployment :: jsonb ->> 'projectDomain') = p.domain
- OR (b.deployment :: jsonb -> 'domains') @ > to_jsonb(array [p.domain])
+ OR (b.deployment :: jsonb -> 'domains') @> to_jsonb(array [p.domain])
)
ORDER BY
b."createdAt" DESC
@@ -113,4 +118,33 @@ LIMIT
$$ STABLE LANGUAGE sql;
-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.';
\ No newline at end of file
+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.';
+
+-- Add latestBuildVirtual function overload for DashboardProject view
+-- This is needed because PostgREST computed fields require a function
+-- that matches the source table/view type. DashboardProject is a view
+-- over Project, so we need this wrapper function that casts to Project type.
+CREATE
+OR REPLACE FUNCTION "latestBuildVirtual"("DashboardProject") RETURNS SETOF "latestBuildVirtual" ROWS 1 AS $$
+SELECT
+ *
+FROM
+ "latestBuildVirtual"(ROW($1.id, $1.title, $1.domain, $1."userId", $1."isDeleted", $1."createdAt", $1."previewImageAssetId", $1."marketplaceApprovalStatus", $1.tags)::"Project");
+
+$$ STABLE LANGUAGE sql;
+
+COMMENT ON FUNCTION "latestBuildVirtual"("DashboardProject") IS 'Wrapper function to make latestBuildVirtual work with DashboardProject view for PostgREST computed fields.';
+
+-- Grant execute permissions to all PostgREST roles
+-- Uses DO block to check if roles exist before granting (prevents errors if roles are missing)
+DO $$
+DECLARE
+ role_name TEXT;
+BEGIN
+ FOREACH role_name IN ARRAY ARRAY['anon', 'authenticated', 'service_role']
+ LOOP
+ IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = role_name) THEN
+ EXECUTE format('GRANT EXECUTE ON FUNCTION "latestBuildVirtual"("DashboardProject") TO %I', role_name);
+ END IF;
+ END LOOP;
+END $$;
\ No newline at end of file
diff --git a/packages/prisma-client/prisma/migrations/20251130131728_add_latest_build_virtual_for_dashboard_project/migration.sql b/packages/prisma-client/prisma/migrations/20251130131728_add_latest_build_virtual_for_dashboard_project/migration.sql
deleted file mode 100644
index 9f4e1f08f0..0000000000
--- a/packages/prisma-client/prisma/migrations/20251130131728_add_latest_build_virtual_for_dashboard_project/migration.sql
+++ /dev/null
@@ -1,28 +0,0 @@
--- Add latestBuildVirtual function overload for DashboardProject view
--- This is needed because PostgREST computed fields require a function
--- that matches the source table/view type. DashboardProject is a view
--- over Project, so we need this wrapper function.
-CREATE
-OR REPLACE FUNCTION "latestBuildVirtual"("DashboardProject") RETURNS SETOF "latestBuildVirtual" ROWS 1 AS $$
-SELECT
- *
-FROM
- "latestBuildVirtual"(
- (
- SELECT
- p
- FROM
- "Project" p
- WHERE
- p.id = $ 1.id
- )
- );
-
-$$ STABLE LANGUAGE sql;
-
-COMMENT ON FUNCTION "latestBuildVirtual"("DashboardProject") IS 'Wrapper function to make latestBuildVirtual work with DashboardProject view for PostgREST computed fields.';
-
--- Grant execute permissions to all PostgREST roles
-GRANT EXECUTE ON FUNCTION "latestBuildVirtual"("DashboardProject") TO anon,
-authenticated,
-service_role;
\ No newline at end of file