-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_planetscale.js
More file actions
53 lines (43 loc) · 1.88 KB
/
Copy pathmigrate_planetscale.js
File metadata and controls
53 lines (43 loc) · 1.88 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
const mysql = require('mysql2/promise');
const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
dotenv.config();
async function migrateToPlanetScale() {
console.log('🚀 Starting PlanetScale Migration...');
const connectionUrl = process.env.DATABASE_URL;
if (!connectionUrl) {
console.error('❌ Error: DATABASE_URL not found in .env file.');
console.log('💡 Tip: Get your connection string from PlanetScale dashboard.');
return;
}
let connection;
try {
console.log('📡 Connecting to PlanetScale...');
// PlanetScale requires SSL
connection = await mysql.createConnection(connectionUrl + (connectionUrl.includes('?') ? '&' : '?') + "multipleStatements=true");
console.log('✅ Connected.');
console.log('📦 Applying Schema...');
const schemaPath = path.join(__dirname, 'database', 'schema.sql');
const schema = fs.readFileSync(schemaPath, 'utf8');
// PlanetScale doesn't support 'CREATE DATABASE' or 'USE database' in branches
const cleanedSchema = schema.replace(/CREATE DATABASE IF NOT EXISTS.*;/gi, '')
.replace(/USE.*;/gi, '');
await connection.query(cleanedSchema);
console.log('✅ Schema Applied.');
console.log('🌱 Seeding Data...');
const seedPath = path.join(__dirname, 'database', 'seed.sql');
if (fs.existsSync(seedPath)) {
const seed = fs.readFileSync(seedPath, 'utf8');
await connection.query(seed);
console.log('✅ Seeding Successful!');
}
console.log('🎉 Migration Successful! Your database is now live on PlanetScale.');
} catch (err) {
console.error('❌ Migration Failed:', err.message);
console.error(err);
} finally {
if (connection) await connection.end();
}
}
migrateToPlanetScale();