feat(database): add managed backup and offline restore (#6359)

This commit is contained in:
InfinityPacer
2026-08-19 17:34:32 +08:00
committed by GitHub
parent f4a879b3de
commit f700db5ac8
27 changed files with 1576 additions and 80 deletions
+40
View File
@@ -0,0 +1,40 @@
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock
from app.application.database import get_database_governance
from app.startup import database as startup_database
def test_builder_uses_cached_engine_as_database_fact_source(monkeypatch) -> None:
engine = SimpleNamespace(dialect=SimpleNamespace(name="sqlite"))
backend = Mock(db_type="sqlite", suffix=".db")
sqlite_backend = Mock(return_value=backend)
monkeypatch.setattr(startup_database, "get_engine", lambda: engine)
monkeypatch.setattr(startup_database, "SQLiteBackupBackend", sqlite_backend)
monkeypatch.setattr(startup_database.settings, "DB_TYPE", "postgresql")
startup_database.build_database_governance()
sqlite_backend.assert_called_once_with(engine)
def test_backup_policy_reads_current_path_and_retention(tmp_path: Path, monkeypatch) -> None:
monkeypatch.setattr(startup_database.settings, "DB_BACKUP_PATH", str(tmp_path))
monkeypatch.setattr(startup_database.settings, "DB_BACKUP_RETENTION_DAYS", 7)
monkeypatch.setattr(startup_database.settings, "DB_BACKUP_MAX_COUNT", 5)
policy = startup_database.read_backup_policy()
assert policy.root == tmp_path
assert policy.retention_days == 7
assert policy.max_count == 5
def test_configure_registers_one_database_governance(monkeypatch) -> None:
governance = Mock()
monkeypatch.setattr(startup_database, "build_database_governance", lambda: governance)
startup_database.configure_database()
assert get_database_governance() is governance