AI Resource Pro / website / scripts / add-learning-slug.ts
| 1 | import { createClient } from '@supabase/supabase-js'; |
| 2 | import 'dotenv/config'; |
| 3 | |
| 4 | // Type for learning resource |
| 5 | interface LearningResource { |
| 6 | id: number; |
| 7 | title: string; |
| 8 | slug?: string; |
| 9 | } |
| 10 | |
| 11 | // Function to generate slug from title |
| 12 | function generateSlug(title: string, id: number): string { |
| 13 | const baseSlug = title |
| 14 | .toLowerCase() |
| 15 | .replace(/[^a-z0-9\s]/g, '') // Remove non-alphanumeric characters |
| 16 | .replace(/\s+/g, '-') // Replace spaces with hyphens |
| 17 | .replace(/-+/g, '-') // Remove consecutive hyphens |
| 18 | .replace(/^-|-$/g, ''); // Remove leading/trailing hyphens |
| 19 | |
| 20 | return `${baseSlug}-${id}`; |
| 21 | } |
| 22 | |
| 23 | // Main function |
| 24 | async function addSlugToLearningResources() { |
| 25 | // Validate environment variables |
| 26 | if (!process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.SUPABASE_SERVICE_ROLE_KEY) { |
| 27 | console.error('Error: Missing required environment variables'); |
| 28 | console.error('Make sure NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY are set in .env.local'); |
| 29 | process.exit(1); |
| 30 | } |
| 31 | |
| 32 | // Create Supabase client |
| 33 | const supabase = createClient( |
| 34 | process.env.NEXT_PUBLIC_SUPABASE_URL, |
| 35 | process.env.SUPABASE_SERVICE_ROLE_KEY, |
| 36 | { |
| 37 | auth: { |
| 38 | autoRefreshToken: false, |
| 39 | persistSession: false |
| 40 | } |
| 41 | } |
| 42 | ); |
| 43 | |
| 44 | try { |
| 45 | console.log('Step 1: Checking if slug column exists...'); |
| 46 | |
| 47 | // Use RPC to check if column exists |
| 48 | const { data: columnExists, error: columnCheckError } = await supabase.rpc( |
| 49 | 'column_exists', |
| 50 | { p_table: 'learning', p_column: 'slug' } |
| 51 | ); |
| 52 | |
| 53 | if (columnCheckError) { |
| 54 | console.log('Could not check if column exists via RPC, trying direct SQL...'); |
| 55 | |
| 56 | // If RPC fails, try adding the column directly |
| 57 | console.log('Step 2: Adding slug column...'); |
| 58 | try { |
| 59 | await supabase.rpc('exec_sql', { |
| 60 | sql: 'ALTER TABLE public.learning ADD COLUMN IF NOT EXISTS slug TEXT;' |
| 61 | }); |
| 62 | console.log('Added slug column successfully'); |
| 63 | } catch (addColumnError) { |
| 64 | console.error('Error adding slug column:', addColumnError); |
| 65 | process.exit(1); |
| 66 | } |
| 67 | } else if (!columnExists) { |
| 68 | console.log('Slug column does not exist, adding it...'); |
| 69 | await supabase.rpc('exec_sql', { |
| 70 | sql: 'ALTER TABLE public.learning ADD COLUMN slug TEXT;' |
| 71 | }); |
| 72 | console.log('Added slug column successfully'); |
| 73 | } else { |
| 74 | console.log('Slug column already exists'); |
| 75 | } |
| 76 | |
| 77 | // Fetch all learning resources without slugs |
| 78 | console.log('Step 3: Fetching learning resources without slugs...'); |
| 79 | const { data: resources, error: fetchError } = await supabase |
| 80 | .from('learning') |
| 81 | .select('id, title, slug') |
| 82 | .is('slug', null); |
| 83 | |
| 84 | if (fetchError) { |
| 85 | throw fetchError; |
| 86 | } |
| 87 | |
| 88 | console.log(`Found ${resources?.length || 0} resources without slugs`); |
| 89 | |
| 90 | // Update each resource with a generated slug |
| 91 | if (resources && resources.length > 0) { |
| 92 | console.log('Step 4: Generating and updating slugs...'); |
| 93 | |
| 94 | for (const resource of resources) { |
| 95 | const slug = generateSlug(resource.title, resource.id); |
| 96 | console.log(`Updating resource ${resource.id}: "${resource.title}" -> "${slug}"`); |
| 97 | |
| 98 | const { error: updateError } = await supabase |
| 99 | .from('learning') |
| 100 | .update({ slug }) |
| 101 | .eq('id', resource.id); |
| 102 | |
| 103 | if (updateError) { |
| 104 | console.error(`Error updating slug for resource ${resource.id}:`, updateError); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | console.log('All resources updated with slugs'); |
| 109 | } |
| 110 | |
| 111 | // Add constraint and index if they don't exist |
| 112 | console.log('Step 5: Adding constraint and index...'); |
| 113 | |
| 114 | try { |
| 115 | // Try to add a unique constraint |
| 116 | await supabase.rpc('exec_sql', { |
| 117 | sql: ` |
| 118 | DO $$ |
| 119 | BEGIN |
| 120 | IF NOT EXISTS ( |
| 121 | SELECT 1 |
| 122 | FROM pg_constraint |
| 123 | WHERE conname = 'learning_slug_unique' |
| 124 | ) THEN |
| 125 | ALTER TABLE public.learning ADD CONSTRAINT learning_slug_unique UNIQUE (slug); |
| 126 | END IF; |
| 127 | END $$; |
| 128 | ` |
| 129 | }); |
| 130 | console.log('Added unique constraint (if it didn\'t exist)'); |
| 131 | |
| 132 | // Try to add an index |
| 133 | await supabase.rpc('exec_sql', { |
| 134 | sql: ` |
| 135 | DO $$ |
| 136 | BEGIN |
| 137 | IF NOT EXISTS ( |
| 138 | SELECT 1 |
| 139 | FROM pg_indexes |
| 140 | WHERE indexname = 'idx_learning_slug' |
| 141 | ) THEN |
| 142 | CREATE INDEX idx_learning_slug ON public.learning (slug); |
| 143 | END IF; |
| 144 | END $$; |
| 145 | ` |
| 146 | }); |
| 147 | console.log('Added index (if it didn\'t exist)'); |
| 148 | |
| 149 | // Make the column not null |
| 150 | await supabase.rpc('exec_sql', { |
| 151 | sql: ` |
| 152 | DO $$ |
| 153 | BEGIN |
| 154 | IF EXISTS ( |
| 155 | SELECT 1 |
| 156 | FROM information_schema.columns |
| 157 | WHERE table_schema = 'public' |
| 158 | AND table_name = 'learning' |
| 159 | AND column_name = 'slug' |
| 160 | AND is_nullable = 'YES' |
| 161 | ) THEN |
| 162 | ALTER TABLE public.learning ALTER COLUMN slug SET NOT NULL; |
| 163 | END IF; |
| 164 | END $$; |
| 165 | ` |
| 166 | }); |
| 167 | console.log('Made slug column NOT NULL (if it was nullable)'); |
| 168 | |
| 169 | } catch (constraintError) { |
| 170 | console.error('Error adding constraint or index:', constraintError); |
| 171 | // Continue execution despite error |
| 172 | } |
| 173 | |
| 174 | console.log('Migration completed successfully!'); |
| 175 | |
| 176 | } catch (error) { |
| 177 | console.error('Error in migration:', error); |
| 178 | process.exit(1); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Run the migration |
| 183 | addSlugToLearningResources(); |