Skip to content

Commit 7275cb6

Browse files
committed
Add safety checks before dropping payments table and policies
1 parent e61ee6f commit 7275cb6

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed
Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
1-
-- Drop foreign key constraints first
2-
ALTER TABLE IF EXISTS payments
3-
DROP CONSTRAINT IF EXISTS payments_subscription_id_fkey;
1+
-- Check if the payments table exists before trying to drop it
2+
DO $$
3+
BEGIN
4+
-- Check if the table exists
5+
IF EXISTS (
6+
SELECT FROM pg_tables
7+
WHERE schemaname = 'public'
8+
AND tablename = 'payments'
9+
) THEN
10+
-- Drop foreign key constraints first
11+
ALTER TABLE IF EXISTS payments
12+
DROP CONSTRAINT IF EXISTS payments_subscription_id_fkey;
413

5-
-- Drop the payments table
6-
DROP TABLE IF EXISTS payments;
14+
-- Drop the payments table
15+
DROP TABLE IF EXISTS payments;
16+
END IF;
717

8-
-- Drop any related policies
9-
DROP POLICY IF EXISTS payments_select_policy ON payments;
10-
DROP POLICY IF EXISTS payments_insert_policy ON payments;
11-
DROP POLICY IF EXISTS payments_all_policy ON payments;
18+
-- Check if policies exist before dropping them
19+
IF EXISTS (
20+
SELECT FROM pg_policies
21+
WHERE schemaname = 'public'
22+
AND tablename = 'payments'
23+
AND policyname = 'payments_select_policy'
24+
) THEN
25+
DROP POLICY IF EXISTS payments_select_policy ON payments;
26+
END IF;
27+
28+
IF EXISTS (
29+
SELECT FROM pg_policies
30+
WHERE schemaname = 'public'
31+
AND tablename = 'payments'
32+
AND policyname = 'payments_insert_policy'
33+
) THEN
34+
DROP POLICY IF EXISTS payments_insert_policy ON payments;
35+
END IF;
36+
37+
IF EXISTS (
38+
SELECT FROM pg_policies
39+
WHERE schemaname = 'public'
40+
AND tablename = 'payments'
41+
AND policyname = 'payments_all_policy'
42+
) THEN
43+
DROP POLICY IF EXISTS payments_all_policy ON payments;
44+
END IF;
45+
END
46+
$$;
1247

1348
-- Comment explaining the migration
1449
COMMENT ON SCHEMA public IS 'Removed payments table as CryptAPI integration has been deprecated';

0 commit comments

Comments
 (0)