Files
ComfyUI/tests-unit/app_test/database_path_test.py
Simon Pinfold 1d2ea2948d Turn the assets system on by default; add --disable-assets (#16884)
* Turn the assets system on by default; add --disable-assets

Assets are on unless ComfyUI is started with --disable-assets; --enable-assets
is still accepted, does nothing and logs one deprecation line.

Startup failures that already stopped ComfyUI with assets on now log
"ASSETS_STARTUP_FAILED: <kind>" and a message naming the database file and
the fix (another ComfyUI or program holding it, corrupt, written by a newer
ComfyUI, not writable, a file in the way, an unsupported --database-url),
each ending with --disable-assets. Missing database packages stop startup
with the install command instead of quietly turning assets off.

A read-only database that needs an upgrade is reported before it's opened
or backed up, so no read-only copies are left behind. A % in the install or
database path works, and the default database URL is built by SQLAlchemy.

* Read the stored revision through the WAL after an unclean shutdown; point a bad --database-url back to the default

* Build the suggested --database-url with SQLAlchemy; mention programs holding the file; log the full error at DEBUG

The suggested URL is quoted the same way as the default one, so a %20 or ?
in the path opens the intended file. A file held open by another program
(SQLite reports it as unable to open) gets that in the advice. An
unrecognised startup failure logs its traceback at DEBUG, as its message
says, except for an unsupported --database-url, whose error repeats the URL.

* Read through the WAL only when its -shm is there too, so the check never creates one

* Quote only what SQLAlchemy reads as URL syntax; report a corrupt read-only database as corrupt

A suggested --database-url keeps a plain path as typed, so pasting it into a
.bat file can't expand a quoted ':' or '\' as a batch argument. With
SQLAlchemy 2.1+, % ? and # are still quoted so the URL opens that file.
The pre-upgrade revision read lets a corrupt database raise, so it is
reported as corrupt rather than as not writable.

* Stub the revision read in the legacy-copy lock test, whose copied file isn't a real database

* Drop the absolute --database-url example; build the default URL with SQLAlchemy again

The failure messages go back to the generic --database-url example, so the
helper and the version-dependent quoting that kept its pasted form plain
are gone; the default database URL is built by URL.create again. Also tests
a read-only -wal left by a run that didn't shut down.

* Remove the pre-upgrade read-only check

A read-only database that needs an upgrade fails in the upgrade again, with
the not-writable message. The check and its tests move to a follow-up.
2026-10-09 00:39:49 -07:00

191 lines
6.6 KiB
Python

import os
import shutil
import sqlite3
from contextlib import closing
from app.database import db
from comfy.cli_args import database_default_path
def test_default_database_url_uses_effective_user_directory(monkeypatch, tmp_path):
user_dir = tmp_path / "custom_user"
user_dir.mkdir()
monkeypatch.setattr(db.args, "database_url", None)
monkeypatch.setattr("folder_paths.get_user_directory", lambda: str(user_dir))
assert db.get_db_path() == str(user_dir / "comfyui.db")
def test_default_db_path_matches_legacy_default_without_custom_user_directory(monkeypatch):
import folder_paths
monkeypatch.setattr(db.args, "database_url", None)
monkeypatch.setattr(
"folder_paths.get_user_directory",
lambda: os.path.join(folder_paths.base_path, "user"),
)
assert os.path.abspath(db.get_db_path()) == database_default_path
def test_explicit_database_url_is_preserved(monkeypatch):
database_url = "sqlite:///:memory:"
monkeypatch.setattr(db.args, "database_url", database_url)
assert db.get_database_url() == database_url
def test_explicit_url_at_legacy_default_location_is_honoured(monkeypatch):
url = f"sqlite:///{database_default_path}"
monkeypatch.setattr(db.args, "database_url", url)
assert db.get_database_url() == url
assert db.get_db_path() == database_default_path
def test_explicit_database_url_skips_legacy_copy(monkeypatch, tmp_path):
legacy_db = tmp_path / "install" / "user" / "comfyui.db"
target_db = tmp_path / "target" / "comfyui.db"
legacy_db.parent.mkdir(parents=True)
target_db.parent.mkdir(parents=True)
legacy_db.write_bytes(b"legacy db")
monkeypatch.setattr(db.args, "database_url", f"sqlite:///{target_db}")
monkeypatch.setattr(db, "get_legacy_default_db_path", lambda: str(legacy_db))
db.copy_legacy_default_db(str(target_db))
assert not target_db.exists()
def test_legacy_default_database_is_copied_to_effective_user_directory(monkeypatch, tmp_path):
legacy_db = tmp_path / "install" / "user" / "comfyui.db"
user_dir = tmp_path / "custom_user"
legacy_db.parent.mkdir(parents=True)
user_dir.mkdir()
legacy_db.write_bytes(b"legacy db")
monkeypatch.setattr(db.args, "database_url", None)
monkeypatch.setattr("folder_paths.get_user_directory", lambda: str(user_dir))
monkeypatch.setattr(db, "get_legacy_default_db_path", lambda: str(legacy_db))
db.copy_legacy_default_db(str(user_dir / "comfyui.db"))
assert (user_dir / "comfyui.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_copy_skipped_when_backup_already_exists(monkeypatch, tmp_path):
legacy_db = tmp_path / "install" / "user" / "comfyui.db"
backup_db = tmp_path / "install" / "user" / "comfyui.db.bak"
user_db = tmp_path / "custom_user" / "comfyui.db"
legacy_db.parent.mkdir(parents=True)
user_db.parent.mkdir(parents=True)
legacy_db.write_bytes(b"live db")
backup_db.write_bytes(b"old backup")
monkeypatch.setattr(db.args, "database_url", None)
monkeypatch.setattr(db, "get_legacy_default_db_path", lambda: str(legacy_db))
db.copy_legacy_default_db(str(user_db))
assert not user_db.exists()
assert legacy_db.read_bytes() == b"live db"
assert backup_db.read_bytes() == b"old backup"
def test_legacy_default_database_does_not_overwrite_existing_effective_db(monkeypatch, tmp_path):
legacy_db = tmp_path / "install" / "user" / "comfyui.db"
user_db = tmp_path / "custom_user" / "comfyui.db"
legacy_db.parent.mkdir(parents=True)
user_db.parent.mkdir(parents=True)
legacy_db.write_bytes(b"legacy db")
user_db.write_bytes(b"user db")
monkeypatch.setattr(db.args, "database_url", None)
monkeypatch.setattr(db, "get_legacy_default_db_path", lambda: str(legacy_db))
db.copy_legacy_default_db(str(user_db))
assert user_db.read_bytes() == b"user db"
assert legacy_db.read_bytes() == b"legacy db"
def test_prepare_file_database_creates_parent_directory(monkeypatch, tmp_path):
db_path = tmp_path / "nested" / "comfyui.db"
monkeypatch.setattr(db.args, "database_url", None)
db.prepare_file_db_path(str(db_path))
assert db_path.parent.is_dir()
def test_prepare_file_database_accepts_relative_database_path(monkeypatch, tmp_path):
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(db.args, "database_url", "sqlite:///relative.db")
db.prepare_file_db_path("relative.db")
assert os.getcwd() == str(tmp_path)
assert not list(tmp_path.iterdir())
def test_legacy_copy_includes_rows_still_in_the_wal(monkeypatch, tmp_path):
legacy_db = tmp_path / "install" / "user" / "comfyui.db"
user_db = tmp_path / "custom_user" / "comfyui.db"
legacy_db.parent.mkdir(parents=True)
user_db.parent.mkdir(parents=True)
writer = sqlite3.connect(legacy_db)
writer.execute("PRAGMA journal_mode=WAL")
writer.execute("PRAGMA wal_autocheckpoint=0")
writer.execute("CREATE TABLE t (x)")
writer.execute("INSERT INTO t VALUES (1)")
writer.commit()
# An unclean exit: the committed rows stay in comfyui.db-wal.
shutil.copy(legacy_db, tmp_path / "main-only.db")
shutil.copy(str(legacy_db) + "-wal", tmp_path / "saved-wal")
writer.close()
shutil.copy(tmp_path / "main-only.db", legacy_db)
shutil.copy(tmp_path / "saved-wal", str(legacy_db) + "-wal")
monkeypatch.setattr(db.args, "database_url", None)
monkeypatch.setattr(db, "get_legacy_default_db_path", lambda: str(legacy_db))
db.copy_legacy_default_db(str(user_db))
with closing(sqlite3.connect(user_db)) as copied:
assert copied.execute("SELECT x FROM t").fetchall() == [(1,)]
def test_legacy_copy_skipped_when_its_wal_cannot_be_checkpointed(monkeypatch, tmp_path):
legacy_db = tmp_path / "install" / "user" / "comfyui.db"
user_db = tmp_path / "custom_user" / "comfyui.db"
legacy_db.parent.mkdir(parents=True)
user_db.parent.mkdir(parents=True)
writer = sqlite3.connect(legacy_db)
writer.execute("PRAGMA journal_mode=WAL")
writer.execute("PRAGMA wal_autocheckpoint=0")
writer.execute("CREATE TABLE t (x)")
writer.execute("INSERT INTO t VALUES (1)")
writer.commit()
reader = sqlite3.connect(legacy_db, isolation_level=None)
reader.execute("BEGIN")
reader.execute("SELECT x FROM t").fetchall() # an open read pins the WAL
monkeypatch.setattr(db.args, "database_url", None)
monkeypatch.setattr(db, "get_legacy_default_db_path", lambda: str(legacy_db))
try:
db.copy_legacy_default_db(str(user_db))
finally:
reader.close()
writer.close()
assert not user_db.exists()
assert legacy_db.exists()