updated migration script

This commit is contained in:
Deep Koluguri 2026-04-05 15:47:51 -04:00
parent 8a4529066d
commit e4089c9f75
1 changed files with 14 additions and 15 deletions

View File

@ -112,10 +112,6 @@ async function fetchPostgresTable(pg, table) {
} }
} }
async function clearSqliteTable(sqlite, table) {
await sqlite.query(`DELETE FROM "${table}"`, { type: QueryTypes.DELETE });
}
async function insertRows(sqlite, table, rows) { async function insertRows(sqlite, table, rows) {
if (!rows.length) { if (!rows.length) {
console.log(` ${table}: 0 rows`); console.log(` ${table}: 0 rows`);
@ -182,14 +178,14 @@ async function main() {
} }
} }
console.log('\n🗑 Clearing SQLite tables (child → parent order)…'); // SQLite enforces FKs on INSERT. `users.created_by` → `users.id` must not depend on row
await sqlite.transaction(async (transaction) => { // order from Postgres; same for optional refs. Turn FKs off for the whole clear+copy, then on.
await sqlite.query('PRAGMA foreign_keys = OFF', { transaction }); console.log('\n🗑 Clearing SQLite tables…');
await sqlite.query('PRAGMA foreign_keys = OFF');
try {
for (const table of TABLES_DELETE_REVERSE) { for (const table of TABLES_DELETE_REVERSE) {
await sqlite.query(`DELETE FROM "${table}"`, { type: QueryTypes.DELETE, transaction }); await sqlite.query(`DELETE FROM "${table}"`, { type: QueryTypes.DELETE });
} }
await sqlite.query('PRAGMA foreign_keys = ON', { transaction });
});
console.log(' Done.\n'); console.log(' Done.\n');
console.log('📥 Copying from PostgreSQL → SQLite…\n'); console.log('📥 Copying from PostgreSQL → SQLite…\n');
@ -197,6 +193,9 @@ async function main() {
const rows = await fetchPostgresTable(pg, table); const rows = await fetchPostgresTable(pg, table);
await insertRows(sqlite, table, rows); await insertRows(sqlite, table, rows);
} }
} finally {
await sqlite.query('PRAGMA foreign_keys = ON');
}
await pg.close(); await pg.close();
await sqlite.close(); await sqlite.close();