|
2 | 2 | -- No existing users with custom landing pages, so safe to drop columns. |
3 | 3 |
|
4 | 4 | -- Replace sections/settings columns with puck_data on landing_pages |
5 | | -ALTER TABLE landing_pages DROP COLUMN IF EXISTS sections; |
6 | | -ALTER TABLE landing_pages DROP COLUMN IF EXISTS settings; |
7 | | -ALTER TABLE landing_pages ADD COLUMN puck_data jsonb NOT NULL DEFAULT '{}'; |
| 5 | +DO $$ |
| 6 | +BEGIN |
| 7 | + IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'landing_pages') THEN |
| 8 | + ALTER TABLE landing_pages DROP COLUMN IF EXISTS sections; |
| 9 | + ALTER TABLE landing_pages DROP COLUMN IF EXISTS settings; |
| 10 | + IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'landing_pages' AND column_name = 'puck_data') THEN |
| 11 | + ALTER TABLE landing_pages ADD COLUMN puck_data jsonb NOT NULL DEFAULT '{}'; |
| 12 | + END IF; |
| 13 | + ELSE |
| 14 | + CREATE TABLE landing_pages ( |
| 15 | + page_id uuid PRIMARY KEY DEFAULT gen_random_uuid(), |
| 16 | + tenant_id uuid NOT NULL REFERENCES tenants(id) ON DELETE CASCADE, |
| 17 | + title text NOT NULL, |
| 18 | + slug text NOT NULL, |
| 19 | + is_published boolean NOT NULL DEFAULT false, |
| 20 | + puck_data jsonb NOT NULL DEFAULT '{}', |
| 21 | + created_at timestamptz NOT NULL DEFAULT now(), |
| 22 | + updated_at timestamptz NOT NULL DEFAULT now(), |
| 23 | + UNIQUE (tenant_id, slug) |
| 24 | + ); |
| 25 | + ALTER TABLE landing_pages ENABLE ROW LEVEL SECURITY; |
| 26 | + CREATE POLICY "Tenant members can view landing pages" ON landing_pages FOR SELECT USING (tenant_id = (SELECT current_setting('request.jwt.claims', true)::jsonb->>'tenant_id')::uuid); |
| 27 | + CREATE POLICY "Tenant admins can manage landing pages" ON landing_pages FOR ALL USING (tenant_id = (SELECT current_setting('request.jwt.claims', true)::jsonb->>'tenant_id')::uuid); |
| 28 | + END IF; |
| 29 | +END $$; |
8 | 30 |
|
9 | 31 | -- Replace sections/settings columns with puck_data on landing_page_templates |
10 | | -ALTER TABLE landing_page_templates DROP COLUMN IF EXISTS sections; |
11 | | -ALTER TABLE landing_page_templates DROP COLUMN IF EXISTS settings; |
12 | | -ALTER TABLE landing_page_templates ADD COLUMN puck_data jsonb NOT NULL DEFAULT '{}'; |
| 32 | +DO $$ |
| 33 | +BEGIN |
| 34 | + IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'landing_page_templates') THEN |
| 35 | + ALTER TABLE landing_page_templates DROP COLUMN IF EXISTS sections; |
| 36 | + ALTER TABLE landing_page_templates DROP COLUMN IF EXISTS settings; |
| 37 | + IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'landing_page_templates' AND column_name = 'puck_data') THEN |
| 38 | + ALTER TABLE landing_page_templates ADD COLUMN puck_data jsonb NOT NULL DEFAULT '{}'; |
| 39 | + END IF; |
| 40 | + ELSE |
| 41 | + CREATE TABLE landing_page_templates ( |
| 42 | + template_id uuid PRIMARY KEY DEFAULT gen_random_uuid(), |
| 43 | + name text NOT NULL, |
| 44 | + description text, |
| 45 | + puck_data jsonb NOT NULL DEFAULT '{}', |
| 46 | + created_at timestamptz NOT NULL DEFAULT now() |
| 47 | + ); |
| 48 | + END IF; |
| 49 | +END $$; |
13 | 50 |
|
14 | 51 | -- Create storage bucket for landing page assets (images, etc.) |
15 | 52 | INSERT INTO storage.buckets (id, name, public, file_size_limit, allowed_mime_types) |
|
0 commit comments