🔧 Migration base v2: colonnes created_at/last_seen_at absentes des bases tres anciennes
This commit is contained in:
1 parent
6191b32bf7
commit
c793411595
2 files changed
+79
-4
No files matched your search
@@ -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]:
|
def run_db_migrations(engine: Engine) -> list[str]:
|
||||||
"""Applique les migrations non jouees; renvoie leurs descriptions."""
|
"""Applique les migrations non jouees; renvoie leurs descriptions."""
|
||||||
with engine.begin() as conn:
|
with engine.begin() as conn:
|
||||||
|
|||||||
@@ -42,7 +42,10 @@ def test_db_migration_from_legacy_schema(tmp_path) -> None:
|
|||||||
|
|
||||||
applied = run_db_migrations(engine)
|
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")}
|
columns = {col["name"] for col in inspect(engine).get_columns("iso_movies")}
|
||||||
assert {
|
assert {
|
||||||
"processed_at",
|
"processed_at",
|
||||||
@@ -51,6 +54,7 @@ def test_db_migration_from_legacy_schema(tmp_path) -> None:
|
|||||||
"last_error",
|
"last_error",
|
||||||
"poster_url",
|
"poster_url",
|
||||||
"radarr_linked",
|
"radarr_linked",
|
||||||
|
"created_at",
|
||||||
} <= columns
|
} <= columns
|
||||||
|
|
||||||
# Les donnees existantes sont conservees.
|
# 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")))
|
rows = list(conn.execute(text("SELECT title, radarr_linked FROM iso_movies")))
|
||||||
assert rows == [("ISO Movie", 1)]
|
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:
|
with engine.connect() as conn:
|
||||||
recorded = list(conn.execute(text("SELECT version FROM schema_migrations")))
|
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) == []
|
assert run_db_migrations(engine) == []
|
||||||
|
|
||||||
|
|
||||||
@@ -73,7 +77,10 @@ def test_db_migration_fresh_database(tmp_path) -> None:
|
|||||||
|
|
||||||
applied = run_db_migrations(engine)
|
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) == []
|
assert run_db_migrations(engine) == []
|
||||||
|
|
||||||
|
|
||||||
@@ -136,3 +143,47 @@ def test_appconfig_version_matches_registry(monkeypatch) -> None:
|
|||||||
migrations conf.json."""
|
migrations conf.json."""
|
||||||
monkeypatch.setattr("app.migrations.CONF_MIGRATIONS", CONF_MIGRATIONS)
|
monkeypatch.setattr("app.migrations.CONF_MIGRATIONS", CONF_MIGRATIONS)
|
||||||
assert AppConfig().version == current_conf_version()
|
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)
|
||||||
Reference in new issue
Block a user