Skip to content

Commit

Permalink
Refuse to open old databases (except to migrate)
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Jan 15, 2024
1 parent 76af052 commit e0e3cb2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions damnit/backend/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def db_path(root_path: Path):
DATA_FORMAT_VERSION = 1

class DamnitDB:
def __init__(self, path=DB_NAME):
def __init__(self, path=DB_NAME, allow_old=False):
db_existed = path.exists()
log.debug("Opening database at %s", path)
self.conn = sqlite3.connect(path, timeout=30)
Expand Down Expand Up @@ -100,10 +100,15 @@ def __init__(self, path=DB_NAME):

if not db_existed:
# If this is a new database, set the latest current version
self.metameta["data_format_version"] = DATA_FORMAT_VERSION
elif db_existed and "data_format_version" not in self.metameta:
# Otherwise this is a legacy database
self.metameta["data_format_version"] = 0
db_version = self.metameta["data_format_version"] = DATA_FORMAT_VERSION
else:
db_version = self.metameta.setdefault("data_format_version", 0)

if (not allow_old) and db_version < DATA_FORMAT_VERSION:
raise RuntimeError(
f"Cannot open older (v{db_version}) database, please contact DA "
"for help migrating"
)

@classmethod
def from_dir(cls, path):
Expand Down
2 changes: 1 addition & 1 deletion damnit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def main():
from .backend.db import DamnitDB
from .migrations import migrate_images, migrate_v0_to_v1

db = DamnitDB()
db = DamnitDB(allow_old=True)

if args.migrate_subcmd == "images":
migrate_images(db, Path.cwd(), args.dry_run)
Expand Down

0 comments on commit e0e3cb2

Please sign in to comment.