@@ -229,6 +229,43 @@ SELECT COUNT(*) > 0 AS should_repeat_migration FROM inserted;
229229-- Clean up temporary index
230230DROP INDEX IF EXISTS " temp_eco_branch_transfer_idx" ;
231231
232+ -- Safety net: find any env configs that still have branch-level fields (like apps.installed.authentication.enabled)
233+ -- and process them. This should only affect projects that were modified during the migration.
234+ -- SPLIT_STATEMENT_SENTINEL
235+ -- SINGLE_STATEMENT_SENTINEL
236+ WITH leftover_configs AS (
237+ SELECT eco." projectId" , eco." branchId" , eco." config" , eco." createdAt"
238+ FROM " EnvironmentConfigOverride" eco
239+ WHERE eco." config" ? ' apps.installed.authentication.enabled'
240+ ),
241+ upserted_branch AS (
242+ INSERT INTO " BranchConfigOverride" (" projectId" , " branchId" , " createdAt" , " updatedAt" , " config" , " source" )
243+ SELECT
244+ lc." projectId" ,
245+ lc." branchId" ,
246+ lc." createdAt" ,
247+ CURRENT_TIMESTAMP ,
248+ -- Merge with existing branch config if present
249+ COALESCE(
250+ (SELECT bco." config" FROM " BranchConfigOverride" bco
251+ WHERE bco." projectId" = lc." projectId" AND bco." branchId" = lc." branchId" ),
252+ ' {}' ::jsonb
253+ ) || temp_filter_branch_config(lc." config" ),
254+ ' {"type": "unlinked"}' ::jsonb
255+ FROM leftover_configs lc
256+ ON CONFLICT (" projectId" , " branchId" ) DO UPDATE
257+ SET " config" = " BranchConfigOverride" ." config" || temp_filter_branch_config(EXCLUDED." config" ),
258+ " updatedAt" = CURRENT_TIMESTAMP
259+ RETURNING " projectId" , " branchId"
260+ )
261+ UPDATE " EnvironmentConfigOverride" eco
262+ SET " config" = temp_filter_env_only_config(eco." config" ),
263+ " updatedAt" = CURRENT_TIMESTAMP
264+ FROM leftover_configs lc
265+ WHERE eco." projectId" = lc." projectId"
266+ AND eco." branchId" = lc." branchId" ;
267+ -- SPLIT_STATEMENT_SENTINEL
268+
232269-- Clean up temporary functions
233270DROP FUNCTION IF EXISTS temp_filter_env_only_config(JSONB);
234271DROP FUNCTION IF EXISTS temp_filter_branch_config(JSONB);
0 commit comments