✨ Migrations versionnees de la base et de conf.json (schema_migrations + cle version)
This commit is contained in:
1 parent
3a75527135
commit
c7344f8988
5 files changed
+325
-28
No files matched your search
+24
-7
@@ -53,6 +53,10 @@ def build_base_url(url: str | None, port: int | None = None) -> str | None:
|
||||
|
||||
|
||||
class AppConfig(BaseModel):
|
||||
# Version de la structure du fichier, migree au demarrage
|
||||
# (voir app/migrations.py). Doit rester synchrone avec
|
||||
# current_conf_version(): un test verifie cet invariant.
|
||||
version: int = 1
|
||||
radarr: ApiServiceConfig = Field(default_factory=ApiServiceConfig)
|
||||
sonarr: ApiServiceConfig = Field(default_factory=ApiServiceConfig)
|
||||
prowlarr: ApiServiceConfig = Field(default_factory=ApiServiceConfig)
|
||||
@@ -108,22 +112,31 @@ def reset_test(service: ApiServiceConfig | QbittorrentConfig) -> None:
|
||||
service.test = TestState()
|
||||
|
||||
|
||||
def read_config() -> AppConfig:
|
||||
def read_raw_config() -> dict | None:
|
||||
"""Contenu brut de conf.json (dict), ou None s'il est absent.
|
||||
|
||||
Sert aux migrations, qui transforment le fichier avant validation
|
||||
pydantic. Un contenu invalide (non-dict) est traite comme absent.
|
||||
"""
|
||||
path = config_path()
|
||||
if not path.exists():
|
||||
return AppConfig()
|
||||
return None
|
||||
raw = json.loads(path.read_text(encoding="utf-8"))
|
||||
if not isinstance(raw, dict):
|
||||
return raw if isinstance(raw, dict) else None
|
||||
|
||||
|
||||
def read_config() -> AppConfig:
|
||||
raw = read_raw_config()
|
||||
if raw is None:
|
||||
return AppConfig()
|
||||
return AppConfig.model_validate(raw)
|
||||
|
||||
|
||||
def write_config(config: AppConfig) -> None:
|
||||
def write_raw_config(data: dict) -> None:
|
||||
"""Ecriture atomique du contenu brut de conf.json."""
|
||||
path = config_path()
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
serialized = config.model_dump(mode="json", exclude_none=True)
|
||||
|
||||
# Atomic write to avoid corrupting conf.json if the process is interrupted.
|
||||
tmp_dir = str(path.parent)
|
||||
with tempfile.NamedTemporaryFile(
|
||||
@@ -134,9 +147,13 @@ def write_config(config: AppConfig) -> None:
|
||||
suffix=".tmp",
|
||||
delete=False,
|
||||
) as handle:
|
||||
json.dump(serialized, handle, indent=2, ensure_ascii=False)
|
||||
json.dump(data, handle, indent=2, ensure_ascii=False)
|
||||
handle.write("\n")
|
||||
tmp_name = handle.name
|
||||
|
||||
os.replace(tmp_name, path)
|
||||
|
||||
|
||||
def write_config(config: AppConfig) -> None:
|
||||
write_raw_config(config.model_dump(mode="json", exclude_none=True))
|
||||
|
||||
Reference in new issue
Block a user