#!/bin/bash # Exit on error set -e # Check if .env.local exists if [ ! -f .env.local ]; then echo "Error: .env.local file not found" exit 1 fi # Load environment variables export $(grep -v '^#' .env.local | xargs) # Check if required environment variables are set if [ -z "$NEXT_PUBLIC_SUPABASE_URL" ] || [ -z "$SUPABASE_SERVICE_ROLE_KEY" ]; then echo "Error: Missing required environment variables" echo "Make sure NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY are set in .env.local" exit 1 fi # Extract project reference from URL PROJECT_REF=$(echo $NEXT_PUBLIC_SUPABASE_URL | awk -F[/:] '{print $4}') if [ -z "$PROJECT_REF" ]; then echo "Error: Could not extract project reference from Supabase URL" exit 1 fi echo "Adding slug column to learning table..." # Step 1: Add the slug column if it doesn't exist QUERY="ALTER TABLE public.learning ADD COLUMN IF NOT EXISTS slug TEXT;" curl -X POST "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/rpc/exec_sql" \ -H "Content-Type: application/json" \ -H "apikey: $SUPABASE_SERVICE_ROLE_KEY" \ -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" \ -d "{\"sql\": \"$QUERY\"}" echo -e "\nGenerating slugs for existing records..." # Step 2: Generate slugs for existing records QUERY="UPDATE public.learning SET slug = LOWER(REGEXP_REPLACE(REGEXP_REPLACE(title, '[^a-zA-Z0-9\s]', '', 'g'), '\s+', '-', 'g')) || '-' || id WHERE slug IS NULL;" curl -X POST "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/rpc/exec_sql" \ -H "Content-Type: application/json" \ -H "apikey: $SUPABASE_SERVICE_ROLE_KEY" \ -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" \ -d "{\"sql\": \"$QUERY\"}" echo -e "\nAdding unique constraint..." # Step 3: Add a unique constraint if it doesn't exist QUERY="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 \$\$;" curl -X POST "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/rpc/exec_sql" \ -H "Content-Type: application/json" \ -H "apikey: $SUPABASE_SERVICE_ROLE_KEY" \ -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" \ -d "{\"sql\": \"$QUERY\"}" echo -e "\nAdding index..." # Step 4: Add an index if it doesn't exist QUERY="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 \$\$;" curl -X POST "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/rpc/exec_sql" \ -H "Content-Type: application/json" \ -H "apikey: $SUPABASE_SERVICE_ROLE_KEY" \ -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" \ -d "{\"sql\": \"$QUERY\"}" echo -e "\nMaking slug column NOT NULL..." # Step 5: Make slug NOT NULL QUERY="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 \$\$;" curl -X POST "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/rpc/exec_sql" \ -H "Content-Type: application/json" \ -H "apikey: $SUPABASE_SERVICE_ROLE_KEY" \ -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" \ -d "{\"sql\": \"$QUERY\"}" echo -e "\nVerifying migration..." # Verify the migration curl -X GET "$NEXT_PUBLIC_SUPABASE_URL/rest/v1/learning?select=id,title,slug&limit=1" \ -H "apikey: $SUPABASE_SERVICE_ROLE_KEY" \ -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" echo -e "\nMigration completed!"