Sicherung einspielen: nur vorhandene Spalten übernehmen
Neue Spalten behalten ihren Vorgabewert, damit eine ältere Sicherung auch nach einer Migration passt. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
This commit is contained in:
@@ -29,8 +29,13 @@ export async function speichereStand(c: Client, s: Stand): Promise<Record<string
|
|||||||
// TABELLEN steht in der Reihenfolge der Fremdschlüssel
|
// TABELLEN steht in der Reihenfolge der Fremdschlüssel
|
||||||
for (const t of TABELLEN) {
|
for (const t of TABELLEN) {
|
||||||
const zeilen = s.daten[t.pfad] ?? [];
|
const zeilen = s.daten[t.pfad] ?? [];
|
||||||
|
// nur Spalten, die es gibt und die in der Sicherung stehen: neue Spalten behalten ihren Vorgabewert,
|
||||||
|
// so passt eine ältere Sicherung auch nach einer Migration
|
||||||
|
const vorhanden = new Set((await c.query<{ column_name: string }>(
|
||||||
|
"select column_name from information_schema.columns where table_schema = current_schema() and table_name = $1", [t.name])).rows.map((r) => r.column_name));
|
||||||
for (const z of zeilen) {
|
for (const z of zeilen) {
|
||||||
await c.query(`insert into ${t.name} select * from json_populate_record(null::${t.name}, $1::json)`, [JSON.stringify(z)]);
|
const spalten = Object.keys(z).filter((k) => vorhanden.has(k)).join(", ");
|
||||||
|
await c.query(`insert into ${t.name} (${spalten}) select ${spalten} from json_populate_record(null::${t.name}, $1::json)`, [JSON.stringify(z)]);
|
||||||
}
|
}
|
||||||
anzahl[t.pfad] = zeilen.length;
|
anzahl[t.pfad] = zeilen.length;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user