Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refuse to open old databases (except to migrate) #166

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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