fix: migration 020 SQL syntax - correlated subquery issue
Deploy Development / deploy (push) Successful in 42s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 13s

Fixed Step 3 pipeline_configs migration:
- Simplified JSONB aggregation logic
- Properly scope pc alias in subqueries
- Use UNNEST with FROM clause for array expansion

Previous version had correlation issues with nested subqueries.
This commit is contained in:
Lars committed 2026-03-25 12:58:02 +01:00
1 parent 2e0838ca08
commit 95dcf080e5
2 files changed
+126 -52

No files matched your search

@@ -44,70 +44,70 @@ INSERT INTO ai_prompts (
category
)
SELECT
'pipeline_config_' || LOWER(REPLACE(name, ' ', '_')) || '_' || SUBSTRING(id::TEXT FROM 1 FOR 8),
name,
description,
'pipeline',
-- Build stages JSONB structure
'pipeline_config_' || LOWER(REPLACE(pc.name, ' ', '_')) || '_' || SUBSTRING(pc.id::TEXT FROM 1 FOR 8) as slug,
pc.name,
pc.description,
'pipeline' as type,
-- Build stages JSONB: combine stage1_prompts, stage2_prompt, stage3_prompt
(
SELECT jsonb_agg(
jsonb_build_object(
'stage', stage_num,
'prompts', stage_prompts
)
ORDER BY stage_num
)
-- Stage 1: Convert array to prompts
SELECT jsonb_agg(stage_obj ORDER BY stage_num)
FROM (
-- Stage 1: All parallel prompts
SELECT
1 as stage_num,
jsonb_agg(
jsonb_build_object(
'source', 'reference',
'slug', stage1_slug,
'output_key', REPLACE(stage1_slug, 'pipeline_', 'stage1_'),
'output_format', 'json'
)
) as stage_prompts
FROM UNNEST(pc.stage1_prompts) as stage1_slug
SELECT 1 as stage_num,
jsonb_build_object(
'stage', 1,
'prompts', (
SELECT jsonb_agg(
jsonb_build_object(
'source', 'reference',
'slug', s1.slug_val,
'output_key', REPLACE(s1.slug_val, 'pipeline_', 'stage1_'),
'output_format', 'json'
)
)
FROM UNNEST(pc.stage1_prompts) AS s1(slug_val)
)
) as stage_obj
WHERE array_length(pc.stage1_prompts, 1) > 0
UNION ALL
-- Stage 2: Synthesis prompt
SELECT
2 as stage_num,
jsonb_build_array(
jsonb_build_object(
'source', 'reference',
'slug', pc.stage2_prompt,
'output_key', 'synthesis',
'output_format', 'text'
)
) as stage_prompts
SELECT 2 as stage_num,
jsonb_build_object(
'stage', 2,
'prompts', jsonb_build_array(
jsonb_build_object(
'source', 'reference',
'slug', pc.stage2_prompt,
'output_key', 'synthesis',
'output_format', 'text'
)
)
) as stage_obj
WHERE pc.stage2_prompt IS NOT NULL
UNION ALL
-- Stage 3: Optional goals/analysis prompt
SELECT
3 as stage_num,
jsonb_build_array(
jsonb_build_object(
'source', 'reference',
'slug', pc.stage3_prompt,
'output_key', 'goals',
'output_format', 'text'
)
) as stage_prompts
SELECT 3 as stage_num,
jsonb_build_object(
'stage', 3,
'prompts', jsonb_build_array(
jsonb_build_object(
'source', 'reference',
'slug', pc.stage3_prompt,
'output_key', 'goals',
'output_format', 'text'
)
)
) as stage_obj
WHERE pc.stage3_prompt IS NOT NULL
) stages_union
) all_stages
) as stages,
'text',
'text' as output_format,
pc.active,
pc.is_default,
'pipeline'
FROM pipeline_configs pc
WHERE pc.id IS NOT NULL;
pc.is_default as is_system_default,
'pipeline' as category
FROM pipeline_configs pc;
-- Step 4: Add indices for performance
CREATE INDEX IF NOT EXISTS idx_ai_prompts_type ON ai_prompts(type);