refactor(backend): Alembic owns the local schema; v1 ALTER list deleted
Platform v2 step 1.0 (APPS_PLATFORM D-13, D-17). - migrations/ with env.py (render_as_batch, models/all.py metadata, no fileConfig) and baseline 0001_v2_baseline autogenerated from the models and reviewed; it also seeds the pos_settings rows a fresh v1 install got - core/schema.py::upgrade_to_head() is the only way the schema is built: app startup, seed.py, demo_seed.py, wipe_database.py and the tests. It refuses v1 databases (tables without alembic_version) with a clear error - main.py: _run_migrations() and create_all removed (723 lines) - sync_events became a model (was raw DDL); price_event_log order indexes declared on the model; unused order_discounts.price_before/after dropped - tests/guards: no-drift + idempotent upgrade + install defaults + v1 refusal + no create_all; placeholders for the time (1.1) and access (1.5) guards - CS-1 rewritten for Alembic on this branch Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Wipes the POS database completely and re-applies the schema + default seed data.
|
||||
Wipes the POS database completely and re-creates it through Alembic
|
||||
(schema + install defaults, migrations/versions/0001_v2_baseline.py).
|
||||
|
||||
Usage:
|
||||
python wipe_database.py
|
||||
@@ -23,27 +24,9 @@ if answer != "YES":
|
||||
|
||||
# ── Imports (after confirmation, so we don't waste time if user bails) ────────
|
||||
from sqlalchemy import text, inspect
|
||||
from database import engine, Base, SessionLocal
|
||||
from database import engine
|
||||
|
||||
# Import all models so Base.metadata knows about every table.
|
||||
# Order matters: tables with no FK dependencies first, then dependents.
|
||||
import models.user # noqa: F401
|
||||
import models.table # noqa: F401
|
||||
import models.printer # noqa: F401
|
||||
import models.product # noqa: F401
|
||||
import models.customers # noqa: F401 must be before models.order (orders.customer_id FK)
|
||||
import models.order # noqa: F401
|
||||
import models.business_day # noqa: F401
|
||||
import models.shift # noqa: F401
|
||||
import models.settings # noqa: F401
|
||||
import models.flag # noqa: F401
|
||||
import models.message # noqa: F401
|
||||
import models.notes # noqa: F401
|
||||
import models.expenses # noqa: F401
|
||||
import models.tabs # noqa: F401
|
||||
import models.schedule # noqa: F401
|
||||
import models.waste # noqa: F401
|
||||
import models.reservation # noqa: F401
|
||||
from core.schema import upgrade_to_head
|
||||
|
||||
print("\nDropping all tables...")
|
||||
|
||||
@@ -65,63 +48,8 @@ with engine.connect() as conn:
|
||||
conn.execute(text("PRAGMA foreign_keys = ON"))
|
||||
conn.commit()
|
||||
|
||||
print("\nRecreating schema...")
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
# Re-run the same migration/seed SQL from main.py so default settings,
|
||||
# flags, and quick message templates are present after the wipe.
|
||||
print("Applying seed migrations...")
|
||||
|
||||
seed_migrations = [
|
||||
# pos_settings defaults
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('shifts.waiter_self_start', 'true', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('shifts.waiter_self_end', 'true', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('business_day.force_close_allowed', 'true', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('flags.display_mode', 'both', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('print.ticket_mode', 'detailed', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('print.font_order_number', '48:1:0', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('print.font_meta', '0:0:0', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('print.font_item_name', '16:1:0', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('print.font_quick', '0:0:0', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('print.font_pref', '0:0:0', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('print.font_extra', '0:0:0', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('print.font_ingredient', '0:0:0', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('print.font_item_note', '0:0:0', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('print.font_order_note', '0:1:0', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('venue.name', '', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('venue.type', '', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('security.login_method', 'password', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('security.autofill_username', 'true', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('security.auto_lock', 'false', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('security.auto_lock_seconds', '300', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('security.auto_logout', 'false', CURRENT_TIMESTAMP)",
|
||||
"INSERT OR IGNORE INTO pos_settings (key, value, updated_at) VALUES ('security.auto_logout_seconds', '1800', CURRENT_TIMESTAMP)",
|
||||
# Default table flag definitions
|
||||
"INSERT OR IGNORE INTO table_flag_defs (id, name, emoji, color, sort_order) VALUES (1, 'Χρειάζεται καθάρισμα', '🧹', '#ef4444', 1)",
|
||||
"INSERT OR IGNORE INTO table_flag_defs (id, name, emoji, color, sort_order) VALUES (2, 'Χρειάζεται Βοήθεια', '🆘', '#f97316', 2)",
|
||||
"INSERT OR IGNORE INTO table_flag_defs (id, name, emoji, color, sort_order) VALUES (3, 'Χρειάζεται Σερβιτόρο', '🔔', '#eab308', 3)",
|
||||
"INSERT OR IGNORE INTO table_flag_defs (id, name, emoji, color, sort_order) VALUES (4, 'Περιμένει να πληρώσει', '💳', '#3b82f6', 4)",
|
||||
"INSERT OR IGNORE INTO table_flag_defs (id, name, emoji, color, sort_order) VALUES (5, 'VIP', '⭐', '#8b5cf6', 5)",
|
||||
"INSERT OR IGNORE INTO table_flag_defs (id, name, emoji, color, sort_order) VALUES (6, 'Ευγενικός Πελάτης', '😊', '#22c55e', 6)",
|
||||
"INSERT OR IGNORE INTO table_flag_defs (id, name, emoji, color, sort_order) VALUES (7, 'Αγενής Πελάτης', '😤', '#dc2626', 7)",
|
||||
"INSERT OR IGNORE INTO table_flag_defs (id, name, emoji, color, sort_order) VALUES (8, 'Αλλεργίες', '⚠️', '#f59e0b', 8)",
|
||||
"INSERT OR IGNORE INTO table_flag_defs (id, name, emoji, color, sort_order) VALUES (9, 'Παιδιά στο τραπέζι', '👶', '#06b6d4', 9)",
|
||||
"INSERT OR IGNORE INTO table_flag_defs (id, name, emoji, color, sort_order) VALUES (10, 'Επέτειος / Γενέθλια', '🎂', '#ec4899', 10)",
|
||||
# Default quick message templates
|
||||
"INSERT OR IGNORE INTO quick_message_templates (id, body, sort_order) VALUES (1, 'Σε χρειάζομαι τώρα', 1)",
|
||||
"INSERT OR IGNORE INTO quick_message_templates (id, body, sort_order) VALUES (2, 'Πάρε διάλειμμα', 2)",
|
||||
"INSERT OR IGNORE INTO quick_message_templates (id, body, sort_order) VALUES (3, 'Ετοιμάσου για κλείσιμο', 3)",
|
||||
"INSERT OR IGNORE INTO quick_message_templates (id, body, sort_order) VALUES (4, 'Ήρθε νέος πελάτης', 4)",
|
||||
"INSERT OR IGNORE INTO quick_message_templates (id, body, sort_order) VALUES (5, 'Ο πελάτης περιμένει να πληρώσει', 5)",
|
||||
]
|
||||
|
||||
for sql in seed_migrations:
|
||||
try:
|
||||
with engine.connect() as conn:
|
||||
conn.execute(text(sql))
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
print(f" [skip] {e}")
|
||||
print("\nRecreating schema (Alembic, incl. install defaults)...")
|
||||
upgrade_to_head()
|
||||
|
||||
print("\nDone. Database is clean and ready.")
|
||||
print("Run python demo_seed.py to populate with demo data.")
|
||||
|
||||
Reference in New Issue
Block a user