From 70ead41121918577c4c3fb5cb87788bc9588cef3 Mon Sep 17 00:00:00 2001 From: Konstantin Wohlwend Date: Fri, 23 Jan 2026 16:47:19 -0800 Subject: [PATCH] Clean up branch config overrides after completion --- .../db-migration-backwards-compatibility.yaml | 5 +-- .../migration.sql | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) rename apps/backend/prisma/migrations/{20260201230001_env_to_branch_config => 20260201230002_env_to_branch_config}/migration.sql (84%) diff --git a/.github/workflows/db-migration-backwards-compatibility.yaml b/.github/workflows/db-migration-backwards-compatibility.yaml index 63ecfa422..882755384 100644 --- a/.github/workflows/db-migration-backwards-compatibility.yaml +++ b/.github/workflows/db-migration-backwards-compatibility.yaml @@ -2,14 +2,13 @@ name: DB migrations are backwards-compatible with main branch on: push: - branches: + branches-ignore: - main - - dev pull_request: concurrency: group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/dev' }} + cancel-in-progress: ${{ github.ref != 'refs/heads/dev' }} jobs: check-migrations-changed: diff --git a/apps/backend/prisma/migrations/20260201230001_env_to_branch_config/migration.sql b/apps/backend/prisma/migrations/20260201230002_env_to_branch_config/migration.sql similarity index 84% rename from apps/backend/prisma/migrations/20260201230001_env_to_branch_config/migration.sql rename to apps/backend/prisma/migrations/20260201230002_env_to_branch_config/migration.sql index 0e6b6f826..a3ad60aae 100644 --- a/apps/backend/prisma/migrations/20260201230001_env_to_branch_config/migration.sql +++ b/apps/backend/prisma/migrations/20260201230002_env_to_branch_config/migration.sql @@ -229,6 +229,43 @@ SELECT COUNT(*) > 0 AS should_repeat_migration FROM inserted; -- Clean up temporary index DROP INDEX IF EXISTS "temp_eco_branch_transfer_idx"; +-- Safety net: find any env configs that still have branch-level fields (like apps.installed.authentication.enabled) +-- and process them. This should only affect projects that were modified during the migration. +-- SPLIT_STATEMENT_SENTINEL +-- SINGLE_STATEMENT_SENTINEL +WITH leftover_configs AS ( + SELECT eco."projectId", eco."branchId", eco."config", eco."createdAt" + FROM "EnvironmentConfigOverride" eco + WHERE eco."config" ? 'apps.installed.authentication.enabled' +), +upserted_branch AS ( + INSERT INTO "BranchConfigOverride" ("projectId", "branchId", "createdAt", "updatedAt", "config", "source") + SELECT + lc."projectId", + lc."branchId", + lc."createdAt", + CURRENT_TIMESTAMP, + -- Merge with existing branch config if present + COALESCE( + (SELECT bco."config" FROM "BranchConfigOverride" bco + WHERE bco."projectId" = lc."projectId" AND bco."branchId" = lc."branchId"), + '{}'::jsonb + ) || temp_filter_branch_config(lc."config"), + '{"type": "unlinked"}'::jsonb + FROM leftover_configs lc + ON CONFLICT ("projectId", "branchId") DO UPDATE + SET "config" = "BranchConfigOverride"."config" || temp_filter_branch_config(EXCLUDED."config"), + "updatedAt" = CURRENT_TIMESTAMP + RETURNING "projectId", "branchId" +) +UPDATE "EnvironmentConfigOverride" eco +SET "config" = temp_filter_env_only_config(eco."config"), + "updatedAt" = CURRENT_TIMESTAMP +FROM leftover_configs lc +WHERE eco."projectId" = lc."projectId" + AND eco."branchId" = lc."branchId"; +-- SPLIT_STATEMENT_SENTINEL + -- Clean up temporary functions DROP FUNCTION IF EXISTS temp_filter_env_only_config(JSONB); DROP FUNCTION IF EXISTS temp_filter_branch_config(JSONB);