-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add ensure_flow_compiled function with dev/prod mode support #466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jumski
wants to merge
1
commit into
11-30-feat_core_add_flow_creation_and_deletion_functions
Choose a base branch
from
11-30-feat_core_add_ensure_flow_compiled_function
base: 11-30-feat_core_add_flow_creation_and_deletion_functions
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| -- Ensure a flow is compiled in the database | ||
| -- Handles both development (auto-recompile) and production (fail on mismatch) modes | ||
| -- Returns: { status: 'compiled' | 'verified' | 'recompiled' | 'mismatch', differences: text[] } | ||
| create or replace function pgflow.ensure_flow_compiled( | ||
| p_flow_slug text, | ||
| p_shape jsonb, | ||
| p_mode text default 'production' -- 'development' | 'production' | ||
| ) | ||
| returns jsonb | ||
| language plpgsql | ||
| volatile | ||
| set search_path to '' | ||
| as $$ | ||
| DECLARE | ||
| v_flow_exists boolean; | ||
| v_db_shape jsonb; | ||
| v_differences text[]; | ||
| BEGIN | ||
| -- 1. Check if flow exists | ||
| SELECT EXISTS(SELECT 1 FROM pgflow.flows AS flow WHERE flow.flow_slug = p_flow_slug) | ||
| INTO v_flow_exists; | ||
|
|
||
| -- 2. If flow missing: compile (both modes) | ||
| IF NOT v_flow_exists THEN | ||
| PERFORM pgflow._create_flow_from_shape(p_flow_slug, p_shape); | ||
| RETURN jsonb_build_object('status', 'compiled', 'differences', '[]'::jsonb); | ||
| END IF; | ||
|
|
||
| -- 3. Get current shape from DB | ||
| v_db_shape := pgflow._get_flow_shape(p_flow_slug); | ||
|
|
||
| -- 4. Compare shapes | ||
| v_differences := pgflow._compare_flow_shapes(p_shape, v_db_shape); | ||
graphite-app[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| -- 5. If shapes match: return verified | ||
| IF array_length(v_differences, 1) IS NULL THEN | ||
| RETURN jsonb_build_object('status', 'verified', 'differences', '[]'::jsonb); | ||
| END IF; | ||
|
|
||
| -- 6. Shapes differ - handle by mode | ||
| IF p_mode = 'development' THEN | ||
| -- Recompile in dev mode: full deletion + fresh compile | ||
| PERFORM pgflow.delete_flow_and_data(p_flow_slug); | ||
graphite-app[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| PERFORM pgflow._create_flow_from_shape(p_flow_slug, p_shape); | ||
| RETURN jsonb_build_object('status', 'recompiled', 'differences', to_jsonb(v_differences)); | ||
| ELSE | ||
| -- Fail in production mode | ||
| RETURN jsonb_build_object('status', 'mismatch', 'differences', to_jsonb(v_differences)); | ||
| END IF; | ||
| END; | ||
| $$; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
pkgs/core/supabase/migrations/20251130012803_pgflow_temp_ensure_flow_compiled.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| -- Create "ensure_flow_compiled" function | ||
| CREATE FUNCTION "pgflow"."ensure_flow_compiled" ("p_flow_slug" text, "p_shape" jsonb, "p_mode" text DEFAULT 'production') RETURNS jsonb LANGUAGE plpgsql SET "search_path" = '' AS $$ | ||
| DECLARE | ||
| v_flow_exists boolean; | ||
| v_db_shape jsonb; | ||
| v_differences text[]; | ||
| BEGIN | ||
| -- 1. Check if flow exists | ||
| SELECT EXISTS(SELECT 1 FROM pgflow.flows AS flow WHERE flow.flow_slug = p_flow_slug) | ||
| INTO v_flow_exists; | ||
|
|
||
| -- 2. If flow missing: compile (both modes) | ||
| IF NOT v_flow_exists THEN | ||
| PERFORM pgflow._create_flow_from_shape(p_flow_slug, p_shape); | ||
| RETURN jsonb_build_object('status', 'compiled', 'differences', '[]'::jsonb); | ||
| END IF; | ||
|
|
||
| -- 3. Get current shape from DB | ||
| v_db_shape := pgflow._get_flow_shape(p_flow_slug); | ||
|
|
||
| -- 4. Compare shapes | ||
| v_differences := pgflow._compare_flow_shapes(p_shape, v_db_shape); | ||
|
|
||
| -- 5. If shapes match: return verified | ||
| IF array_length(v_differences, 1) IS NULL THEN | ||
| RETURN jsonb_build_object('status', 'verified', 'differences', '[]'::jsonb); | ||
| END IF; | ||
|
|
||
| -- 6. Shapes differ - handle by mode | ||
| IF p_mode = 'development' THEN | ||
| -- Recompile in dev mode: full deletion + fresh compile | ||
| PERFORM pgflow.delete_flow_and_data(p_flow_slug); | ||
| PERFORM pgflow._create_flow_from_shape(p_flow_slug, p_shape); | ||
| RETURN jsonb_build_object('status', 'recompiled', 'differences', to_jsonb(v_differences)); | ||
| ELSE | ||
| -- Fail in production mode | ||
| RETURN jsonb_build_object('status', 'mismatch', 'differences', to_jsonb(v_differences)); | ||
| END IF; | ||
| END; | ||
| $$; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
pkgs/core/supabase/tests/ensure_flow_compiled/compiles_missing_flow.test.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| begin; | ||
| select plan(3); | ||
| select pgflow_tests.reset_db(); | ||
|
|
||
| -- Test: Missing flow should be compiled (default production mode) | ||
| select is( | ||
| ( | ||
| select result->>'status' | ||
| from pgflow.ensure_flow_compiled( | ||
| 'new_flow', | ||
| '{ | ||
| "steps": [ | ||
| {"slug": "first", "stepType": "single", "dependencies": []} | ||
| ] | ||
| }'::jsonb | ||
| ) as result | ||
| ), | ||
| 'compiled', | ||
| 'Should return compiled status for missing flow' | ||
| ); | ||
|
|
||
| -- Verify flow was actually created | ||
| select is( | ||
| (select count(*)::int from pgflow.flows where flow_slug = 'new_flow'), | ||
| 1, | ||
| 'Flow should be created in database' | ||
| ); | ||
|
|
||
| select is( | ||
| (select count(*)::int from pgflow.steps where flow_slug = 'new_flow'), | ||
| 1, | ||
| 'Step should be created in database' | ||
| ); | ||
|
|
||
| select finish(); | ||
| rollback; |
52 changes: 52 additions & 0 deletions
52
pkgs/core/supabase/tests/ensure_flow_compiled/fails_on_production_mismatch.test.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| begin; | ||
| select plan(3); | ||
| select pgflow_tests.reset_db(); | ||
|
|
||
| -- Setup: Create flow with different shape | ||
| select pgflow.create_flow('prod_flow'); | ||
| select pgflow.add_step('prod_flow', 'old_step'); | ||
|
|
||
| -- Test: Different shape in production mode should return mismatch | ||
| select is( | ||
| ( | ||
| select result->>'status' | ||
| from pgflow.ensure_flow_compiled( | ||
| 'prod_flow', | ||
| '{ | ||
| "steps": [ | ||
| {"slug": "new_step", "stepType": "single", "dependencies": []} | ||
| ] | ||
| }'::jsonb, | ||
| 'production' | ||
| ) as result | ||
| ), | ||
| 'mismatch', | ||
| 'Should return mismatch status in production mode' | ||
| ); | ||
|
|
||
| -- Verify differences are returned | ||
| select ok( | ||
| ( | ||
| select jsonb_array_length(result->'differences') > 0 | ||
| from pgflow.ensure_flow_compiled( | ||
| 'prod_flow', | ||
| '{ | ||
| "steps": [ | ||
| {"slug": "new_step", "stepType": "single", "dependencies": []} | ||
| ] | ||
| }'::jsonb, | ||
| 'production' | ||
| ) as result | ||
| ), | ||
| 'Should return differences for production mismatch' | ||
| ); | ||
|
|
||
| -- Verify database was NOT modified | ||
| select is( | ||
| (select step_slug from pgflow.steps where flow_slug = 'prod_flow'), | ||
| 'old_step', | ||
| 'Database should not be modified on production mismatch' | ||
| ); | ||
|
|
||
| select finish(); | ||
| rollback; |
42 changes: 42 additions & 0 deletions
42
pkgs/core/supabase/tests/ensure_flow_compiled/recompiles_in_development_mode.test.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| begin; | ||
| select plan(3); | ||
| select pgflow_tests.reset_db(); | ||
|
|
||
| -- Setup: Create flow with different shape | ||
| select pgflow.create_flow('dev_flow'); | ||
| select pgflow.add_step('dev_flow', 'old_step'); | ||
|
|
||
| -- Test: Different shape in development mode should recompile | ||
| select is( | ||
| ( | ||
| select result->>'status' | ||
| from pgflow.ensure_flow_compiled( | ||
| 'dev_flow', | ||
| '{ | ||
| "steps": [ | ||
| {"slug": "new_step", "stepType": "single", "dependencies": []} | ||
| ] | ||
| }'::jsonb, | ||
| 'development' | ||
| ) as result | ||
| ), | ||
| 'recompiled', | ||
| 'Should return recompiled status in development mode' | ||
| ); | ||
|
|
||
| -- Verify old step is gone | ||
| select is( | ||
| (select count(*)::int from pgflow.steps where flow_slug = 'dev_flow' and step_slug = 'old_step'), | ||
| 0, | ||
| 'Old step should be deleted' | ||
| ); | ||
|
|
||
| -- Verify new step exists | ||
| select is( | ||
| (select count(*)::int from pgflow.steps where flow_slug = 'dev_flow' and step_slug = 'new_step'), | ||
| 1, | ||
| 'New step should be created' | ||
| ); | ||
|
|
||
| select finish(); | ||
| rollback; |
47 changes: 47 additions & 0 deletions
47
pkgs/core/supabase/tests/ensure_flow_compiled/verifies_matching_shape.test.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| begin; | ||
| select plan(2); | ||
| select pgflow_tests.reset_db(); | ||
|
|
||
| -- Setup: Create flow first | ||
| select pgflow.create_flow('existing_flow'); | ||
| select pgflow.add_step('existing_flow', 'first'); | ||
| select pgflow.add_step('existing_flow', 'second', array['first']); | ||
|
|
||
| -- Test: Matching shape should return verified | ||
| select is( | ||
| ( | ||
| select result->>'status' | ||
| from pgflow.ensure_flow_compiled( | ||
| 'existing_flow', | ||
| '{ | ||
| "steps": [ | ||
| {"slug": "first", "stepType": "single", "dependencies": []}, | ||
| {"slug": "second", "stepType": "single", "dependencies": ["first"]} | ||
| ] | ||
| }'::jsonb | ||
| ) as result | ||
| ), | ||
| 'verified', | ||
| 'Should return verified status for matching shape' | ||
| ); | ||
|
|
||
| -- Verify differences array is empty | ||
| select is( | ||
| ( | ||
| select jsonb_array_length(result->'differences') | ||
| from pgflow.ensure_flow_compiled( | ||
| 'existing_flow', | ||
| '{ | ||
| "steps": [ | ||
| {"slug": "first", "stepType": "single", "dependencies": []}, | ||
| {"slug": "second", "stepType": "single", "dependencies": ["first"]} | ||
| ] | ||
| }'::jsonb | ||
| ) as result | ||
| ), | ||
| 0, | ||
| 'Differences should be empty for matching shape' | ||
| ); | ||
|
|
||
| select finish(); | ||
| rollback; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.