Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 86 additions & 30 deletions tests/cleanup.sql
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');
Comment on lines +12 to +14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

current_setting() will throw an error if the setting doesn't exist.

When the session config isn't set (e.g., if set_config wasn't called), current_setting() raises an exception. Consider using current_setting('cleanup.org_pattern', true) to return NULL on missing settings instead of erroring.

🛡️ Proposed fix to handle missing settings gracefully
     -- Retrieve variables from session config
-    v_org_pattern text := current_setting('cleanup.org_pattern');
-    v_workspace_name text := current_setting('cleanup.workspace_name');
+    v_org_pattern text := current_setting('cleanup.org_pattern', true);
+    v_workspace_name text := current_setting('cleanup.workspace_name', true);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
-- Retrieve variables from session config
v_org_pattern text := current_setting('cleanup.org_pattern');
v_workspace_name text := current_setting('cleanup.workspace_name');
-- Retrieve variables from session config
v_org_pattern text := current_setting('cleanup.org_pattern', true);
v_workspace_name text := current_setting('cleanup.workspace_name', true);
🤖 Prompt for AI Agents
In `@tests/cleanup.sql` around lines 12 - 14, Change the current_setting calls
used to initialize v_org_pattern and v_workspace_name so they don't raise when
the settings are absent: replace current_setting('cleanup.org_pattern') and
current_setting('cleanup.workspace_name') with
current_setting('cleanup.org_pattern', true) and
current_setting('cleanup.workspace_name', true') (or wrap them in COALESCE(...,
'<default>') if you need a fallback), update the v_org_pattern and
v_workspace_name initializations accordingly, and ensure any later logic that
assumes non-NULL values checks for NULL or uses the chosen defaults.

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 $$;
4 changes: 2 additions & 2 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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%\""
}
}
}
42 changes: 38 additions & 4 deletions tests/test-with-cleanup.sh
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down Expand Up @@ -74,4 +108,4 @@ else
echo "========================================"
fi

exit $TEST_EXIT_CODE
exit $TEST_EXIT_CODE
Loading