Multi tenant DB, registratie per huishouden V1.2

This commit is contained in:
2026-08-09 19:50:53 +02:00
parent 1f9fd73026
commit c37b8956d4
15 changed files with 148 additions and 96 deletions
+18
View File
@@ -210,5 +210,23 @@ CREATE TABLE IF NOT EXISTS product_stores (
`);
const addColumnIfMissing = (table, column, definition) => {
const columns = db.prepare(`PRAGMA table_info(${table})`).all();
if (!columns.some(existing => existing.name === column)) {
db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${definition}`);
}
};
addColumnIfMissing("categories", "household_id", "INTEGER REFERENCES households(id)");
addColumnIfMissing("products", "household_id", "INTEGER REFERENCES households(id)");
const defaultHousehold = db.prepare("SELECT id FROM households ORDER BY id LIMIT 1").get();
if (defaultHousehold) {
db.prepare("UPDATE categories SET household_id = ? WHERE household_id IS NULL")
.run(defaultHousehold.id);
db.prepare("UPDATE products SET household_id = ? WHERE household_id IS NULL")
.run(defaultHousehold.id);
}
console.log("Database migratie voltooid.");