63 lines
1.5 KiB
JavaScript
63 lines
1.5 KiB
JavaScript
#!/usr/bin/env node
|
|
/* eslint-disable no-console */
|
|
require('dotenv').config();
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { PrismaClient } = require('@prisma/client');
|
|
|
|
const fileArg = process.argv[2];
|
|
|
|
if (!fileArg) {
|
|
console.error('Usage: node scripts/run-sql-patch.js <path-to.sql>');
|
|
process.exit(1);
|
|
}
|
|
|
|
const filePath = path.resolve(process.cwd(), fileArg);
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
console.error(`SQL file not found: ${filePath}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
const splitStatements = (sql) =>
|
|
sql
|
|
.split(/;\s*(?:\r?\n|$)/)
|
|
.map((part) =>
|
|
part
|
|
.split(/\r?\n/)
|
|
.filter((line) => !line.trim().startsWith('--'))
|
|
.join('\n')
|
|
.trim()
|
|
)
|
|
.filter(Boolean);
|
|
|
|
async function main() {
|
|
const sql = fs.readFileSync(filePath, 'utf8');
|
|
const statements = splitStatements(sql);
|
|
|
|
if (statements.length === 0) {
|
|
console.error('No SQL statements found in file.');
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`Applying ${statements.length} statement(s) from ${path.basename(filePath)}...`);
|
|
|
|
for (const [index, statement] of statements.entries()) {
|
|
await prisma.$executeRawUnsafe(`${statement};`);
|
|
console.log(` [${index + 1}/${statements.length}] OK`);
|
|
}
|
|
|
|
console.log('Patch applied successfully.');
|
|
}
|
|
|
|
main()
|
|
.catch((err) => {
|
|
console.error('Patch failed:', err.message);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|