AI Resource Pro / website / scripts / get-fresh-supabase-schema.js
| 1 | // Script to fetch the schema for blog_posts table |
| 2 | import { config } from 'dotenv'; |
| 3 | import { createClient } from '@supabase/supabase-js'; |
| 4 | import fs from 'fs'; |
| 5 | |
| 6 | // Load environment variables |
| 7 | config({ path: '.env.local' }); |
| 8 | |
| 9 | // Create Supabase client |
| 10 | const supabase = createClient( |
| 11 | process.env.NEXT_PUBLIC_SUPABASE_URL, |
| 12 | process.env.SUPABASE_SERVICE_ROLE_KEY |
| 13 | ); |
| 14 | |
| 15 | async function getTableSchema() { |
| 16 | try { |
| 17 | console.log('Fetching blog_posts table schema...'); |
| 18 | |
| 19 | // Get a sample record to understand the structure |
| 20 | const { data: sampleData, error: sampleError } = await supabase |
| 21 | .from('blog_posts') |
| 22 | .select('*') |
| 23 | .limit(1); |
| 24 | |
| 25 | if (sampleError) { |
| 26 | console.error('Error fetching sample data:', sampleError); |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | if (sampleData && sampleData.length > 0) { |
| 31 | const columns = Object.keys(sampleData[0]); |
| 32 | console.log('Blog posts table columns:', columns); |
| 33 | |
| 34 | // Create a SQL schema definition based on the actual table structure |
| 35 | let schemaSQL = ` |
| 36 | -- Blog Posts Table |
| 37 | CREATE TABLE IF NOT EXISTS public.blog_posts ( |
| 38 | `; |
| 39 | |
| 40 | // Add columns based on actual data |
| 41 | for (const column of columns) { |
| 42 | let dataType = 'TEXT'; |
| 43 | let constraints = ''; |
| 44 | |
| 45 | // Determine data type based on sample data |
| 46 | const value = sampleData[0][column]; |
| 47 | if (column === 'id') { |
| 48 | dataType = 'UUID'; |
| 49 | constraints = 'PRIMARY KEY DEFAULT gen_random_uuid()'; |
| 50 | } else if (column === 'slug') { |
| 51 | dataType = 'TEXT'; |
| 52 | constraints = 'UNIQUE NOT NULL'; |
| 53 | } else if (column === 'title' || column === 'content') { |
| 54 | dataType = 'TEXT'; |
| 55 | constraints = 'NOT NULL'; |
| 56 | } else if (column === 'created_at' || column === 'updated_at') { |
| 57 | dataType = 'TIMESTAMPTZ'; |
| 58 | constraints = 'DEFAULT now()'; |
| 59 | } else if (column === 'published') { |
| 60 | dataType = 'BOOLEAN'; |
| 61 | constraints = 'DEFAULT false'; |
| 62 | } else if (column === 'primary_category_id') { |
| 63 | dataType = 'BIGINT'; |
| 64 | constraints = 'REFERENCES public.categories(id)'; |
| 65 | } |
| 66 | |
| 67 | schemaSQL += ` ${column} ${dataType} ${constraints},\n`; |
| 68 | } |
| 69 | |
| 70 | // Remove trailing comma and close the schema |
| 71 | schemaSQL = schemaSQL.slice(0, -2) + '\n);\n'; |
| 72 | |
| 73 | // Add additional schema for blog_posts_categories junction table |
| 74 | schemaSQL += ` |
| 75 | -- Blog Posts Categories Junction Table |
| 76 | CREATE TABLE IF NOT EXISTS public.blog_posts_categories ( |
| 77 | id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, |
| 78 | blog_post_id UUID NOT NULL REFERENCES public.blog_posts(id) ON DELETE CASCADE, |
| 79 | category_id BIGINT NOT NULL REFERENCES public.categories(id) ON DELETE CASCADE, |
| 80 | is_primary BOOLEAN DEFAULT false, |
| 81 | created_at TIMESTAMPTZ DEFAULT now(), |
| 82 | UNIQUE(blog_post_id, category_id) |
| 83 | ); |
| 84 | `; |
| 85 | |
| 86 | // Write the schema to a file |
| 87 | fs.writeFileSync('fresh-blog-schema.sql', schemaSQL); |
| 88 | console.log('Schema written to fresh-blog-schema.sql'); |
| 89 | |
| 90 | // Also write a JSON representation for reference |
| 91 | const schemaJSON = { |
| 92 | tableName: 'blog_posts', |
| 93 | columns: columns.map(col => { |
| 94 | return { |
| 95 | name: col, |
| 96 | example: sampleData[0][col] |
| 97 | }; |
| 98 | }) |
| 99 | }; |
| 100 | |
| 101 | fs.writeFileSync('blog-schema.json', JSON.stringify(schemaJSON, null, 2)); |
| 102 | console.log('Schema also written to blog-schema.json'); |
| 103 | } else { |
| 104 | console.log('No blog posts found in the database.'); |
| 105 | } |
| 106 | } catch (error) { |
| 107 | console.error('Error:', error); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | getTableSchema(); |