-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Add support for sqlite database #8444
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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_FILENAME |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ## Generate new revision | ||
|
|
||
| 1. Update models in `/app/database/models.py` | ||
| 2. Run `alembic revision --autogenerate -m "{your message}"` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ Pillow | |
| scipy | ||
| tqdm | ||
| psutil | ||
| alembic | ||
| SQLAlchemy | ||
|
|
||
| #non essential dependencies: | ||
| kornia>=0.7.1 | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the
^sqlite:///check? I assume the ORM will have an appropriate fit if it's misconfigured.@bigcat88 Hadn't noticed this - disregard my earlier comment!