mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
55607ff2f9
npm run db:move-to-postgres (dist/move-to-postgres.js in the image) copies a SQLite file at this build's migration level into an empty Postgres database. It refuses a file at another level, rows whose references point at nothing and a target holding any table or plugin schema, creates the schema with the engine's own bootstrap, then in one transaction copies every table in foreign key order (setting a self-reference once its rows are in), converts each value to its column's type and refuses one Postgres cannot hold exactly, copies each plugin's plugin.db into its schema, moves the sequences past the copied ids and compares every table's row count and an order-free checksum over all rows before it commits. A failure drops the schema again. The SQLite files and the uploads are never written.
57 lines
2.4 KiB
TypeScript
57 lines
2.4 KiB
TypeScript
/**
|
|
* The order the move fills the tables in, from the target's foreign keys: a
|
|
* table after every table it points at, so each row it inserts finds the row
|
|
* it references. Ties go by name, so two runs over one schema copy in the
|
|
* same order.
|
|
*
|
|
* A foreign key that cannot be satisfied by the order alone (a table pointing
|
|
* at itself, like a reply at its message, or a cycle between tables) is
|
|
* deferred: its columns are inserted as NULL and set once every row of the
|
|
* tables is in, still inside the move's one transaction. Postgres checks each
|
|
* foreign key when the row is written, so this is what lets a parent row
|
|
* arrive after its child.
|
|
*/
|
|
|
|
/** One foreign key of the target: `columns` of `table` point at `references`. */
|
|
export interface ForeignKey {
|
|
table: string;
|
|
columns: readonly string[];
|
|
references: string;
|
|
}
|
|
|
|
interface CopyPlan {
|
|
/** Every table, each after the tables it points at. */
|
|
order: string[];
|
|
/** Per table, the foreign key columns written as NULL first and set after all rows are in. */
|
|
deferred: Map<string, string[]>;
|
|
}
|
|
|
|
function defer(plan: CopyPlan, fk: ForeignKey): void {
|
|
const columns = plan.deferred.get(fk.table) ?? [];
|
|
for (const column of fk.columns) if (!columns.includes(column)) columns.push(column);
|
|
plan.deferred.set(fk.table, columns);
|
|
}
|
|
|
|
/** Plans the order for `tables`. A foreign key to a table outside the list (one the move does not copy) is ignored. */
|
|
export function planCopyOrder(tables: readonly string[], foreignKeys: readonly ForeignKey[]): CopyPlan {
|
|
const plan: CopyPlan = { order: [], deferred: new Map() };
|
|
const known = new Set(tables);
|
|
const pending = new Set([...tables].sort());
|
|
const keys = foreignKeys.filter((fk) => known.has(fk.table) && known.has(fk.references));
|
|
for (const fk of keys) if (fk.table === fk.references) defer(plan, fk);
|
|
const between = keys.filter((fk) => fk.table !== fk.references);
|
|
|
|
while (pending.size > 0) {
|
|
const waitsOn = (table: string) => between.filter((fk) => fk.table === table && pending.has(fk.references));
|
|
let next = [...pending].find((table) => waitsOn(table).length === 0);
|
|
if (next === undefined) {
|
|
// A cycle: break it at the first table by name, deferring its keys into the rest of the cycle.
|
|
next = [...pending][0];
|
|
for (const fk of waitsOn(next)) defer(plan, fk);
|
|
}
|
|
plan.order.push(next);
|
|
pending.delete(next);
|
|
}
|
|
return plan;
|
|
}
|