Add db backup task
Close #w
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,3 +5,4 @@ test.php
|
||||
database.db*
|
||||
.idea
|
||||
db_fixtures
|
||||
backups
|
||||
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "meal-manager"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = "~=3.13.0"
|
||||
@@ -25,7 +25,7 @@ dev = [
|
||||
|
||||
[project.scripts]
|
||||
apply-subscriptions = "meal_manager.scripts:apply_subscriptions_cli"
|
||||
|
||||
meal-manager-nightly = "meal_manager.scripts:run_nightly_tasks"
|
||||
|
||||
[tool.isort]
|
||||
profile = "black"
|
||||
|
||||
@@ -1,13 +1,36 @@
|
||||
import argparse
|
||||
import datetime
|
||||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy import Date, cast, func, select
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from meal_manager.main import engine
|
||||
from meal_manager.main import engine, sqlite_file_name
|
||||
from meal_manager.models import Event, Registration, Subscription
|
||||
|
||||
|
||||
def backup_db():
|
||||
backup_dir = Path(os.environ.get("MEAL_MANAGER_BACKUP_DIR", None))
|
||||
shutil.copy2(
|
||||
sqlite_file_name,
|
||||
backup_dir / f"{sqlite_file_name}.{datetime.datetime.now().isoformat()}",
|
||||
)
|
||||
# TODO: Delete old backups
|
||||
|
||||
|
||||
def run_nightly_tasks():
|
||||
print("Applying Subscriptions")
|
||||
with Session(engine) as session:
|
||||
apply_subscriptions(session)
|
||||
|
||||
print("Running db backup")
|
||||
backup_db()
|
||||
|
||||
print("Done running nightly tasks.")
|
||||
|
||||
|
||||
def apply_subscriptions(session: Session, event: Event = None, dry_run: bool = False):
|
||||
|
||||
subscriptions = session.scalars(select(Subscription)).all()
|
||||
|
||||
Reference in New Issue
Block a user