Branchwell

AI Resource Pro / website / scripts / add-learning-slug.sh

1#!/bin/bash
2
3# Exit on error
4set -e
5
6# Check if .env.local exists
7if [ ! -f .env.local ]; then
8 echo "Error: .env.local file not found"
9 exit 1
10fi
11
12# Load environment variables
13export $(grep -v '^#' .env.local | xargs)
14
15# Check if required environment variables are set
16if [ -z "$NEXT_PUBLIC_SUPABASE_URL" ] || [ -z "$SUPABASE_SERVICE_ROLE_KEY" ]; then
17 echo "Error: Missing required environment variables"
18 echo "Make sure NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY are set in .env.local"
19 exit 1
20fi
21
22# Extract project reference from URL
23PROJECT_REF=$(echo $NEXT_PUBLIC_SUPABASE_URL | awk -F[/:] '{print $4}')
24if [ -z "$PROJECT_REF" ]; then
25 echo "Error: Could not extract project reference from Supabase URL"
26 exit 1
27fi
28
29echo "Adding slug column to learning table..."
30
31# Step 1: Add the slug column if it doesn't exist
32QUERY="ALTER TABLE public.learning ADD COLUMN IF NOT EXISTS slug TEXT;"
33curl -X POST "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/rpc/exec_sql" \
34 -H "Content-Type: application/json" \
35 -H "apikey: $SUPABASE_SERVICE_ROLE_KEY" \
36 -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" \
37 -d "{\"sql\": \"$QUERY\"}"
38
39echo -e "\nGenerating slugs for existing records..."
40
41# Step 2: Generate slugs for existing records
42QUERY="UPDATE public.learning SET slug = LOWER(REGEXP_REPLACE(REGEXP_REPLACE(title, '[^a-zA-Z0-9\s]', '', 'g'), '\s+', '-', 'g')) || '-' || id WHERE slug IS NULL;"
43curl -X POST "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/rpc/exec_sql" \
44 -H "Content-Type: application/json" \
45 -H "apikey: $SUPABASE_SERVICE_ROLE_KEY" \
46 -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" \
47 -d "{\"sql\": \"$QUERY\"}"
48
49echo -e "\nAdding unique constraint..."
50
51# Step 3: Add a unique constraint if it doesn't exist
52QUERY="DO \$\$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'learning_slug_unique') THEN ALTER TABLE public.learning ADD CONSTRAINT learning_slug_unique UNIQUE (slug); END IF; END \$\$;"
53curl -X POST "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/rpc/exec_sql" \
54 -H "Content-Type: application/json" \
55 -H "apikey: $SUPABASE_SERVICE_ROLE_KEY" \
56 -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" \
57 -d "{\"sql\": \"$QUERY\"}"
58
59echo -e "\nAdding index..."
60
61# Step 4: Add an index if it doesn't exist
62QUERY="DO \$\$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_indexes WHERE indexname = 'idx_learning_slug') THEN CREATE INDEX idx_learning_slug ON public.learning (slug); END IF; END \$\$;"
63curl -X POST "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/rpc/exec_sql" \
64 -H "Content-Type: application/json" \
65 -H "apikey: $SUPABASE_SERVICE_ROLE_KEY" \
66 -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" \
67 -d "{\"sql\": \"$QUERY\"}"
68
69echo -e "\nMaking slug column NOT NULL..."
70
71# Step 5: Make slug NOT NULL
72QUERY="DO \$\$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'learning' AND column_name = 'slug' AND is_nullable = 'YES') THEN ALTER TABLE public.learning ALTER COLUMN slug SET NOT NULL; END IF; END \$\$;"
73curl -X POST "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/rpc/exec_sql" \
74 -H "Content-Type: application/json" \
75 -H "apikey: $SUPABASE_SERVICE_ROLE_KEY" \
76 -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" \
77 -d "{\"sql\": \"$QUERY\"}"
78
79echo -e "\nVerifying migration..."
80
81# Verify the migration
82curl -X GET "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/learning?select=id,title,slug&limit=1" \
83 -H "apikey: $SUPABASE_SERVICE_ROLE_KEY" \
84 -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY"
85
86echo -e "\nMigration completed!"