refactor: rename legacy database to .bak after copy

Per review: after copying the legacy install-dir database to the
effective user directory, rename the original to comfyui.db.bak so a
later launch without --user-directory cannot silently fall back to a
diverged copy, while keeping the file around for recovery. Also hoist
the database_default_path import to module scope.
This commit is contained in:
Constantine 2026-07-14 12:25:52 +08:00
parent c34b704f94
commit 2e1235222e
2 changed files with 7 additions and 5 deletions

View File

@ -4,7 +4,7 @@ import shutil
from app.logger import log_startup_warning
from utils.install_util import get_missing_requirements_message
from filelock import FileLock, Timeout
from comfy.cli_args import args
from comfy.cli_args import args, database_default_path
_DB_AVAILABLE = False
Session = None
@ -73,8 +73,6 @@ def get_database_url():
def get_legacy_default_db_path():
from comfy.cli_args import database_default_path
return database_default_path
@ -101,7 +99,10 @@ def copy_legacy_default_db(db_path):
return
shutil.copy(legacy_db_path, db_path)
logging.info(f"Copied legacy database from '{legacy_db_path}' to '{db_path}'")
os.replace(legacy_db_path, legacy_db_path + ".bak")
logging.info(
f"Copied legacy database from '{legacy_db_path}' to '{db_path}' and renamed the original to '{legacy_db_path}.bak'"
)
def prepare_file_db_path(db_path):

View File

@ -72,7 +72,8 @@ def test_legacy_default_database_is_copied_to_effective_user_directory(monkeypat
db.copy_legacy_default_db(str(user_dir / "comfyui.db"))
assert (user_dir / "comfyui.db").read_bytes() == b"legacy db"
assert legacy_db.read_bytes() == b"legacy db"
assert not legacy_db.exists()
assert legacy_db.with_suffix(".db.bak").read_bytes() == b"legacy db"
def test_legacy_default_database_does_not_overwrite_existing_effective_db(monkeypatch, tmp_path):