Skip to content

Commit 16855f8

Browse files
committed
Refactor Suprabase migrations into format the CLI will understand when used on GitHub Actions Test workflow
1 parent c263f13 commit 16855f8

5 files changed

Lines changed: 119 additions & 75 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"containers:supabase:stop": "FORCE_COLOR=1 npx supabase stop --workdir suprabase",
5353
"containers:supabase:status": "FORCE_COLOR=1 npx supabase status --workdir suprabase",
5454
"containers:supabase:logs": "FORCE_COLOR=1 bash test/containers/supabase/logs.sh",
55-
"containers:supabase:db-push": "FORCE_COLOR=1 dotenv -e test/containers/.env -- bash -c 'npx supabase db push --local --workdir suprabase'",
55+
"containers:supabase:db-push": "FORCE_COLOR=1 dotenv -e test/containers/.env -- bash -c 'npx supabase db push --local --workdir suprabase --yes'",
5656
"supabase:db:push": "FORCE_COLOR=1 npx supabase db push --workdir suprabase",
5757
"test": "npm run test:unit && npm run test:e2e",
5858
"test:coverage": "FORCE_COLOR=1 npx vitest run --coverage",

suprabase/migrations/001_create_consent_records.sql

Lines changed: 0 additions & 30 deletions
This file was deleted.

suprabase/migrations/002_create_newsletter_confirmations.sql

Lines changed: 0 additions & 22 deletions
This file was deleted.

suprabase/migrations/003_create_dsar_requests.sql

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
-- Ensure required extensions are available for UUID generation
2+
CREATE EXTENSION IF NOT EXISTS "pgcrypto" WITH SCHEMA public;
3+
4+
-- Consent records ----------------------------------------------------------------
5+
CREATE TABLE IF NOT EXISTS public.consent_records (
6+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
7+
data_subject_id UUID NOT NULL,
8+
email TEXT,
9+
purposes TEXT[] NOT NULL,
10+
"timestamp" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
11+
source TEXT NOT NULL,
12+
user_agent TEXT NOT NULL,
13+
ip_address INET,
14+
privacy_policy_version TEXT NOT NULL,
15+
consent_text TEXT,
16+
verified BOOLEAN DEFAULT FALSE,
17+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
18+
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
19+
);
20+
21+
CREATE INDEX IF NOT EXISTS idx_consent_data_subject_id ON public.consent_records(data_subject_id);
22+
CREATE INDEX IF NOT EXISTS idx_consent_email ON public.consent_records(email) WHERE email IS NOT NULL;
23+
CREATE INDEX IF NOT EXISTS idx_consent_timestamp ON public.consent_records("timestamp" DESC);
24+
25+
ALTER TABLE public.consent_records ENABLE ROW LEVEL SECURITY;
26+
27+
DO $$
28+
BEGIN
29+
IF NOT EXISTS (
30+
SELECT 1
31+
FROM pg_policies
32+
WHERE schemaname = 'public'
33+
AND tablename = 'consent_records'
34+
AND policyname = 'service_role_all_access'
35+
) THEN
36+
CREATE POLICY "service_role_all_access"
37+
ON public.consent_records
38+
FOR ALL
39+
TO service_role
40+
USING (true)
41+
WITH CHECK (true);
42+
END IF;
43+
END
44+
$$;
45+
46+
-- Newsletter confirmations --------------------------------------------------------
47+
CREATE TABLE IF NOT EXISTS public.newsletter_confirmations (
48+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
49+
token TEXT UNIQUE NOT NULL,
50+
email TEXT NOT NULL,
51+
data_subject_id UUID NOT NULL,
52+
expires_at TIMESTAMPTZ NOT NULL,
53+
confirmed_at TIMESTAMPTZ,
54+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
55+
);
56+
57+
CREATE INDEX IF NOT EXISTS idx_newsletter_token ON public.newsletter_confirmations(token);
58+
CREATE INDEX IF NOT EXISTS idx_newsletter_expiry
59+
ON public.newsletter_confirmations(expires_at)
60+
WHERE confirmed_at IS NULL;
61+
62+
ALTER TABLE public.newsletter_confirmations ENABLE ROW LEVEL SECURITY;
63+
64+
DO $$
65+
BEGIN
66+
IF NOT EXISTS (
67+
SELECT 1
68+
FROM pg_policies
69+
WHERE schemaname = 'public'
70+
AND tablename = 'newsletter_confirmations'
71+
AND policyname = 'service_role_all_access'
72+
) THEN
73+
CREATE POLICY "service_role_all_access"
74+
ON public.newsletter_confirmations
75+
FOR ALL
76+
TO service_role
77+
USING (true)
78+
WITH CHECK (true);
79+
END IF;
80+
END
81+
$$;
82+
83+
-- DSAR requests ------------------------------------------------------------------
84+
CREATE TABLE IF NOT EXISTS public.dsar_requests (
85+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
86+
token TEXT UNIQUE NOT NULL,
87+
email TEXT NOT NULL,
88+
request_type TEXT NOT NULL CHECK (request_type IN ('ACCESS', 'DELETE')),
89+
expires_at TIMESTAMPTZ NOT NULL,
90+
fulfilled_at TIMESTAMPTZ,
91+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
92+
);
93+
94+
CREATE INDEX IF NOT EXISTS idx_dsar_token ON public.dsar_requests(token);
95+
CREATE INDEX IF NOT EXISTS idx_dsar_expiry
96+
ON public.dsar_requests(expires_at)
97+
WHERE fulfilled_at IS NULL;
98+
99+
ALTER TABLE public.dsar_requests ENABLE ROW LEVEL SECURITY;
100+
101+
DO $$
102+
BEGIN
103+
IF NOT EXISTS (
104+
SELECT 1
105+
FROM pg_policies
106+
WHERE schemaname = 'public'
107+
AND tablename = 'dsar_requests'
108+
AND policyname = 'service_role_all_access'
109+
) THEN
110+
CREATE POLICY "service_role_all_access"
111+
ON public.dsar_requests
112+
FOR ALL
113+
TO service_role
114+
USING (true)
115+
WITH CHECK (true);
116+
END IF;
117+
END
118+
$$;

0 commit comments

Comments
 (0)