-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-migrations.js
More file actions
144 lines (120 loc) Β· 4.63 KB
/
run-migrations.js
File metadata and controls
144 lines (120 loc) Β· 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Database Migration Runner for StudySync AI
*
* This script runs SQL migrations against your Supabase database.
*
* Usage:
* 1. Make sure you have your Supabase credentials in .env
* 2. Run: node run-migrations.js
*
* The script will:
* - Connect to your Supabase database
* - Run all pending migrations in order
* - Track which migrations have been applied
*/
import { createClient } from '@supabase/supabase-js';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
// Load environment variables
dotenv.config({ path: '.env' });
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Supabase configuration
const supabaseUrl = process.env.VITE_SUPABASE_URL;
const supabaseKey = process.env.VITE_SUPABASE_ANON_KEY;
if (!supabaseUrl || !supabaseKey) {
console.error('β Error: Missing Supabase credentials in .env file');
console.error('Please ensure VITE_SUPABASE_URL and VITE_SUPABASE_ANON_KEY are set');
process.exit(1);
}
const supabase = createClient(supabaseUrl, supabaseKey);
// Create migrations tracking table if it doesn't exist
async function createMigrationsTable() {
const { error } = await supabase.rpc('exec_sql', {
sql: `
CREATE TABLE IF NOT EXISTS schema_migrations (
id SERIAL PRIMARY KEY,
migration_name TEXT NOT NULL UNIQUE,
applied_at TIMESTAMPTZ DEFAULT NOW()
);
`
});
if (error) {
console.log('β οΈ Note: Could not create migrations table via RPC.');
console.log(' Please run the migrations manually in Supabase SQL Editor.');
console.log(' Or use Supabase CLI: supabase db push');
return false;
}
return true;
}
// Get list of applied migrations
async function getAppliedMigrations() {
const { data, error } = await supabase
.from('schema_migrations')
.select('migration_name');
if (error) {
console.log('β οΈ Could not fetch applied migrations');
return [];
}
return data.map(row => row.migration_name);
}
// Run a single migration
async function runMigration(migrationFile) {
const migrationPath = path.join(__dirname, 'migrations', migrationFile);
const sql = fs.readFileSync(migrationPath, 'utf8');
console.log(`\nπ Running migration: ${migrationFile}`);
// Note: Supabase client doesn't support raw SQL execution directly
// You'll need to run these in the Supabase SQL Editor or use Supabase CLI
console.log('β οΈ Please run this migration in Supabase SQL Editor:');
console.log(` File: migrations/${migrationFile}`);
console.log(' Or use: supabase db push');
return false;
}
// Main migration runner
async function runMigrations() {
console.log('π StudySync AI - Database Migration Runner\n');
// Get all migration files
const migrationsDir = path.join(__dirname, 'migrations');
if (!fs.existsSync(migrationsDir)) {
console.error('β Migrations directory not found');
process.exit(1);
}
const migrationFiles = fs.readdirSync(migrationsDir)
.filter(file => file.endsWith('.sql'))
.sort();
if (migrationFiles.length === 0) {
console.log('β
No migrations found');
return;
}
console.log(`π¦ Found ${migrationFiles.length} migration(s):\n`);
migrationFiles.forEach((file, index) => {
console.log(` ${index + 1}. ${file}`);
});
console.log('\n' + '='.repeat(60));
console.log('\nβ οΈ IMPORTANT: Supabase Migration Instructions\n');
console.log('Since the Supabase JS client doesn\'t support raw SQL execution,');
console.log('please run these migrations manually:\n');
console.log('Option 1: Supabase Dashboard (Recommended)');
console.log(' 1. Go to https://supabase.com/dashboard');
console.log(' 2. Select your project');
console.log(' 3. Click "SQL Editor" in the left sidebar');
console.log(' 4. Click "New Query"');
console.log(' 5. Copy and paste the contents of each migration file');
console.log(' 6. Click "Run" to execute\n');
console.log('Option 2: Supabase CLI');
console.log(' 1. Install: npm install -g supabase');
console.log(' 2. Link project: supabase link');
console.log(' 3. Run: supabase db push\n');
console.log('Migrations to run (in order):');
migrationFiles.forEach((file, index) => {
console.log(` ${index + 1}. migrations/${file}`);
});
console.log('\n' + '='.repeat(60) + '\n');
}
// Run the migrations
runMigrations().catch(error => {
console.error('β Migration error:', error);
process.exit(1);
});