mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
a514c9e28b
Update workflows handling for Workflow Library. **Updated Workflow Storage** "Embedded Workflows" are workflows associated with images, and are now only stored in the image files. "Library Workflows" are not associated with images, and are stored only in DB. This works out nicely. We have always saved workflows to files, but recently began saving them to the DB in addition to in image files. When that happened, we stopped reading workflows from files, so all the workflows that only existed in images were inaccessible. With this change, access to those workflows is restored, and no workflows are lost. **Updated Workflow Handling in Nodes** Prior to this change, workflows were embedded in images by passing the whole workflow JSON to a special workflow field on a node. In the node's `invoke()` function, the node was able to access this workflow and save it with the image. This (inaccurately) models workflows as a property of an image and is rather awkward technically. A workflow is now a property of a batch/session queue item. It is available in the InvocationContext and therefore available to all nodes during `invoke()`. **Database Migrations** Added a `SQLiteMigrator` class to handle database migrations. Migrations were needed to accomodate the DB-related changes in this PR. See the code for details. The `images`, `workflows` and `session_queue` tables required migrations for this PR, and are using the new migrator. Other tables/services are still creating tables themselves. A followup PR will adapt them to use the migrator. **Other/Support Changes** - Add a `has_workflow` column to `images` table to indicate that the image has an embedded workflow. - Add handling for retrieving the workflow from an image in python. The image file must be fetched, the workflow extracted, and then sent to client, avoiding needing the browser to parse the image file. With the `has_workflow` column, the UI knows if there is a workflow to be fetched, and only fetches when the user requests to load the workflow. - Add route to get the workflow from an image - Add CRUD service/routes for the library workflows - `workflow_images` table and services removed (no longer needed now that embedded workflows are not in the DB)
136 lines
4.5 KiB
Python
136 lines
4.5 KiB
Python
import pytest
|
|
from pydantic import BaseModel, Field
|
|
|
|
from invokeai.app.services.config.config_default import InvokeAIAppConfig
|
|
from invokeai.app.services.item_storage.item_storage_sqlite import SqliteItemStorage
|
|
from invokeai.app.services.shared.sqlite.sqlite_database import SqliteDatabase
|
|
from invokeai.backend.util.logging import InvokeAILogger
|
|
|
|
|
|
class TestModel(BaseModel):
|
|
id: str = Field(description="ID")
|
|
name: str = Field(description="Name")
|
|
|
|
|
|
@pytest.fixture
|
|
def db() -> SqliteItemStorage[TestModel]:
|
|
sqlite_db = SqliteDatabase(InvokeAIAppConfig(use_memory_db=True), InvokeAILogger.get_logger())
|
|
sqlite_item_storage = SqliteItemStorage[TestModel](db=sqlite_db, table_name="test", id_field="id")
|
|
return sqlite_item_storage
|
|
|
|
|
|
def test_sqlite_service_can_create_and_get(db: SqliteItemStorage[TestModel]):
|
|
db.set(TestModel(id="1", name="Test"))
|
|
assert db.get("1") == TestModel(id="1", name="Test")
|
|
|
|
|
|
def test_sqlite_service_can_list(db: SqliteItemStorage[TestModel]):
|
|
db.set(TestModel(id="1", name="Test"))
|
|
db.set(TestModel(id="2", name="Test"))
|
|
db.set(TestModel(id="3", name="Test"))
|
|
results = db.list()
|
|
assert results.page == 0
|
|
assert results.pages == 1
|
|
assert results.per_page == 10
|
|
assert results.total == 3
|
|
assert results.items == [
|
|
TestModel(id="1", name="Test"),
|
|
TestModel(id="2", name="Test"),
|
|
TestModel(id="3", name="Test"),
|
|
]
|
|
|
|
|
|
def test_sqlite_service_can_delete(db: SqliteItemStorage[TestModel]):
|
|
db.set(TestModel(id="1", name="Test"))
|
|
db.delete("1")
|
|
assert db.get("1") is None
|
|
|
|
|
|
def test_sqlite_service_calls_set_callback(db: SqliteItemStorage[TestModel]):
|
|
called = False
|
|
|
|
def on_changed(item: TestModel):
|
|
nonlocal called
|
|
called = True
|
|
|
|
db.on_changed(on_changed)
|
|
db.set(TestModel(id="1", name="Test"))
|
|
assert called
|
|
|
|
|
|
def test_sqlite_service_calls_delete_callback(db: SqliteItemStorage[TestModel]):
|
|
called = False
|
|
|
|
def on_deleted(item_id: str):
|
|
nonlocal called
|
|
called = True
|
|
|
|
db.on_deleted(on_deleted)
|
|
db.set(TestModel(id="1", name="Test"))
|
|
db.delete("1")
|
|
assert called
|
|
|
|
|
|
def test_sqlite_service_can_list_with_pagination(db: SqliteItemStorage[TestModel]):
|
|
db.set(TestModel(id="1", name="Test"))
|
|
db.set(TestModel(id="2", name="Test"))
|
|
db.set(TestModel(id="3", name="Test"))
|
|
results = db.list(page=0, per_page=2)
|
|
assert results.page == 0
|
|
assert results.pages == 2
|
|
assert results.per_page == 2
|
|
assert results.total == 3
|
|
assert results.items == [TestModel(id="1", name="Test"), TestModel(id="2", name="Test")]
|
|
|
|
|
|
def test_sqlite_service_can_list_with_pagination_and_offset(db: SqliteItemStorage[TestModel]):
|
|
db.set(TestModel(id="1", name="Test"))
|
|
db.set(TestModel(id="2", name="Test"))
|
|
db.set(TestModel(id="3", name="Test"))
|
|
results = db.list(page=1, per_page=2)
|
|
assert results.page == 1
|
|
assert results.pages == 2
|
|
assert results.per_page == 2
|
|
assert results.total == 3
|
|
assert results.items == [TestModel(id="3", name="Test")]
|
|
|
|
|
|
def test_sqlite_service_can_search(db: SqliteItemStorage[TestModel]):
|
|
db.set(TestModel(id="1", name="Test"))
|
|
db.set(TestModel(id="2", name="Test"))
|
|
db.set(TestModel(id="3", name="Test"))
|
|
results = db.search(query="Test")
|
|
assert results.page == 0
|
|
assert results.pages == 1
|
|
assert results.per_page == 10
|
|
assert results.total == 3
|
|
assert results.items == [
|
|
TestModel(id="1", name="Test"),
|
|
TestModel(id="2", name="Test"),
|
|
TestModel(id="3", name="Test"),
|
|
]
|
|
|
|
|
|
def test_sqlite_service_can_search_with_pagination(db: SqliteItemStorage[TestModel]):
|
|
db.set(TestModel(id="1", name="Test"))
|
|
db.set(TestModel(id="2", name="Test"))
|
|
db.set(TestModel(id="3", name="Test"))
|
|
results = db.search(query="Test", page=0, per_page=2)
|
|
assert results.page == 0
|
|
assert results.pages == 2
|
|
assert results.per_page == 2
|
|
assert results.total == 3
|
|
assert results.items == [TestModel(id="1", name="Test"), TestModel(id="2", name="Test")]
|
|
|
|
|
|
def test_sqlite_service_can_search_with_pagination_and_offset(db: SqliteItemStorage[TestModel]):
|
|
db.set(TestModel(id="1", name="Test"))
|
|
db.set(TestModel(id="2", name="Test"))
|
|
db.set(TestModel(id="3", name="Test"))
|
|
results = db.search(query="Test", page=1, per_page=2)
|
|
assert results.page == 1
|
|
assert results.pages == 2
|
|
assert results.per_page == 2
|
|
assert results.total == 3
|
|
assert results.items == [TestModel(id="3", name="Test")]
|