Skip to content

Commit

Permalink
Minor cleanup of backup_db().
Browse files Browse the repository at this point in the history
  • Loading branch information
liffiton committed Nov 8, 2024
1 parent f775f86 commit b04347f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/gened/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def main() -> str:
@bp.route("/get_db")
def get_db_file() -> Response:
db_backup_file = NamedTemporaryFile()
backup_db(db_backup_file.name)
backup_db(Path(db_backup_file.name))
db_name = current_app.config['DATABASE_NAME']
db_basename = Path(db_name).stem
dl_name = f"{db_basename}_{date.today().strftime('%Y%m%d')}.db"
Expand Down
7 changes: 3 additions & 4 deletions src/gened/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ def get_db() -> sqlite3.Connection:
return g.db


def backup_db(target: str | Path) -> None:
def backup_db(target: Path) -> None:
""" Safely make a backup of the database to the given path.
target: str or any path-like object. Must not exist yet or be empty.
target: Path object to the location of the new backup. Must not exist yet or be empty.
"""
target = Path(target)
if target.exists() and target.stat().st_size > 0:
raise FileExistsError(errno.EEXIST, "File already exists or is not empty", target)
raise FileExistsError(errno.EEXIST, "File already exists and is not empty", target)

db = get_db()
tmp_db = sqlite3.connect(target)
Expand Down

0 comments on commit b04347f

Please sign in to comment.