From c7934115952d6cb7784858fc56109802002ddf88 Mon Sep 17 00:00:00 2001 From: Orionis312050 Date: Tue, 13 Oct 2026 20:34:49 +0000 Subject: [PATCH] :wrench: Migration base v2: colonnes created_at/last_seen_at absentes des bases tres anciennes --- app/migrations.py | 24 ++++++++++++++++ tests/test_migrations.py | 59 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/app/migrations.py b/app/migrations.py index c1372b1..6341d6d 100644 --- a/app/migrations.py +++ b/app/migrations.py @@ -73,6 +73,30 @@ DB_MIGRATIONS: list[DbMigration] = [ ] +def _upgrade_v2(conn: Connection) -> None: + """Colonnes apparues dans les modeles sans migration a l'epoque. + + Bases creees avant l'introduction de created_at / last_seen_at: elles + manquent alors que les autres migrations les considerent acquises. + """ + inspector = inspect(conn) + if not inspector.has_table("iso_movies"): + return + columns = {col["name"] for col in inspector.get_columns("iso_movies")} + for column, ddl in (("created_at", "DATETIME"), ("last_seen_at", "DATETIME")): + if column not in columns: + conn.execute(text(f"ALTER TABLE iso_movies ADD COLUMN {column} {ddl}")) + + +DB_MIGRATIONS.append( + DbMigration( + version=2, + description="iso_movies: colonnes created_at/last_seen_at sur les bases anciennes", + upgrade=_upgrade_v2, + ) +) + + def run_db_migrations(engine: Engine) -> list[str]: """Applique les migrations non jouees; renvoie leurs descriptions.""" with engine.begin() as conn: diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 4040746..1bff842 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -42,7 +42,10 @@ def test_db_migration_from_legacy_schema(tmp_path) -> None: applied = run_db_migrations(engine) - assert applied == ["iso_movies: colonnes de suivi des conversions"] + assert applied == [ + "iso_movies: colonnes de suivi des conversions", + "iso_movies: colonnes created_at/last_seen_at sur les bases anciennes", + ] columns = {col["name"] for col in inspect(engine).get_columns("iso_movies")} assert { "processed_at", @@ -51,6 +54,7 @@ def test_db_migration_from_legacy_schema(tmp_path) -> None: "last_error", "poster_url", "radarr_linked", + "created_at", } <= columns # Les donnees existantes sont conservees. @@ -58,10 +62,10 @@ def test_db_migration_from_legacy_schema(tmp_path) -> None: rows = list(conn.execute(text("SELECT title, radarr_linked FROM iso_movies"))) assert rows == [("ISO Movie", 1)] - # La version est tracee et la migration n'est pas rejouee. + # Les versions sont tracees et les migrations ne sont pas rejouees. with engine.connect() as conn: recorded = list(conn.execute(text("SELECT version FROM schema_migrations"))) - assert [row[0] for row in recorded] == [1] + assert [row[0] for row in recorded] == [1, 2] assert run_db_migrations(engine) == [] @@ -73,7 +77,10 @@ def test_db_migration_fresh_database(tmp_path) -> None: applied = run_db_migrations(engine) - assert applied == ["iso_movies: colonnes de suivi des conversions"] + assert applied == [ + "iso_movies: colonnes de suivi des conversions", + "iso_movies: colonnes created_at/last_seen_at sur les bases anciennes", + ] assert run_db_migrations(engine) == [] @@ -136,3 +143,47 @@ def test_appconfig_version_matches_registry(monkeypatch) -> None: migrations conf.json.""" monkeypatch.setattr("app.migrations.CONF_MIGRATIONS", CONF_MIGRATIONS) assert AppConfig().version == current_conf_version() + + +def test_db_migration_adds_created_at_on_very_old_database(tmp_path) -> None: + """Une base d'epoque, sans created_at/last_seen_at, est reparee par la + migration v2 et redevient utilisable par l'application.""" + from sqlalchemy import create_engine, inspect, text + + from app.database import Base + from app.migrations import run_db_migrations + + engine = create_engine(f"sqlite:///{tmp_path / 'old.db'}") + with engine.begin() as conn: + conn.execute( + text( + """ + CREATE TABLE iso_movies ( + id INTEGER PRIMARY KEY, + radarr_movie_id INTEGER, + radarr_movie_file_id INTEGER, + title TEXT, + year INTEGER, + path TEXT, + size INTEGER, + status TEXT + ) + """ + ) + ) + conn.execute( + text( + "INSERT INTO iso_movies (radarr_movie_id, radarr_movie_file_id," + " title, status) VALUES (1, 2, 'Vieux Film', 'detected')" + ) + ) + + run_db_migrations(engine) + + columns = {col["name"] for col in inspect(engine).get_columns("iso_movies")} + assert {"created_at", "last_seen_at", "radarr_linked", "last_error"} <= columns + with engine.connect() as conn: + rows = list(conn.execute(text("SELECT title, status FROM iso_movies"))) + assert rows == [("Vieux Film", "detected")] + # La table est conforme au modele: create_all ne change rien. + Base.metadata.create_all(bind=engine)