Final version
This commit is contained in:
26
server/scripts/migrate.js
Normal file
26
server/scripts/migrate.js
Normal file
@ -0,0 +1,26 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import url from 'url';
|
||||
import pool from '../src/config/db.js';
|
||||
|
||||
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
|
||||
|
||||
async function run() {
|
||||
const schemaPath = path.join(__dirname, '..', 'sql', 'schema.sql');
|
||||
const sql = fs.readFileSync(schemaPath, 'utf8');
|
||||
const statements = sql.split(/;\s*\n/).filter(s => s.trim());
|
||||
const conn = await pool.getConnection();
|
||||
try {
|
||||
for (const stmt of statements) {
|
||||
await conn.query(stmt);
|
||||
}
|
||||
console.log('Migration completed');
|
||||
} finally {
|
||||
conn.release();
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
run().catch(err => { console.error(err); process.exit(1); });
|
||||
|
||||
|
||||
26
server/scripts/seed.js
Normal file
26
server/scripts/seed.js
Normal file
@ -0,0 +1,26 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import url from 'url';
|
||||
import pool from '../src/config/db.js';
|
||||
|
||||
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
|
||||
|
||||
async function run() {
|
||||
const seedPath = path.join(__dirname, '..', 'sql', 'seed.sql');
|
||||
const sql = fs.readFileSync(seedPath, 'utf8');
|
||||
const statements = sql.split(/;\s*\n/).filter(s => s.trim());
|
||||
const conn = await pool.getConnection();
|
||||
try {
|
||||
for (const stmt of statements) {
|
||||
await conn.query(stmt);
|
||||
}
|
||||
console.log('Seed completed');
|
||||
} finally {
|
||||
conn.release();
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
run().catch(err => { console.error(err); process.exit(1); });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user