fix: support existing meal planning bridge tables

This commit is contained in:
OpenClaw Bot
2026-05-11 23:36:52 +02:00
parent ba534cb864
commit 1828bef8f1
3 changed files with 133 additions and 35 deletions
+45
View File
@@ -1437,6 +1437,51 @@ const MIGRATIONS = [
CREATE INDEX IF NOT EXISTS idx_kids_cookbooks_recipe ON kids_cookbooks(recipe_id);
`,
},
{
version: 40,
description: 'Harmonize existing meal planning bridge tables',
up(database) {
const columns = (table) => new Set(database.prepare(`PRAGMA table_info(${table})`).all().map((row) => row.name));
const addColumn = (table, name, definition) => {
if (!columns(table).has(name)) database.exec(`ALTER TABLE ${table} ADD COLUMN ${name} ${definition}`);
};
addColumn('meal_cooking_rules', 'priority', "INTEGER NOT NULL DEFAULT 100");
addColumn('meal_cooking_rules', 'created_by', "INTEGER REFERENCES users(id) ON DELETE SET NULL");
addColumn('recipe_family_preferences', 'created_by', "INTEGER REFERENCES users(id) ON DELETE SET NULL");
addColumn('recipe_variation_meta', 'kid_suitable_confidence', "INTEGER NOT NULL DEFAULT 0");
addColumn('recipe_variation_meta', 'created_by', "INTEGER REFERENCES users(id) ON DELETE SET NULL");
addColumn('planned_meal_cooks', 'planned_for_date', "TEXT");
addColumn('planned_meal_cooks', 'source_plan_id', "TEXT");
addColumn('planned_meal_cooks', 'created_by', "INTEGER REFERENCES users(id) ON DELETE SET NULL");
database.exec(`UPDATE planned_meal_cooks SET planned_for_date = COALESCE(planned_for_date, meal_date) WHERE planned_for_date IS NULL AND meal_date IS NOT NULL`);
addColumn('meal_plan_feedback', 'plan_id', "TEXT");
addColumn('meal_plan_feedback', 'meal_id', "INTEGER REFERENCES meals(id) ON DELETE SET NULL");
addColumn('meal_plan_feedback', 'meal_type', "TEXT");
addColumn('meal_plan_feedback', 'action', "TEXT NOT NULL DEFAULT 'edit'");
addColumn('meal_plan_feedback', 'original_title', "TEXT");
addColumn('meal_plan_feedback', 'final_title', "TEXT");
addColumn('meal_plan_feedback', 'notes', "TEXT");
addColumn('meal_plan_feedback', 'user_id', "INTEGER REFERENCES users(id) ON DELETE SET NULL");
database.exec(`UPDATE meal_plan_feedback SET action = COALESCE(NULLIF(action, ''), type, 'edit') WHERE action IS NULL OR action = ''`);
addColumn('kids_cookbooks', 'content_json', "TEXT");
database.exec(`UPDATE kids_cookbooks SET content_json = COALESCE(content_json, payload) WHERE content_json IS NULL AND payload IS NOT NULL`);
database.exec(`
CREATE INDEX IF NOT EXISTS idx_meal_cooking_rules_weekday ON meal_cooking_rules(weekday, meal_type);
CREATE INDEX IF NOT EXISTS idx_recipe_family_preferences_user ON recipe_family_preferences(user_id, preference);
CREATE INDEX IF NOT EXISTS idx_planned_meal_cooks_date ON planned_meal_cooks(planned_for_date, meal_type);
CREATE INDEX IF NOT EXISTS idx_meal_plan_feedback_recipe ON meal_plan_feedback(recipe_id, action);
CREATE INDEX IF NOT EXISTS idx_meal_plan_feedback_created ON meal_plan_feedback(created_at);
CREATE INDEX IF NOT EXISTS idx_kids_cookbooks_recipe ON kids_cookbooks(recipe_id);
`);
},
},
];
/**