Skip to content

Fix default database path for custom user directory - #14539

Merged
guill merged 5 commits into
Comfy-Org:masterfrom
Constantine1916:fix/database-user-directory
Aug 24, 2026
Merged

Fix default database path for custom user directory#14539
guill merged 5 commits into
Comfy-Org:masterfrom
Constantine1916:fix/database-user-directory

Conversation

@Constantine1916

@Constantine1916 Constantine1916 commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #14524

Summary

  • Resolve the default SQLite database URL from the effective user directory instead of the install directory.
  • --database-url now defaults to None; a non-None value is treated as explicit at resolution time and used verbatim. db.get_database_url() is the accessor for the effective URL.
  • Copy the legacy default database into the effective user directory when the target database does not exist, then rename the original to comfyui.db.bak so it stays available for recovery but cannot be silently loaded again.

Notes

For users who did not explicitly pass --database-url and use --user-directory or --base-directory, the default DB location now follows the effective user directory. If an existing legacy install-directory user/comfyui.db is present and the new target DB is missing, it is copied once to avoid appearing to lose existing asset data, and the original is renamed to comfyui.db.bak.

Intentional behavior change: parent directories of the database path are now created automatically, including for explicit --database-url values (previously a missing parent directory failed at startup).

Testing

  • .venv/bin/python -m pytest tests-unit/app_test/test_migrations.py tests-unit/app_test/database_path_test.py -q
  • .venv/bin/python -m ruff check .

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

comfy/cli_args.py now defers the default database URL. app/database/db.py resolves the effective URL, derives SQLite paths, prepares file-backed database directories, and copies a legacy comfyui.db when needed before migrations. init_db() and _init_file_db() use these helpers, with unit tests covering URL resolution, legacy copying, overwrite protection, directory creation, and relative paths.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: moving the default database path to the custom user directory.
Description check ✅ Passed The description matches the PR and explains the database path fix, legacy migration, and directory creation changes.
Linked Issues check ✅ Passed The changes address #14524 by resolving the SQLite DB from the effective user/base directory instead of the install directory.
Out of Scope Changes check ✅ Passed No unrelated code changes are evident; the legacy migration and path setup are part of the stated fix.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Constantine1916
Constantine1916 force-pushed the fix/database-user-directory branch from 374bb9a to 9789de0 Compare June 26, 2026 03:08
@Constantine1916

Copy link
Copy Markdown
Contributor Author

Rebased this PR on the latest master and re-ran the related checks:

  • .venv/bin/python -m pytest tests-unit/app_test/test_migrations.py tests-unit/app_test/database_path_test.py -q
  • .venv/bin/python -m ruff check .

@Constantine1916

Copy link
Copy Markdown
Contributor Author

@guill @alexisrolland Hi, sorry for the ping — would either of you have a moment to take a look at this one? It fixes #14524 (the DB ending up in the install dir when --user-directory / --base-directory is set). CI is green and it's rebased on latest master. Happy to rework the approach if you'd prefer something different.

@guill

guill commented Jul 10, 2026

Copy link
Copy Markdown
Member

Looping in @mattmillerai and @synap5e who have been working in this space recently and may have opinions.

@Constantine1916

Copy link
Copy Markdown
Contributor Author

Thanks @guill. @mattmillerai @synap5e — the gist is: default DB follows --user-directory / --base-directory now, explicit --database-url is untouched, and a legacy install-dir comfyui.db gets copied over once so nobody loses asset data. Open to changing the approach if it gets in the way of what you're doing with assets.

@synap5e synap5e left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, agree the database should respect user dir and would want to merge a fix.
The overall approach is good: resolving the default lazily at init time (after apply_custom_paths() has run).

Couple of things to address.

1. database-url argument handling

Scanning sys.argv misses cases argparse itself handles (e.g. --database-u "abbreviated args") and regresses programmatic use + introduces a refactoring hazard.

