freshyo/packages/migrator/scripts/generate-drop.js
2026-03-27 18:47:12 +05:30

37 lines
908 B
JavaScript

import fs from 'fs'
import path from 'path'
const inputPath = process.argv[2]
const outputPath = process.argv[3]
if (!inputPath || !outputPath) {
console.error('Usage: node generate-drop.js <input.sql> <output.sql>')
process.exit(1)
}
const input = fs.readFileSync(path.resolve(inputPath), 'utf8')
const tableRegex = /CREATE TABLE IF NOT EXISTS "([^"]+)"/g
const tables = []
let match
while ((match = tableRegex.exec(input)) !== null) {
tables.push(match[1])
}
const uniqueTables = Array.from(new Set(tables))
const drops = [
'PRAGMA foreign_keys=OFF;',
'BEGIN TRANSACTION;'
]
// Drop in reverse order of creation
for (const table of uniqueTables.reverse()) {
drops.push(`DROP TABLE IF EXISTS "${table}";`)
}
drops.push('COMMIT;')
fs.writeFileSync(path.resolve(outputPath), drops.join('\n') + '\n', 'utf8')
console.log(`Wrote ${outputPath} with ${uniqueTables.length} DROP statements`)