64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Genera app/src/main/assets/frases.db (BD maestra) desde res/raw.
|
|
|
|
Fuente de verdad: res/raw/sqlfrases y res/raw/sqlautores (INSERTs).
|
|
Los IDs se respetan (mismo orden de inserción), por eso los favoritos y
|
|
días históricos de la BD de usuario sobreviven a las actualizaciones.
|
|
|
|
Al añadir/cambiar frases: editar los raw y subir MASTER_DB_VERSION
|
|
(la app recopia el maestro cuando el user_version del asset es mayor).
|
|
|
|
Uso: python3 tools/build_master_db.py
|
|
"""
|
|
import sqlite3
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
RAW = ROOT / "app/src/main/res/raw"
|
|
OUT = ROOT / "app/src/main/assets/frases.db"
|
|
|
|
# Subir al cambiar el contenido de frases/autores
|
|
MASTER_DB_VERSION = 1
|
|
|
|
SCHEMA = """
|
|
CREATE TABLE frases (
|
|
`_id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
`Frase` TEXT NOT NULL UNIQUE,
|
|
`AutorID` INTEGER
|
|
);
|
|
CREATE TABLE autores (
|
|
`_id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
`Autor` TEXT UNIQUE
|
|
);
|
|
"""
|
|
|
|
|
|
def load_statements(path: Path) -> list[str]:
|
|
# Los raw separan statements con '|', igual que hacía DatabaseHelper.onCreate
|
|
return [s.strip() for s in path.read_text(encoding="utf-8").split("|") if s.strip()]
|
|
|
|
|
|
def main() -> None:
|
|
OUT.parent.mkdir(parents=True, exist_ok=True)
|
|
if OUT.exists():
|
|
OUT.unlink()
|
|
con = sqlite3.connect(OUT)
|
|
try:
|
|
con.executescript(SCHEMA)
|
|
for stmt in load_statements(RAW / "sqlfrases"):
|
|
con.execute(stmt)
|
|
for stmt in load_statements(RAW / "sqlautores"):
|
|
con.execute(stmt)
|
|
con.execute(f"PRAGMA user_version = {MASTER_DB_VERSION}")
|
|
con.commit()
|
|
n_frases = con.execute("SELECT COUNT(*) FROM frases").fetchone()[0]
|
|
n_autores = con.execute("SELECT COUNT(*) FROM autores").fetchone()[0]
|
|
finally:
|
|
con.close()
|
|
print(f"OK {OUT}: {n_frases} frases, {n_autores} autores, user_version={MASTER_DB_VERSION}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|