diff --git a/tests/cleanup.sql b/tests/cleanup.sql index dd8d8c84d..3ae5a7838 100644 --- a/tests/cleanup.sql +++ b/tests/cleanup.sql @@ -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 $$; \ No newline at end of file +END $$; diff --git a/tests/package.json b/tests/package.json index 5040b9db8..875d453b5 100644 --- a/tests/package.json +++ b/tests/package.json @@ -14,6 +14,6 @@ "scripts": { "test": "bun test", "test:clean": "./test-with-cleanup.sh", - "cleanup": "./test-with-cleanup.sh --cleanup-only" + "cleanup": "./test-with-cleanup.sh --cleanup-only --org-pattern \"org%\"" } -} \ No newline at end of file +} diff --git a/tests/test-with-cleanup.sh b/tests/test-with-cleanup.sh index a3a656c07..cab3e403f 100755 --- a/tests/test-with-cleanup.sh +++ b/tests/test-with-cleanup.sh @@ -1,13 +1,37 @@ #!/bin/bash # Parse flags +ORG_PATTERN="org%" +WORKSPACE_NAME="" CLEANUP_ONLY=false -for arg in "$@"; do - case $arg in + +while [[ $# -gt 0 ]]; do + case $1 in --cleanup-only) CLEANUP_ONLY=true shift ;; + --org-pattern) + if [[ -z "$2" || "$2" == --* ]]; then + echo "Error: --org-pattern requires a value" + exit 1 + fi + ORG_PATTERN="$2" + shift 2 + ;; + --workspace) + if [[ -z "$2" || "$2" == --* ]]; then + echo "Error: --workspace requires a value" + exit 1 + fi + WORKSPACE_NAME="$2" + shift 2 + ;; + *) + echo "Unknown argument: $1" + echo "Usage: $0 [--cleanup-only] [--org-pattern pattern] [--workspace name]" + exit 1 + ;; esac done @@ -32,8 +56,18 @@ run_cleanup() { echo "" echo "========================================" echo "Running cleanup..." + if [ -n "$WORKSPACE_NAME" ]; then + echo "Mode: Specific Workspace ($WORKSPACE_NAME) in Orgs ($ORG_PATTERN)" + else + echo "Mode: Full Organization Cleanup ($ORG_PATTERN)" + fi echo "========================================" - psql "$DATABASE_URL" -f cleanup.sql + + psql "$DATABASE_URL" \ + -v org_pattern="$ORG_PATTERN" \ + -v workspace_name="$WORKSPACE_NAME" \ + -f cleanup.sql + CLEANUP_EXIT_CODE=$? if [ $CLEANUP_EXIT_CODE -eq 0 ]; then @@ -74,4 +108,4 @@ else echo "========================================" fi -exit $TEST_EXIT_CODE \ No newline at end of file +exit $TEST_EXIT_CODE