Multi tenant DB, registratie per huishouden V1.2
This commit is contained in:
@@ -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.");
|
||||
Reference in New Issue
Block a user