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) {
|
async function insertRows(sqlite, table, rows) {
|
||||||
if (!rows.length) {
|
if (!rows.length) {
|
||||||
console.log(` ${table}: 0 rows`);
|
console.log(` ${table}: 0 rows`);
|
||||||
|
|
@ -182,20 +178,23 @@ 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');
|
||||||
for (const table of TABLES_ORDER) {
|
for (const table of TABLES_ORDER) {
|
||||||
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();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue