Favorieten toegevoegd aan de navigatiebalk en sidebar.

This commit is contained in:
2026-08-29 19:53:28 +02:00
parent 2912f7c455
commit ce07575d5e
19 changed files with 355 additions and 29 deletions
+31 -4
View File
@@ -163,18 +163,18 @@ CREATE TABLE IF NOT EXISTS product_favorites (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
household_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE(user_id, product_id),
UNIQUE(household_id, product_id),
FOREIGN KEY(user_id)
REFERENCES users(id)
FOREIGN KEY(household_id)
REFERENCES households(id)
ON DELETE CASCADE,
@@ -217,8 +217,35 @@ const addColumnIfMissing = (table, column, definition) => {
}
};
const migrateFavoriteTableToHouseholdScope = () => {
const columns = db.prepare(`PRAGMA table_info(product_favorites)`).all();
const hasHouseholdId = columns.some(column => column.name === "household_id");
if (!hasHouseholdId) {
db.exec(`ALTER TABLE product_favorites ADD COLUMN household_id INTEGER`);
db.exec(`UPDATE product_favorites
SET household_id = (
SELECT products.household_id
FROM products
WHERE products.id = product_favorites.product_id
)
WHERE household_id IS NULL`);
db.exec(`DELETE FROM product_favorites
WHERE id NOT IN (
SELECT MIN(id)
FROM product_favorites
GROUP BY household_id, product_id
)`);
db.exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_product_favorites_household_product
ON product_favorites (household_id, product_id)`);
}
};
addColumnIfMissing("categories", "household_id", "INTEGER REFERENCES households(id)");
addColumnIfMissing("products", "household_id", "INTEGER REFERENCES households(id)");
migrateFavoriteTableToHouseholdScope();
const defaultHousehold = db.prepare("SELECT id FROM households ORDER BY id LIMIT 1").get();
if (defaultHousehold) {