updated migration script
This commit is contained in:
parent
8a4529066d
commit
e4089c9f75
|
|
@ -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,20 +178,23 @@ 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(' Done.\n');
|
||||
|
||||
console.log('📥 Copying from PostgreSQL → SQLite…\n');
|
||||
for (const table of TABLES_ORDER) {
|
||||
const rows = await fetchPostgresTable(pg, table);
|
||||
await insertRows(sqlite, table, rows);
|
||||
console.log('📥 Copying from PostgreSQL → SQLite…\n');
|
||||
for (const table of TABLES_ORDER) {
|
||||
const rows = await fetchPostgresTable(pg, table);
|
||||
await insertRows(sqlite, table, rows);
|
||||
}
|
||||
} finally {
|
||||
await sqlite.query('PRAGMA foreign_keys = ON');
|
||||
}
|
||||
|
||||
await pg.close();
|
||||
|
|
|
|||
Loading…
Reference in New Issue