#!/usr/bin/env python3 """ Migrate MariaDB export to SQLite for qaffee. Usage: python3 db/migrate_to_sqlite.py Creates data/qaffee.db ready for use with the Quarkus backend. Flyway will baseline at V4 on first startup (no re-migration needed). """ import re import sqlite3 import os import sys SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) REPO_ROOT = os.path.join(SCRIPT_DIR, '..') MIGRATION_DIR = os.path.join(REPO_ROOT, 'backend', 'src', 'main', 'resources', 'db', 'migration') EXPORT_FILE = os.path.join(SCRIPT_DIR, 'mariadb_export.sql') OUTPUT_FILE = os.path.join(REPO_ROOT, 'data', 'qaffee.db') # V2 (seed data) is intentionally skipped – it inserts dev/test rows that # conflict with production IDs in the export. MIGRATIONS = [ 'V1__initial_schema.sql', 'V3__company_logo.sql', 'V4__tally_price_at_booking.sql', ] # Tables to import in FK-safe order (parents before children) DATA_TABLES = ['company', 'employee', 'product', 'access_link', 'tally_entry'] def apply_migrations(conn: sqlite3.Connection) -> None: for filename in MIGRATIONS: path = os.path.join(MIGRATION_DIR, filename) with open(path, 'r', encoding='utf-8') as f: sql = f.read() conn.executescript(sql) print(f' schema {filename}') def mariadb_to_sqlite_sql(sql: str) -> str: # Remove MariaDB sandbox-mode comment at the top sql = re.sub(r'/\*M!.*?\*/', '', sql, flags=re.DOTALL) # Strip backtick identifier quotes sql = sql.replace('`', '') # Convert 0xABCD hex literals to SQLite X'ABCD' blob literals sql = re.sub(r'\b0x([0-9A-Fa-f]+)', r"X'\1'", sql) return sql def extract_insert(sql: str, table: str) -> str | None: """Return the full INSERT INTO