-
Notifications
You must be signed in to change notification settings - Fork 41
ci: support any org/workspace cleanup #855
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
knutties
wants to merge
2
commits into
main
Choose a base branch
from
org-cleanup-with-args
base: main
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -1,48 +1,104 @@ | ||
| -- Set session variables from psql variables before the DO block | ||
| -- This works because psql interpolates :'var' in top-level SQL, but not inside $$ strings | ||
| SELECT set_config('cleanup.org_pattern', :'org_pattern', false); | ||
| SELECT set_config('cleanup.workspace_name', :'workspace_name', false); | ||
|
|
||
| DO $$ | ||
| DECLARE | ||
| org_record RECORD; | ||
| workspace_record RECORD; | ||
| org_count INTEGER := 0; | ||
| workspace_count INTEGER := 0; | ||
| -- Retrieve variables from session config | ||
| v_org_pattern text := current_setting('cleanup.org_pattern'); | ||
| v_workspace_name text := current_setting('cleanup.workspace_name'); | ||
| BEGIN | ||
| RAISE NOTICE '========================================'; | ||
| RAISE NOTICE 'Starting cleanup of test organizations'; | ||
| RAISE NOTICE 'Pattern: ID starts with org%%'; | ||
| RAISE NOTICE '========================================'; | ||
| RAISE NOTICE 'Starting cleanup'; | ||
|
|
||
| FOR org_record IN | ||
| SELECT id, name FROM superposition.organisations | ||
| WHERE id LIKE 'org%' | ||
| LOOP | ||
| RAISE NOTICE ''; | ||
| RAISE NOTICE 'Organization: % (%)', org_record.id, org_record.name; | ||
| org_count := org_count + 1; | ||
|
|
||
| -- Drop workspace schemas | ||
| FOR workspace_record IN | ||
| SELECT workspace_schema_name, workspace_name | ||
| FROM superposition.workspaces | ||
| WHERE organisation_id = org_record.id | ||
| IF v_workspace_name IS NOT NULL AND v_workspace_name <> '' THEN | ||
| ----------------------------------------------------- | ||
| -- SPECIFIC WORKSPACE CLEANUP MODE | ||
| ----------------------------------------------------- | ||
| RAISE NOTICE 'Mode: DELETE SPECIFIC WORKSPACE'; | ||
| RAISE NOTICE 'Org Pattern: %', v_org_pattern; | ||
| RAISE NOTICE 'Workspace: %', v_workspace_name; | ||
| RAISE NOTICE '========================================'; | ||
|
|
||
| FOR org_record IN | ||
| SELECT id, name FROM superposition.organisations | ||
| WHERE id LIKE v_org_pattern | ||
| LOOP | ||
| RAISE NOTICE ' - Dropping schema: % (workspace: %)', | ||
| workspace_record.workspace_schema_name, workspace_record.workspace_name; | ||
| EXECUTE format('DROP SCHEMA IF EXISTS %I CASCADE', workspace_record.workspace_schema_name); | ||
| workspace_count := workspace_count + 1; | ||
| RAISE NOTICE 'Checking Org: % (%)', org_record.id, org_record.name; | ||
|
|
||
| -- Find specific workspaces | ||
| FOR workspace_record IN | ||
| SELECT workspace_schema_name, workspace_name | ||
| FROM superposition.workspaces | ||
| WHERE organisation_id = org_record.id | ||
| AND workspace_name = v_workspace_name | ||
| LOOP | ||
| RAISE NOTICE ' - Dropping schema: % (workspace: %)', | ||
| workspace_record.workspace_schema_name, workspace_record.workspace_name; | ||
|
|
||
| EXECUTE format('DROP SCHEMA IF EXISTS %I CASCADE', workspace_record.workspace_schema_name); | ||
|
|
||
| -- Delete workspace | ||
| DELETE FROM superposition.workspaces | ||
| WHERE organisation_id = org_record.id | ||
| AND workspace_name = workspace_record.workspace_name; | ||
|
|
||
| RAISE NOTICE ' - Deleted workspace record'; | ||
| workspace_count := workspace_count + 1; | ||
| END LOOP; | ||
| END LOOP; | ||
|
|
||
| ELSE | ||
| ----------------------------------------------------- | ||
| -- FULL ORG CLEANUP MODE (Original Behavior) | ||
| ----------------------------------------------------- | ||
| RAISE NOTICE 'Mode: FULL ORGANIZATION CLEANUP'; | ||
| RAISE NOTICE 'Org Pattern: %', v_org_pattern; | ||
| RAISE NOTICE '========================================'; | ||
|
|
||
| -- Delete workspaces | ||
| DELETE FROM superposition.workspaces WHERE organisation_id = org_record.id; | ||
| RAISE NOTICE ' - Deleted workspaces from superposition.workspaces'; | ||
| FOR org_record IN | ||
| SELECT id, name FROM superposition.organisations | ||
| WHERE id LIKE v_org_pattern | ||
| LOOP | ||
| RAISE NOTICE ''; | ||
| RAISE NOTICE 'Organization: % (%)', org_record.id, org_record.name; | ||
| org_count := org_count + 1; | ||
|
|
||
| -- Drop workspace schemas | ||
| FOR workspace_record IN | ||
| SELECT workspace_schema_name, workspace_name | ||
| FROM superposition.workspaces | ||
| WHERE organisation_id = org_record.id | ||
| LOOP | ||
| RAISE NOTICE ' - Dropping schema: % (workspace: %)', | ||
| workspace_record.workspace_schema_name, workspace_record.workspace_name; | ||
| EXECUTE format('DROP SCHEMA IF EXISTS %I CASCADE', workspace_record.workspace_schema_name); | ||
| workspace_count := workspace_count + 1; | ||
| END LOOP; | ||
|
|
||
| -- Delete workspaces | ||
| DELETE FROM superposition.workspaces WHERE organisation_id = org_record.id; | ||
| RAISE NOTICE ' - Deleted workspaces from superposition.workspaces'; | ||
|
|
||
| -- Delete organization | ||
| DELETE FROM superposition.organisations WHERE id = org_record.id; | ||
| RAISE NOTICE ' - Deleted organization from superposition.organisations'; | ||
| END LOOP; | ||
|
|
||
| -- Delete organization | ||
| DELETE FROM superposition.organisations WHERE id = org_record.id; | ||
| RAISE NOTICE ' - Deleted organization from superposition.organisations'; | ||
| END LOOP; | ||
| RAISE NOTICE ' Organizations deleted: %', org_count; | ||
| END IF; | ||
|
|
||
| RAISE NOTICE ''; | ||
| RAISE NOTICE '========================================'; | ||
| RAISE NOTICE 'Cleanup Summary:'; | ||
| RAISE NOTICE ' Organizations deleted: %', org_count; | ||
| RAISE NOTICE ' Workspace schemas dropped: %', workspace_count; | ||
| RAISE NOTICE ' Workspace schemas dropped/deleted: %', workspace_count; | ||
| IF org_count > 0 THEN | ||
| RAISE NOTICE ' Organizations deleted: %', org_count; | ||
| END IF; | ||
| RAISE NOTICE '========================================'; | ||
| END $$; | ||
| 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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
current_setting()will throw an error if the setting doesn't exist.When the session config isn't set (e.g., if
set_configwasn't called),current_setting()raises an exception. Consider usingcurrent_setting('cleanup.org_pattern', true)to return NULL on missing settings instead of erroring.🛡️ Proposed fix to handle missing settings gracefully
📝 Committable suggestion
🤖 Prompt for AI Agents