From ca9548bfea1c0550965bfcb69c57d26886bee038 Mon Sep 17 00:00:00 2001 From: Deep Koluguri Date: Sun, 5 Apr 2026 15:53:17 -0400 Subject: [PATCH] updated script --- backend/scripts/migrate-pg-to-sqlite.js | 27 +++++++++++++------------ 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/backend/scripts/migrate-pg-to-sqlite.js b/backend/scripts/migrate-pg-to-sqlite.js index f66a101..88059d8 100644 --- a/backend/scripts/migrate-pg-to-sqlite.js +++ b/backend/scripts/migrate-pg-to-sqlite.js @@ -152,10 +152,13 @@ async function main() { 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({ dialect: 'sqlite', storage: sqlitePath, logging: false, + foreignKeys: false, 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…'); - await sqlite.query('PRAGMA foreign_keys = OFF'); - try { - for (const table of TABLES_DELETE_REVERSE) { - await sqlite.query(`DELETE FROM "${table}"`, { type: QueryTypes.DELETE }); - } - console.log(' Done.\n'); + for (const table of TABLES_DELETE_REVERSE) { + await sqlite.query(`DELETE FROM "${table}"`, { type: QueryTypes.DELETE }); + } + console.log(' Done.\n'); - console.log('📥 Copying from PostgreSQL → SQLite…\n'); - for (const table of TABLES_ORDER) { - const rows = await fetchPostgresTable(pg, table); + console.log('📥 Copying from PostgreSQL → SQLite…\n'); + for (const table of TABLES_ORDER) { + const rows = await fetchPostgresTable(pg, table); + try { 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();