Instead, I'd rather we use default=None on --database-url and treat args.database_url is not None as explicit at resolution time - probably mention the fallback in the help text. This is consistent with other path args (--user-directory, --output-directory), and removes the argv scan and the database_url_explicit attribute. database_default_path in cli_args.py then stops being the argparse default and can serve as the legacy path for the copy, instead of parsing it back out of a URL string.

Keeping args.database_url populated for compatibility: I'm concerned this is a footgun rather than a feature. Whenever this fix matters (custom user dir in effect), args.database_url holds a path the DB is not actually at, so anything reading it gets misled. None plus db.get_database_url() as the accessor would be preferred.

2. Legacy copy semantics

Copying preserves user-authored DB state, which feels correct, but it leaves two databases behind. Launch once more without --user-directory and you are silently back on an old diverged install-dir copy.

@guill what are your thoughts here?

I'm inclined to say lets keep as this PR proposes, it since that hazard is less bad than appearing to lose data.

Smaller notes

  • Parent directories are now auto-created for explicit --database-url values too, where before a missing parent failed at startup. Agree with this fix, just state it in the PR description as intentional.
  • Tests: worth adding a regression test that with no flags get_db_path() resolves to the same <install>/user/comfyui.db as before, and one that an explicit URL pointing at the old default location is honoured verbatim with no copy.

Per review: default --database-url to None and treat a non-None value
as explicit at resolution time. Removes the sys.argv scan and the
database_url_explicit attribute. database_default_path now serves
directly as the legacy copy source. Adds regression tests for the
unchanged no-flag default path and explicit URLs at the legacy
location.
@github-actions

github-actions Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

✅ All contributors have signed the CLA. Thank you! This PR is ready to be merged.
Posted by the CLA Assistant Lite bot.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
app/database/db.py (1)

65-72: 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Hoist the database-path imports to module scope. import folder_paths and from comfy.cli_args import database_default_path are only used in these helpers, and folder_paths.py doesn’t import app.database.db, so there’s no cycle preventing the move.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@app/database/db.py` around lines 65 - 72, Move the folder_paths and
database_default_path imports out of the get_database_url helper and into module
scope, then reuse those module-level symbols in the helper. Preserve the
existing database URL selection and fallback path behavior.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@app/database/db.py`:
- Around line 65-72: Move the folder_paths and database_default_path imports out
of the get_database_url helper and into module scope, then reuse those
module-level symbols in the helper. Preserve the existing database URL selection
and fallback path behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: c56fa556-65ef-4926-b230-25ee36b46778

📥 Commits

Reviewing files that changed from the base of the PR and between 9789de0 and c34b704.

📒 Files selected for processing (3)
  • app/database/db.py
  • comfy/cli_args.py
  • tests-unit/app_test/database_path_test.py
📜 Review details
⏰ Context from checks skipped due to timeout. (1)
  • GitHub Check: CodeRabbit
⚠️ CI failures not shown inline (2)

GitHub Actions: CLA Assistant / 0_cla-assistant.txt: Fix default database path for custom user directory

Conclusion: failure

View job details

##[group]Run contributor-assistant/github-action@ca4a40a7d1004f18d9960b404b97e5f30a505a08
 with:
   path-to-document: https://github.com/Comfy-Org/comfy-cla/blob/main/comfyui_icla.md
   remote-organization-name: comfy-org
   remote-repository-name: comfy-cla
   path-to-signatures: signatures/cla.json
   branch: main
   allowlist: action@github.com,actions-user,ampagent,claude,comfy-pr-bot,GitHub Action,github-actions,github-actions[bot],Glary Bot,Glary-Bot,*[bot]
   custom-notsigned-prcomment: 🎉 Thank you for your contribution, we really appreciate it! 🎉
