// Script to fetch the schema for blog_posts table import { config } from 'dotenv'; import { createClient } from '@supabase/supabase-js'; import fs from 'fs'; // Load environment variables config({ path: '.env.local' }); // Create Supabase client const supabase = createClient( process.env.NEXT_PUBLIC_SUPABASE_URL, process.env.SUPABASE_SERVICE_ROLE_KEY ); async function getTableSchema() { try { console.log('Fetching blog_posts table schema...'); // Get a sample record to understand the structure const { data: sampleData, error: sampleError } = await supabase .from('blog_posts') .select('*') .limit(1); if (sampleError) { console.error('Error fetching sample data:', sampleError); return; } if (sampleData && sampleData.length > 0) { const columns = Object.keys(sampleData[0]); console.log('Blog posts table columns:', columns); // Create a SQL schema definition based on the actual table structure let schemaSQL = ` -- Blog Posts Table CREATE TABLE IF NOT EXISTS public.blog_posts ( `; // Add columns based on actual data for (const column of columns) { let dataType = 'TEXT'; let constraints = ''; // Determine data type based on sample data const value = sampleData[0][column]; if (column === 'id') { dataType = 'UUID'; constraints = 'PRIMARY KEY DEFAULT gen_random_uuid()'; } else if (column === 'slug') { dataType = 'TEXT'; constraints = 'UNIQUE NOT NULL'; } else if (column === 'title' || column === 'content') { dataType = 'TEXT'; constraints = 'NOT NULL'; } else if (column === 'created_at' || column === 'updated_at') { dataType = 'TIMESTAMPTZ'; constraints = 'DEFAULT now()'; } else if (column === 'published') { dataType = 'BOOLEAN'; constraints = 'DEFAULT false'; } else if (column === 'primary_category_id') { dataType = 'BIGINT'; constraints = 'REFERENCES public.categories(id)'; } schemaSQL += ` ${column} ${dataType} ${constraints},\n`; } // Remove trailing comma and close the schema schemaSQL = schemaSQL.slice(0, -2) + '\n);\n'; // Add additional schema for blog_posts_categories junction table schemaSQL += ` -- Blog Posts Categories Junction Table CREATE TABLE IF NOT EXISTS public.blog_posts_categories ( id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, blog_post_id UUID NOT NULL REFERENCES public.blog_posts(id) ON DELETE CASCADE, category_id BIGINT NOT NULL REFERENCES public.categories(id) ON DELETE CASCADE, is_primary BOOLEAN DEFAULT false, created_at TIMESTAMPTZ DEFAULT now(), UNIQUE(blog_post_id, category_id) ); `; // Write the schema to a file fs.writeFileSync('fresh-blog-schema.sql', schemaSQL); console.log('Schema written to fresh-blog-schema.sql'); // Also write a JSON representation for reference const schemaJSON = { tableName: 'blog_posts', columns: columns.map(col => { return { name: col, example: sampleData[0][col] }; }) }; fs.writeFileSync('blog-schema.json', JSON.stringify(schemaJSON, null, 2)); console.log('Schema also written to blog-schema.json'); } else { console.log('No blog posts found in the database.'); } } catch (error) { console.error('Error:', error); } } getTableSchema();