DB backup script and alembic auto-backup hook #6
1 changed files with 24 additions and 0 deletions
|
|
@ -1,4 +1,6 @@
|
||||||
|
import subprocess
|
||||||
from logging.config import fileConfig
|
from logging.config import fileConfig
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from sqlalchemy import engine_from_config, pool
|
from sqlalchemy import engine_from_config, pool
|
||||||
|
|
||||||
|
|
@ -7,6 +9,28 @@ from alembic import context
|
||||||
from quartermaster.config import DB_URL
|
from quartermaster.config import DB_URL
|
||||||
from quartermaster.models import Base
|
from quartermaster.models import Base
|
||||||
|
|
||||||
|
|
||||||
|
def _backup_before_migrations() -> None:
|
||||||
|
script = Path(__file__).resolve().parents[1] / "scripts" / "backup-db.sh"
|
||||||
|
if not script.is_file():
|
||||||
|
return
|
||||||
|
result = subprocess.run(
|
||||||
|
[str(script), "alembic"],
|
||||||
|
check=False,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
if result.stdout:
|
||||||
|
print(result.stdout, end="")
|
||||||
|
if result.returncode != 0 and result.stderr:
|
||||||
|
print(
|
||||||
|
f"alembic: backup-db.sh returned {result.returncode}; "
|
||||||
|
f"continuing without a fresh backup.\n{result.stderr}",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
_backup_before_migrations()
|
||||||
|
|
||||||
config = context.config
|
config = context.config
|
||||||
config.set_main_option("sqlalchemy.url", DB_URL)
|
config.set_main_option("sqlalchemy.url", DB_URL)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue