updated script

This commit is contained in:
Deep Koluguri 2026-04-05 15:53:17 -04:00
parent e4089c9f75
commit ca9548bfea
1 changed files with 14 additions and 13 deletions

View File

@ -152,10 +152,13 @@ async function main() {
pool: { max: 5, min: 0, idle: 10000 }, pool: { max: 5, min: 0, idle: 10000 },
}); });
// Sequelize sqlite connection-manager runs PRAGMA FOREIGN_KEYS=ON on every new connection
// unless foreignKeys === false, which overrides manual PRAGMA OFF during pooled/transaction use.
const sqlite = new Sequelize({ const sqlite = new Sequelize({
dialect: 'sqlite', dialect: 'sqlite',
storage: sqlitePath, storage: sqlitePath,
logging: false, logging: false,
foreignKeys: false,
define: { underscored: true, freezeTableName: true }, define: { underscored: true, freezeTableName: true },
}); });
@ -178,23 +181,21 @@ async function main() {
} }
} }
// 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…'); console.log('\n🗑 Clearing SQLite tables…');
await sqlite.query('PRAGMA foreign_keys = OFF'); for (const table of TABLES_DELETE_REVERSE) {
try { await sqlite.query(`DELETE FROM "${table}"`, { type: QueryTypes.DELETE });
for (const table of TABLES_DELETE_REVERSE) { }
await sqlite.query(`DELETE FROM "${table}"`, { type: QueryTypes.DELETE }); console.log(' Done.\n');
}
console.log(' Done.\n');
console.log('📥 Copying from PostgreSQL → SQLite…\n'); console.log('📥 Copying from PostgreSQL → SQLite…\n');
for (const table of TABLES_ORDER) { for (const table of TABLES_ORDER) {
const rows = await fetchPostgresTable(pg, table); const rows = await fetchPostgresTable(pg, table);
try {
await insertRows(sqlite, table, rows); await insertRows(sqlite, table, rows);
} catch (e) {
e.message = `[${table}] ${e.message}`;
throw e;
} }
} finally {
await sqlite.query('PRAGMA foreign_keys = ON');
} }
await pg.close(); await pg.close();