Files
bonaminandClaude Opus 5.5 d3d328804e 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>
2026-11-12 16:54:15 +02:00

56 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
"""
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
Safe to run even if the backend is stopped. Does NOT delete the .db file —
it drops all tables and recreates them so the file path stays intact.
After running this, run demo_seed.py to populate with demo data.
"""
import sys
# ── Confirmation prompt ───────────────────────────────────────────────────────
print("=" * 60)
print(" WARNING: This will PERMANENTLY DELETE all data in the")
print(" POS database including orders, products, users, etc.")
print("=" * 60)
answer = input("\nType YES to confirm: ").strip()
if answer != "YES":
print("Aborted.")
sys.exit(0)
# ── Imports (after confirmation, so we don't waste time if user bails) ────────
from sqlalchemy import text, inspect
from database import engine
from core.schema import upgrade_to_head
print("\nDropping all tables...")
# SQLite doesn't support DROP TABLE ... CASCADE, so we disable FK checks,
# drop every table we know about, then re-enable them.
with engine.connect() as conn:
conn.execute(text("PRAGMA foreign_keys = OFF"))
conn.commit()
# Reflect the actual DB to catch any tables we might have missed in models
inspector = inspect(engine)
existing_tables = inspector.get_table_names()
# Drop in reverse dependency order using reflected metadata
with engine.connect() as conn:
for table_name in existing_tables:
conn.execute(text(f"DROP TABLE IF EXISTS [{table_name}]"))
print(f" dropped: {table_name}")
conn.execute(text("PRAGMA foreign_keys = ON"))
conn.commit()
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.")