"""Migraciones de schema entre versiones de .vmsproj. Sprint 0 sólo tiene v1. Cuando agreguemos columnas o tablas en sprints futuros, agregamos funciones `_migrate_v1_to_v2(conn)`, etc. """ from __future__ import annotations import sqlite3 from datetime import UTC, datetime from vmssailor.version import VMSPROJ_SCHEMA_VERSION def current_schema_version(conn: sqlite3.Connection) -> int: """Devuelve la versión activa en este .vmsproj, o 0 si no hay tabla.""" row = conn.execute( "SELECT name FROM sqlite_master WHERE type='table' AND name='schema_version'" ).fetchone() if not row: return 0 row = conn.execute( "SELECT MAX(version) FROM schema_version" ).fetchone() return int(row[0]) if row and row[0] is not None else 0 def stamp_schema_version(conn: sqlite3.Connection, version: int) -> None: """Registra que el schema fue aplicado/migrado a `version`.""" ts = datetime.now(UTC).isoformat() conn.execute( "INSERT OR REPLACE INTO schema_version(version, applied_at) VALUES (?, ?)", (version, ts), ) def migrate_to_latest(conn: sqlite3.Connection) -> None: """Migra el schema desde su versión actual hasta `VMSPROJ_SCHEMA_VERSION`. En Sprint 0 sólo aplicamos el schema inicial v1. """ current = current_schema_version(conn) if current >= VMSPROJ_SCHEMA_VERSION: return # Sprint 0: una sola versión, no hay migraciones intermedias. stamp_schema_version(conn, VMSPROJ_SCHEMA_VERSION)