78 lines
2.2 KiB
JavaScript
78 lines
2.2 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Production database migration script for DigitalOcean deployment
|
|
* This script runs database migrations against the production MySQL database
|
|
*/
|
|
|
|
import { drizzle } from 'drizzle-orm/mysql2';
|
|
import mysql from 'mysql2/promise';
|
|
import { migrate } from 'drizzle-orm/mysql2/migrator';
|
|
import { config } from 'dotenv';
|
|
import { fileURLToPath } from 'url';
|
|
import { dirname, join } from 'path';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = dirname(__filename);
|
|
|
|
// Load environment variables
|
|
config();
|
|
|
|
async function runMigrations() {
|
|
console.log('🚀 Starting production database migration...');
|
|
|
|
if (!process.env.DATABASE_URL) {
|
|
console.error('❌ DATABASE_URL environment variable is required');
|
|
process.exit(1);
|
|
}
|
|
|
|
let connection;
|
|
|
|
try {
|
|
// Parse DATABASE_URL for DigitalOcean managed database
|
|
const dbUrl = new URL(process.env.DATABASE_URL);
|
|
|
|
// Create connection to MySQL
|
|
connection = await mysql.createConnection({
|
|
host: dbUrl.hostname,
|
|
port: parseInt(dbUrl.port) || 25060,
|
|
user: dbUrl.username,
|
|
password: dbUrl.password,
|
|
database: dbUrl.pathname.slice(1), // Remove leading slash
|
|
ssl: {
|
|
rejectUnauthorized: false // DigitalOcean managed databases use SSL
|
|
},
|
|
connectTimeout: 60000,
|
|
acquireTimeout: 60000,
|
|
timeout: 60000,
|
|
});
|
|
|
|
console.log('✅ Connected to database');
|
|
|
|
// Create drizzle instance
|
|
const db = drizzle(connection);
|
|
|
|
// Run migrations
|
|
console.log('🔄 Running migrations...');
|
|
await migrate(db, { migrationsFolder: join(__dirname, '../drizzle') });
|
|
|
|
console.log('✅ Migrations completed successfully!');
|
|
|
|
} catch (error) {
|
|
console.error('❌ Migration failed:', error);
|
|
process.exit(1);
|
|
} finally {
|
|
if (connection) {
|
|
await connection.end();
|
|
console.log('🔌 Database connection closed');
|
|
}
|
|
}
|
|
}
|
|
|
|
// Run migrations if this script is executed directly
|
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
runMigrations().catch(console.error);
|
|
}
|
|
|
|
export { runMigrations };
|