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