Like many open source projects, we require contributors to sign our [Contributor License Agreement (CLA)](https://github.com/Comfy-Org/comfy-cla/blob/main/comfyui_icla.md). A CLA makes the ownership of contributions explicit, so contributors and the project share a clear understanding of how the code can be used. By signing, you:
- Confirm that you own your contribution.
- Keep the right to reuse your own code.
- Grant us a copyright license to include and share it within our projects.
CLAs are standard practice across major open source projects including those under the Apache Software Foundation and the Linux Foundation. Ours is based on the Apache Software Foundation's CLA. Most importantly, it would enable us to relicense the project under a more permissive license in the future, giving the project and its community greater flexibility.
✍ **To sign, please post a new comment on this PR with exactly the following text:** ✍
   custom-pr-sign-comment: I have read and agree to the Contributor License Agreement
   custom-allsigned-prcomment: ✅ All contributors have signed the CLA. Thank you! This PR is ready to be merged.
   use-dco-flag: false
   lock-pullrequest-aftermerge: true
   suggest-recheck: true
 env:
   GITHUB_***REDACTED***
   PERSONAL_ACCESS_***REDACTED***
 ##[endgroup]
 CLA Assistant GitHub Action bot has started the process
 (node:2112) [DEP0040] DeprecationWarning: The `punycode` module is deprec...

GitHub Actions: CLA Assistant / cla-assistant: Fix default database path for custom user directory

Conclusion: failure

View job details

##[group]Run contributor-assistant/github-action@ca4a40a7d1004f18d9960b404b97e5f30a505a08
 with:
   path-to-document: https://github.com/Comfy-Org/comfy-cla/blob/main/comfyui_icla.md
   remote-organization-name: comfy-org
   remote-repository-name: comfy-cla
   path-to-signatures: signatures/cla.json
   branch: main
   allowlist: action@github.com,actions-user,ampagent,claude,comfy-pr-bot,GitHub Action,github-actions,github-actions[bot],Glary Bot,Glary-Bot,*[bot]
   custom-notsigned-prcomment: 🎉 Thank you for your contribution, we really appreciate it! 🎉
Like many open source projects, we require contributors to sign our [Contributor License Agreement (CLA)](https://github.com/Comfy-Org/comfy-cla/blob/main/comfyui_icla.md). A CLA makes the ownership of contributions explicit, so contributors and the project share a clear understanding of how the code can be used. By signing, you:
- Confirm that you own your contribution.
- Keep the right to reuse your own code.
- Grant us a copyright license to include and share it within our projects.
CLAs are standard practice across major open source projects including those under the Apache Software Foundation and the Linux Foundation. Ours is based on the Apache Software Foundation's CLA. Most importantly, it would enable us to relicense the project under a more permissive license in the future, giving the project and its community greater flexibility.
✍ **To sign, please post a new comment on this PR with exactly the following text:** ✍
   custom-pr-sign-comment: I have read and agree to the Contributor License Agreement
   custom-allsigned-prcomment: ✅ All contributors have signed the CLA. Thank you! This PR is ready to be merged.
   use-dco-flag: false
   lock-pullrequest-aftermerge: true
   suggest-recheck: true
 env:
   GITHUB_***REDACTED***
   PERSONAL_ACCESS_***REDACTED***
 ##[endgroup]
 CLA Assistant GitHub Action bot has started the process
 (node:2112) [DEP0040] DeprecationWarning: The `punycode` module is deprec...
🧰 Additional context used
📓 Path-based instructions (3)
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

**/*.py: Keep imports at module scope; avoid inline imports unless they are already part of an established optional-backend probe or are needed to avoid an import cycle.
Do not add unnecessary try/except blocks; use them for optional dependency, platform, or backend capability detection only when the program has a useful fallback, and prefer specific exception types when changing new code.
If a library version is pinned in requirements.txt, do not add code to ComfyUI to handle older versions of that library.
Remove any workarounds for PyTorch versions that ComfyUI no longer officially supports; deprecated workarounds include catching an exception and rerunning the same op with the input cast to float unless the code comments name the exact supported PyTorch versions.
Let unsupported model formats, invalid quantization metadata, and bad states fail with clear errors instead of silently producing lower quality output.
Match the existing local style in the file you edit; long lines, simple helper functions, module-level state, and direct tensor operations are acceptable when they make the code easier to follow.
Keep comments sparse and useful; strip comments that restate the code or describe obvious behavior, and keep short TODOs only when they name the concrete missing follow-up.
Treat dtype, device placement, VRAM usage, and offloading behavior as core correctness concerns; check CPU, CUDA, ROCm, MPS, DirectML, XPU, NPU, and low-VRAM implications when touching shared execution or loading code.
Prefer native ComfyUI formats and existing quantization/offload helpers over adding parallel code paths; use comfy.quant_ops, comfy.model_management, comfy.memory_management, comfy.pinned_memory, comfy_aimdo, and comfy-kitchen helpers where they already solve the problem.
Use optimized comfy-kitchen ops in places where they improve performance without changing the expected dtype, device, memory, or interface behavior.
All models should use the optimized atte...

Files:

  • app/database/db.py
  • comfy/cli_args.py
  • tests-unit/app_test/database_path_test.py
**

⚙️ CodeRabbit configuration file

**: IMPORTANT: Only comment on issues directly introduced by this PR's code changes.
Treat AGENTS.md as mandatory repository policy, not optional style guidance.
Flag PR changes that violate AGENTS.md even when the code is otherwise functional.
In particular, enforce architecture boundaries, dtype/device/memory rules,
interface contracts, import style, no unnecessary try/except blocks, no inline
imports, no outbound internet paths in core ComfyUI, and narrow scoped fixes.
Prefer direct findings over suggestions when a rule is violated. Only ignore
AGENTS.md when it clearly conflicts with a newer explicit maintainer instruction
in the PR.
Do NOT flag pre-existing issues in code that was merely moved, re-indented,
de-indented, or reformatted without logic changes. If code appears in the diff
only due to whitespace or structural reformatting (e.g., removing a with: block),
treat it as unchanged. Contributors should not feel obligated to address
pre-existing issues outside the scope of their contribution.

Files:

  • app/database/db.py
  • comfy/cli_args.py
  • tests-unit/app_test/database_path_test.py
comfy/**

⚙️ CodeRabbit configuration file

comfy/**: Core ML/diffusion engine. Focus on:

  • Backward compatibility (breaking changes affect all custom nodes)
  • Memory management and GPU resource handling
  • Performance implications in hot paths
  • Thread safety for concurrent execution

Files:

  • comfy/cli_args.py
🧠 Learnings (2)
📚 Learning: 2026-02-21T14:01:41.482Z
Learnt from: pythongosssss
Repo: Comfy-Org/ComfyUI PR: 12555
File: comfy_extras/nodes_glsl.py:719-724
Timestamp: 2026-02-21T14:01:41.482Z
Learning: In PyOpenGL, bare Python scalars can be accepted for 1-element array parameters by NumberHandler. This means you can pass an int/float directly to OpenGL texture deletion (e.g., glDeleteTextures(tex)) without wrapping in a list. Verify function-specific expectations and ensure types match what the OpenGL call expects; use explicit lists only when the API requires an array.

Applied to files:

  • app/database/db.py
  • comfy/cli_args.py
  • tests-unit/app_test/database_path_test.py
📚 Learning: 2026-05-13T12:31:45.069Z
Learnt from: rattus128
Repo: Comfy-Org/ComfyUI PR: 13802
File: comfy/pinned_memory.py:19-30
Timestamp: 2026-05-13T12:31:45.069Z
Learning: When reviewing code that uses comfy/pinned_memory.py’s `HostBuffer.extend(size=..., reallocate=...)`: by default (`reallocate` is not True / False), `extend(size=...)` is a *relative increment* that grows the buffer by `size` bytes—so slicing like `[offset:offset+size]` after `hostbuf.extend(size=size)` is correct and the argument should not be rewritten to `offset + size`. Only in the single-segment reallocation mode (`reallocate=True`, e.g., as used by `resize_pin_buffer()` in `comfy/model_management.py`) should `size` be treated as an *absolute target* and the call/arguments should be checked accordingly.

Applied to files:

  • comfy/cli_args.py
🔇 Additional comments (4)
comfy/cli_args.py (1)

241-241: LGTM!

app/database/db.py (2)

89-104: LGTM!

The copy logic is well-guarded: skips on explicit URL, avoids self-copy via abspath comparison, and preserves existing target DB. The ordering in _init_file_db (prepare → existence check) correctly ensures a copied legacy DB is detected for incremental migration.


142-142: LGTM!

Using get_database_url() in init_db() and calling prepare_file_db_path() before the existence check in _init_file_db() are both correct changes.

Also applies to: 179-179

tests-unit/app_test/database_path_test.py (1)

1-114: LGTM!

Tests are thorough — covering default URL resolution, explicit URL preservation, legacy copy (skip/copy/no-overwrite), directory creation, and relative path handling. Assertions align with the implementation in db.py.

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 11, 2026
@Constantine1916

Copy link
Copy Markdown
Contributor Author

I have read and agree to the Contributor License Agreement

comfy-legal added a commit to Comfy-Org/comfy-cla that referenced this pull request Jul 11, 2026
@Constantine1916

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review @synap5e — all fair points. Pushed the rework: --database-url defaults to None now with args.database_url is not None as the explicit check, the argv scan and database_url_explicit are gone, and database_default_path is used directly as the legacy copy source. Also added the regression tests you suggested and noted the parent-dir creation in the description.

@Constantine1916
Constantine1916 requested a review from synap5e July 13, 2026 14:51
@guill

guill commented Jul 14, 2026

Copy link
Copy Markdown
Member

Copying preserves user-authored DB state, which feels correct, but it leaves two databases behind. Launch once more without --user-directory and you are silently back on an old diverged install-dir copy.

I think the standard solution for this is to rename the original database to <old_name>.bak. That way it's available if something went wrong, but won't get accidentally loaded down the road.

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
app/database/db.py (1)

87-105: 🗄️ Data Integrity & Integration | 🔴 Critical | 🏗️ Heavy lift

Data corruption and divergence risks during legacy database migration.

This migration logic has critical concurrency and atomicity flaws that can lead to database corruption, silent data divergence, or startup crashes:

  1. Legacy DB in use: If an existing ComfyUI process is actively using legacy_db_path, it will be renamed while in use. On Windows, os.replace will raise a PermissionError and crash. On Linux/macOS, the old process will continue writing to the .bak file while the new process uses the copied database, silently splitting user data across two files.
  2. Migration Race Condition (TOCTOU): If multiple instances start simultaneously with the same new db_path, they will race to copy and rename the file. This can lead to concurrent shutil.copy writes (corrupting the DB) or a FileNotFoundError during os.replace.
  3. Non-atomic Copy: If shutil.copy fails midway (e.g., out of disk space or process killed), a partially written db_path is left behind. On the next startup, it will skip the copy and attempt to load a corrupted SQLite file.
  4. Symlink Crash: If the user symlinked the legacy directory to the new user directory to manage space, os.path.abspath won't detect they are the same file, causing shutil.copy to crash with shutil.SameFileError.

Recommendation:
Acquire the legacy_db_path lock to ensure it's not in use, use os.path.realpath to resolve symlinks, perform the copy atomically via a temporary file, and ensure the caller (_init_file_db) has already acquired the lock for db_path.

🛡️ Proposed safe migration logic
def copy_legacy_default_db(db_path):
    if args.database_url is not None:
        return

    legacy_db_path = get_legacy_default_db_path()
    if legacy_db_path is None:
        return

    # Use realpath to avoid SameFileError if the user symlinked the directories
    if os.path.realpath(legacy_db_path) == os.path.realpath(db_path):
        return

    if os.path.exists(db_path) or not os.path.exists(legacy_db_path):
        return

    # 1. Ensure the legacy DB is not actively in use
    legacy_lock = FileLock(f"{legacy_db_path}.lock")
    try:
        legacy_lock.acquire(timeout=0)
    except Timeout:
        raise RuntimeError(f"Cannot migrate legacy database '{legacy_db_path}' because it is actively in use by another process.")

    try:
        # Note: The caller (_init_file_db) must acquire the lock for `db_path` BEFORE calling this function 
        # to prevent multiple instances from racing to perform this migration.
        
        # 2. Perform copy atomically to prevent partial writes
        temp_db_path = f"{db_path}.tmp"
        shutil.copy(legacy_db_path, temp_db_path)
        # (Optional: copy -wal and -shm files here if WAL mode is heavily relied upon)
        
        os.replace(temp_db_path, db_path)
        os.replace(legacy_db_path, f"{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'"
        )
    finally:
        legacy_lock.release()
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@app/database/db.py` around lines 87 - 105, Update copy_legacy_default_db to
resolve both paths with os.path.realpath, acquire and release a non-blocking
FileLock for the legacy database, and fail clearly when it is in use. Ensure
_init_file_db acquires the destination db_path lock before calling this
function, then copy to a temporary path and atomically replace the destination
before renaming the legacy file; preserve existing early-return conditions and
clean up temporary files on failure.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@app/database/db.py`:
- Around line 87-105: Update copy_legacy_default_db to resolve both paths with
os.path.realpath, acquire and release a non-blocking FileLock for the legacy
database, and fail clearly when it is in use. Ensure _init_file_db acquires the
destination db_path lock before calling this function, then copy to a temporary
path and atomically replace the destination before renaming the legacy file;
preserve existing early-return conditions and clean up temporary files on
failure.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 7a0b8878-d756-450d-87a8-dc97d27b7899

📥 Commits

Reviewing files that changed from the base of the PR and between c34b704 and 2e12352.

📒 Files selected for processing (2)
  • app/database/db.py
  • tests-unit/app_test/database_path_test.py
📜 Review details
🧰 Additional context used
📓 Path-based instructions (2)
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

**/*.py: Keep imports at module scope; avoid inline imports unless they are already part of an established optional-backend probe or are needed to avoid an import cycle.
Do not add unnecessary try/except blocks; use them for optional dependency, platform, or backend capability detection only when the program has a useful fallback, and prefer specific exception types when changing new code.
If a library version is pinned in requirements.txt, do not add code to ComfyUI to handle older versions of that library.
Remove any workarounds for PyTorch versions that ComfyUI no longer officially supports; deprecated workarounds include catching an exception and rerunning the same op with the input cast to float unless the code comments name the exact supported PyTorch versions.
Let unsupported model formats, invalid quantization metadata, and bad states fail with clear errors instead of silently producing lower quality output.
Match the existing local style in the file you edit; long lines, simple helper functions, module-level state, and direct tensor operations are acceptable when they make the code easier to follow.
Keep comments sparse and useful; strip comments that restate the code or describe obvious behavior, and keep short TODOs only when they name the concrete missing follow-up.
Treat dtype, device placement, VRAM usage, and offloading behavior as core correctness concerns; check CPU, CUDA, ROCm, MPS, DirectML, XPU, NPU, and low-VRAM implications when touching shared execution or loading code.
Prefer native ComfyUI formats and existing quantization/offload helpers over adding parallel code paths; use comfy.quant_ops, comfy.model_management, comfy.memory_management, comfy.pinned_memory, comfy_aimdo, and comfy-kitchen helpers where they already solve the problem.
Use optimized comfy-kitchen ops in places where they improve performance without changing the expected dtype, device, memory, or interface behavior.
All models should use the optimized atte...

Files:

  • tests-unit/app_test/database_path_test.py
  • app/database/db.py
**

⚙️ CodeRabbit configuration file

**: IMPORTANT: Only comment on issues directly introduced by this PR's code changes.
Treat AGENTS.md as mandatory repository policy, not optional style guidance.
Flag PR changes that violate AGENTS.md even when the code is otherwise functional.
In particular, enforce architecture boundaries, dtype/device/memory rules,
interface contracts, import style, no unnecessary try/except blocks, no inline
imports, no outbound internet paths in core ComfyUI, and narrow scoped fixes.
Prefer direct findings over suggestions when a rule is violated. Only ignore
AGENTS.md when it clearly conflicts with a newer explicit maintainer instruction
in the PR.
Do NOT flag pre-existing issues in code that was merely moved, re-indented,
de-indented, or reformatted without logic changes. If code appears in the diff
only due to whitespace or structural reformatting (e.g., removing a with: block),
treat it as unchanged. Contributors should not feel obligated to address
pre-existing issues outside the scope of their contribution.

Files:

  • tests-unit/app_test/database_path_test.py
  • app/database/db.py
🧠 Learnings (1)
📚 Learning: 2026-02-21T14:01:41.482Z
Learnt from: pythongosssss
Repo: Comfy-Org/ComfyUI PR: 12555
File: comfy_extras/nodes_glsl.py:719-724
Timestamp: 2026-02-21T14:01:41.482Z
Learning: In PyOpenGL, bare Python scalars can be accepted for 1-element array parameters by NumberHandler. This means you can pass an int/float directly to OpenGL texture deletion (e.g., glDeleteTextures(tex)) without wrapping in a list. Verify function-specific expectations and ensure types match what the OpenGL call expects; use explicit lists only when the API requires an array.

Applied to files:

  • tests-unit/app_test/database_path_test.py
  • app/database/db.py
🔇 Additional comments (1)
tests-unit/app_test/database_path_test.py (1)

75-76: LGTM!

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 14, 2026
@Constantine1916

Copy link
Copy Markdown
Contributor Author

Makes sense — done. The legacy DB is now renamed to comfyui.db.bak after the copy, so it can't be silently picked up again but is still there for recovery. Updated the test and PR description to match.

Comment thread app/database/db.py Outdated
Comment on lines +101 to +102
shutil.copy(legacy_db_path, db_path)
os.replace(legacy_db_path, legacy_db_path + ".bak")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user is running one comfyui with no user dir set and launches a second comfyui with an explicit user dir, the second instance could pick up the first's db as a "legacy" and try migrate it.

I think the cleanest fix would be add a guard on .bak existing and bail out of migration then. That way users can run arbitrary new --user-dir's and only the first-run would attempt to migrate. Minor ambiguity of intent on first run because we can't tell if they have always been using user-dir or if they want to create a fresh user dir, but not a blocker IMO.

Also I think cleaner to rename before copy - os.replace fails fast if another running instance holds the legacy DB open (at least on windows).

Suggested change
shutil.copy(legacy_db_path, db_path)
os.replace(legacy_db_path, legacy_db_path + ".bak")
backup_path = legacy_db_path + ".bak"
if os.path.exists(backup_path):
return
os.replace(legacy_db_path, backup_path)
shutil.copy(backup_path, db_path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than this LGTM

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on the two-instance scenario — applied your suggestion: bail out if .bak already exists, and rename before copy so os.replace fails fast if the legacy DB is held open. Added a test for the existing-.bak case.

Per review: bail out of the legacy migration when comfyui.db.bak
already exists, so only the first run migrates and later launches with
a fresh --user-directory cannot grab a database another instance is
using. Rename before copy so os.replace fails fast if the legacy DB is
held open by a running instance.
@Constantine1916

Copy link
Copy Markdown
Contributor Author

@synap5e thanks again for the approval! Is there anything else needed from my side to get this merged? Happy to rebase onto latest master if that helps.

@guill
guill merged commit 5f0c4e1 into Comfy-Org:master Aug 24, 2026
15 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

user-directory and base-directory args being ignored in DB setup

3 participants