The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
Add support for sqlite database (#8444)
pythongosssss50c605e957e6
diff --git a/alembic.ini b/alembic.ininew file mode 100644index 000000000..12f18712f--- /dev/null+++ b/alembic.ini@@ -0,0 +1,84 @@+# A generic, single database configuration.++[alembic]+# path to migration scripts+# Use forward slashes (/) also on windows to provide an os agnostic path+script_location = alembic_db++# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s+# Uncomment the line below if you want the files to be prepended with date and time+# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file+# for all available tokens+# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s++# sys.path path, will be prepended to sys.path if present.+# defaults to the current working directory.+prepend_sys_path = .++# timezone to use when rendering the date within the migration file+# as well as the filename.+# If specified, requires the python>=3.9 or backports.zoneinfo library and tzdata library.+# Any required deps can installed by adding `alembic[tz]` to the pip requirements+# string value is passed to ZoneInfo()+# leave blank for localtime+# timezone =++# max length of characters to apply to the "slug" field+# truncate_slug_length = 40++# set to 'true' to run the environment during+# the 'revision' command, regardless of autogenerate+# revision_environment = false++# set to 'true' to allow .pyc and .pyo files without+# a source .py file to be detected as revisions in the+# versions/ directory+# sourceless = false++# version location specification; This defaults+# to alembic_db/versions. When using multiple version+# directories, initial revisions must be specified with --version-path.+# The path separator used here should be the separator specified by "version_path_separator" below.+# version_locations = %(here)s/bar:%(here)s/bat:alembic_db/versions++# version path separator; As mentioned above, this is the character used to split+# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.+# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.+# Valid values for version_path_separator are:+#+# version_path_separator = :+# version_path_separator = ;+# version_path_separator = space+# version_path_separator = newline+#+# Use os.pathsep. Default configuration used for new projects.+version_path_separator = os++# set to 'true' to search source files recursively+# in each "version_locations" directory+# new in Alembic version 1.10+# recursive_version_locations = false++# the output encoding used when revision files+# are written from script.py.mako+# output_encoding = utf-8++sqlalchemy.url = sqlite:///user/comfyui.db+++[post_write_hooks]+# post_write_hooks defines scripts or Python functions that are run+# on newly generated revision scripts. See the documentation for further+# detail and examples++# format using "black" - use the console_scripts runner, against the "black" entrypoint+# hooks = black+# black.type = console_scripts+# black.entrypoint = black+# black.options = -l 79 REVISION_SCRIPT_FILENAME++# lint with attempts to fix using "ruff" - use the exec runner, execute a binary+# hooks = ruff+# ruff.type = exec+# ruff.executable = %(here)s/.venv/bin/ruff+# ruff.options = check --fix REVISION_SCRIPT_FILENAMEdiff --git a/alembic_db/README.md b/alembic_db/README.mdnew file mode 100644index 000000000..3b808c7ca--- /dev/null+++ b/alembic_db/README.md@@ -0,0 +1,4 @@+## Generate new revision++1. Update models in `/app/database/models.py`+2. Run `alembic revision --autogenerate -m "{your message}"`diff --git a/alembic_db/env.py b/alembic_db/env.pynew file mode 100644index 000000000..4d7770679--- /dev/null+++ b/alembic_db/env.py@@ -0,0 +1,64 @@+from sqlalchemy import engine_from_config+from sqlalchemy import pool++from alembic import context++# this is the Alembic Config object, which provides+# access to the values within the .ini file in use.+config = context.config+++from app.database.models import Base+target_metadata = Base.metadata++# other values from the config, defined by the needs of env.py,+# can be acquired:+# my_important_option = config.get_main_option("my_important_option")+# ... etc.+++def run_migrations_offline() -> None:+ """Run migrations in 'offline' mode.+ This configures the context with just a URL+ and not an Engine, though an Engine is acceptable+ here as well. By skipping the Engine creation+ we don't even need a DBAPI to be available.+ Calls to context.execute() here emit the given string to the+ script output.+ """+ url = config.get_main_option("sqlalchemy.url")+ context.configure(+ url=url,+ target_metadata=target_metadata,+ literal_binds=True,+ dialect_opts={"paramstyle": "named"},+ )++ with context.begin_transaction():+ context.run_migrations()+++def run_migrations_online() -> None:+ """Run migrations in 'online' mode.+ In this scenario we need to create an Engine+ and associate a connection with the context.+ """+ connectable = engine_from_config(+ config.get_section(config.config_ini_section, {}),+ prefix="sqlalchemy.",+ poolclass=pool.NullPool,+ )++ with connectable.connect() as connection:+ context.configure(+ connection=connection, target_metadata=target_metadata+ )++ with context.begin_transaction():+ context.run_migrations()+++if context.is_offline_mode():+ run_migrations_offline()+else:+ run_migrations_online()diff --git a/alembic_db/script.py.mako b/alembic_db/script.py.makonew file mode 100644index 000000000..480b130d6--- /dev/null+++ b/alembic_db/script.py.mako@@ -0,0 +1,28 @@+"""${message}++Revision ID: ${up_revision}+Revises: ${down_revision | comma,n}+Create Date: ${create_date}++"""+from typing import Sequence, Union++from alembic import op+import sqlalchemy as sa+${imports if imports else ""}++# revision identifiers, used by Alembic.+revision: str = ${repr(up_revision)}+down_revision: Union[str, None] = ${repr(down_revision)}+branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}+depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}+++def upgrade() -> None:+ """Upgrade schema."""+ ${upgrades if upgrades else "pass"}+++def downgrade() -> None:+ """Downgrade schema."""+ ${downgrades if downgrades else "pass"}diff --git a/app/database/db.py b/app/database/db.pynew file mode 100644index 000000000..1de8b80ed--- /dev/null+++ b/app/database/db.py@@ -0,0 +1,112 @@+import logging+import os+import shutil+from app.logger import log_startup_warning+from utils.install_util import get_missing_requirements_message+from comfy.cli_args import args++_DB_AVAILABLE = False+Session = None+++try:+ from alembic import command+ from alembic.config import Config+ from alembic.runtime.migration import MigrationContext+ from alembic.script import ScriptDirectory+ from sqlalchemy import create_engine+ from sqlalchemy.orm import sessionmaker++ _DB_AVAILABLE = True+except ImportError as e:+ log_startup_warning(+ f"""+------------------------------------------------------------------------+Error importing dependencies: {e}+{get_missing_requirements_message()}+This error is happening because ComfyUI now uses a local sqlite database.+------------------------------------------------------------------------+""".strip()+ )+++def dependencies_available():+ """+ Temporary function to check if the dependencies are available+ """+ return _DB_AVAILABLE+++def can_create_session():+ """+ Temporary function to check if the database is available to create a session+ During initial release there may be environmental issues (or missing dependencies) that prevent the database from being created+ """+ return dependencies_available() and Session is not None+++def get_alembic_config():+ root_path = os.path.join(os.path.dirname(__file__), "../..")+ config_path = os.path.abspath(os.path.join(root_path, "alembic.ini"))+ scripts_path = os.path.abspath(os.path.join(root_path, "alembic_db"))++ config = Config(config_path)+ config.set_main_option("script_location", scripts_path)+ config.set_main_option("sqlalchemy.url", args.database_url)++ return config+++def get_db_path():+ url = args.database_url+ if url.startswith("sqlite:///"):+ return url.split("///")[1]+ else:+ raise ValueError(f"Unsupported database URL '{url}'.")+++def init_db():+ db_url = args.database_url+ logging.debug(f"Database URL: {db_url}")+ db_path = get_db_path()+ db_exists = os.path.exists(db_path)++ config = get_alembic_config()++ # Check if we need to upgrade+ engine = create_engine(db_url)+ conn = engine.connect()++ context = MigrationContext.configure(conn)+ current_rev = context.get_current_revision()++ script = ScriptDirectory.from_config(config)+ target_rev = script.get_current_head()++ if target_rev is None:+ logging.warning("No target revision found.")+ elif current_rev != target_rev:+ # Backup the database pre upgrade+ backup_path = db_path + ".bkp"+ if db_exists:+ shutil.copy(db_path, backup_path)+ else:+ backup_path = None++ try:+ command.upgrade(config, target_rev)+ logging.info(f"Database upgraded from {current_rev} to {target_rev}")+ except Exception as e:+ if backup_path:+ # Restore the database from backup if upgrade fails+ shutil.copy(backup_path, db_path)+ os.remove(backup_path)+ logging.exception("Error upgrading database: ")+ raise e++ global Session+ Session = sessionmaker(bind=engine)+++def create_session():+ return Session()diff --git a/app/database/models.py b/app/database/models.pynew file mode 100644index 000000000..6facfb8f2--- /dev/null+++ b/app/database/models.py@@ -0,0 +1,14 @@+from sqlalchemy.orm import declarative_base++Base = declarative_base()+++def to_dict(obj):+ fields = obj.__table__.columns.keys()+ return {+ field: (val.to_dict() if hasattr(val, "to_dict") else val)+ for field in fields+ if (val := getattr(obj, field))+ }++# TODO: Define models herediff --git a/app/frontend_management.py b/app/frontend_management.pyindex d9ef8c921..2b626f24e 100644--- a/app/frontend_management.py+++ b/app/frontend_management.py@@ -16,26 +16,17 @@ from importlib.metadata import versionimport requestsfrom typing_extensions import NotRequired+from utils.install_util import get_missing_requirements_message, requirements_path+from comfy.cli_args import DEFAULT_VERSION_STRINGimport app.logger-# The path to the requirements.txt file-req_path = Path(__file__).parents[1] / "requirements.txt"-def frontend_install_warning_message():- """The warning message to display when the frontend version is not up to date."""-- extra = ""- if sys.flags.no_user_site:- extra = "-s "return f"""-Please install the updated requirements.txt file by running:-{sys.executable} {extra}-m pip install -r {req_path}+{get_missing_requirements_message()}This error is happening because the ComfyUI frontend is no longer shipped as part of the main repo but as a pip package instead.--If you are on the portable package you can run: update\\update_comfyui.bat to solve this problem""".strip()@@ -48,7 +39,7 @@ def check_frontend_version():try:frontend_version_str = version("comfyui-frontend-package")frontend_version = parse_version(frontend_version_str)- with open(req_path, "r", encoding="utf-8") as f:+ with open(requirements_path, "r", encoding="utf-8") as f:required_frontend = parse_version(f.readline().split("=")[-1])if frontend_version < required_frontend:app.logger.log_startup_warning(diff --git a/comfy/cli_args.py b/comfy/cli_args.pyindex 4fb675f99..741ecac3f 100644--- a/comfy/cli_args.py+++ b/comfy/cli_args.py@@ -203,6 +203,11 @@ parser.add_argument(help="Set the base URL for the ComfyUI API. (default: https://api.comfy.org)",)+database_default_path = os.path.abspath(+ os.path.join(os.path.dirname(__file__), "..", "user", "comfyui.db")+)+parser.add_argument("--database-url", type=str, default=f"sqlite:///{database_default_path}", help="Specify the database URL, e.g. for an in-memory database you can use 'sqlite:///:memory:'.")+if comfy.options.args_parsing:args = parser.parse_args()else:diff --git a/main.py b/main.pyindex fb1f8d20b..7dfc7bc23 100644--- a/main.py+++ b/main.py@@ -238,6 +238,15 @@ def cleanup_temp():shutil.rmtree(temp_dir, ignore_errors=True)+def setup_database():+ try:+ from app.database.db import init_db, dependencies_available+ if dependencies_available():+ init_db()+ except Exception as e:+ logging.error(f"Failed to initialize database. Please ensure you have installed the latest requirements. If the error persists, please report this as in future the database will be required: {e}")++def start_comfyui(asyncio_loop=None):"""Starts the ComfyUI server using the provided asyncio event loop or creates a new one.@@ -266,6 +275,7 @@ def start_comfyui(asyncio_loop=None):hook_breaker_ac10a0.restore_functions()cuda_malloc_warning()+ setup_database()prompt_server.add_routes()hijack_progress(prompt_server)diff --git a/requirements.txt b/requirements.txtindex ee24b1cb3..6c5bf1d92 100644--- a/requirements.txt+++ b/requirements.txt@@ -18,6 +18,8 @@ Pillowscipytqdmpsutil+alembic+SQLAlchemy#non essential dependencies:kornia>=0.7.1diff --git a/utils/install_util.py b/utils/install_util.pynew file mode 100644index 000000000..0f59bcf91--- /dev/null+++ b/utils/install_util.py@@ -0,0 +1,18 @@+from pathlib import Path+import sys++# The path to the requirements.txt file+requirements_path = Path(__file__).parents[1] / "requirements.txt"+++def get_missing_requirements_message():+ """The warning message to display when a package is missing."""++ extra = ""+ if sys.flags.no_user_site:+ extra = "-s "+ return f"""+Please install the updated requirements.txt file by running:+{sys.executable} {extra}-m pip install -r {requirements_path}+If you are on the portable package you can run: update\\update_comfyui.bat to solve this problem.+""".strip()