diff --git a/backend/database.py b/backend/database.py index c816ae2..e355710 100644 --- a/backend/database.py +++ b/backend/database.py @@ -298,18 +298,23 @@ def init_db(): name = name[:m2.start()] + '(' + m2.group(1) + ')' c.execute("UPDATE recipes SET name = ? WHERE id = ?", (name, row['id'])) - # Migration: add (一) to base recipes that have numbered siblings + # Migration: add number suffix to base recipes that have numbered siblings _all_recipes2 = c.execute("SELECT id, name FROM recipes").fetchall() + _cn_nums = list('一二三四五六七八九十') _base_groups = {} for row in _all_recipes2: name = row['name'] - m = _re.match(r'^(.+?)([一二三四五六七八九十]+)$', name) + m = _re.match(r'^(.+?)(([一二三四五六七八九十]+))$', name) if m: - _base_groups.setdefault(m.group(1), []).append(row['id']) - # Find bare names that match a numbered group + _base_groups.setdefault(m.group(1), set()).add(m.group(2)) + # Find bare names that match a numbered group, assign next available number for row in _all_recipes2: if row['name'] in _base_groups: - c.execute("UPDATE recipes SET name = ? WHERE id = ?", (row['name'] + '(一)', row['id'])) + used = _base_groups[row['name']] + next_num = next((n for n in _cn_nums if n not in used), '十') + new_name = row['name'] + '(' + next_num + ')' + c.execute("UPDATE recipes SET name = ? WHERE id = ?", (new_name, row['id'])) + _base_groups[row['name']].add(next_num) # Seed admin user if no users exist count = c.execute("SELECT COUNT(*) FROM users").fetchone()[0]