Skip to content

Commit 0dd9d0e

Browse files
Mary HippMary Hipp
authored andcommitted
allow opened_at to be nullable for workflows that the user has never opened
1 parent 9ec4d96 commit 0dd9d0e

File tree

8 files changed

+59
-11
lines changed

8 files changed

+59
-11
lines changed

invokeai/app/api/routers/workflows.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ async def list_workflows(
105105
categories: Optional[list[WorkflowCategory]] = Query(default=None, description="The categories of workflow to get"),
106106
tags: Optional[list[str]] = Query(default=None, description="The tags of workflow to get"),
107107
query: Optional[str] = Query(default=None, description="The text to query by (matches name and description)"),
108+
is_recent: Optional[bool] = Query(default=None, description="Whether to include/exclude recent workflows"),
108109
) -> PaginatedResults[WorkflowRecordListItemWithThumbnailDTO]:
109110
"""Gets a page of workflows"""
110111
workflows_with_thumbnails: list[WorkflowRecordListItemWithThumbnailDTO] = []
@@ -116,6 +117,7 @@ async def list_workflows(
116117
query=query,
117118
categories=categories,
118119
tags=tags,
120+
is_recent=is_recent,
119121
)
120122
for workflow in workflows.items:
121123
workflows_with_thumbnails.append(

invokeai/app/services/shared/sqlite/sqlite_util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from invokeai.app.services.shared.sqlite_migrator.migrations.migration_15 import build_migration_15
2121
from invokeai.app.services.shared.sqlite_migrator.migrations.migration_16 import build_migration_16
2222
from invokeai.app.services.shared.sqlite_migrator.migrations.migration_17 import build_migration_17
23+
from invokeai.app.services.shared.sqlite_migrator.migrations.migration_18 import build_migration_18
2324
from invokeai.app.services.shared.sqlite_migrator.sqlite_migrator_impl import SqliteMigrator
2425

2526

@@ -57,6 +58,7 @@ def init_db(config: InvokeAIAppConfig, logger: Logger, image_files: ImageFileSto
5758
migrator.register_migration(build_migration_15())
5859
migrator.register_migration(build_migration_16())
5960
migrator.register_migration(build_migration_17())
61+
migrator.register_migration(build_migration_18())
6062
migrator.run_migrations()
6163

6264
return db
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sqlite3
2+
3+
from invokeai.app.services.shared.sqlite_migrator.sqlite_migrator_common import Migration
4+
5+
6+
class Migration18Callback:
7+
def __call__(self, cursor: sqlite3.Cursor) -> None:
8+
self._make_workflow_opened_at_nullable(cursor)
9+
10+
def _make_workflow_opened_at_nullable(self, cursor: sqlite3.Cursor) -> None:
11+
"""
12+
- Makes the `opened_at` column on workflow library table nullable by adding a new column
13+
and deprecating the old one.
14+
"""
15+
# Rename existing column to deprecated
16+
cursor.execute("ALTER TABLE workflow_library RENAME COLUMN opened_at TO opened_at_deprecated;")
17+
# Add new nullable column
18+
cursor.execute("ALTER TABLE workflow_library ADD COLUMN opened_at DATETIME;")
19+
20+
21+
def build_migration_18() -> Migration:
22+
"""
23+
Build the migration from database version 17 to 18.
24+
25+
This migration does the following:
26+
- Makes the `opened_at` column on workflow library table nullable.
27+
"""
28+
migration_18 = Migration(
29+
from_version=17,
30+
to_version=18,
31+
callback=Migration18Callback(),
32+
)
33+
34+
return migration_18

invokeai/app/services/workflow_records/workflow_records_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def get_many(
4646
per_page: Optional[int],
4747
query: Optional[str],
4848
tags: Optional[list[str]],
49+
is_recent: Optional[bool],
4950
) -> PaginatedResults[WorkflowRecordListItemDTO]:
5051
"""Gets many workflows."""
5152
pass

invokeai/app/services/workflow_records/workflow_records_common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime
22
from enum import Enum
3-
from typing import Any, Union
3+
from typing import Any, Optional, Union
44

55
import semver
66
from pydantic import BaseModel, ConfigDict, Field, JsonValue, TypeAdapter, field_validator
@@ -98,7 +98,7 @@ class WorkflowRecordDTOBase(BaseModel):
9898
name: str = Field(description="The name of the workflow.")
9999
created_at: Union[datetime.datetime, str] = Field(description="The created timestamp of the workflow.")
100100
updated_at: Union[datetime.datetime, str] = Field(description="The updated timestamp of the workflow.")
101-
opened_at: Union[datetime.datetime, str] = Field(description="The opened timestamp of the workflow.")
101+
opened_at: Optional[Union[datetime.datetime, str]] = Field(description="The opened timestamp of the workflow.")
102102

103103

104104
class WorkflowRecordDTO(WorkflowRecordDTOBase):

invokeai/app/services/workflow_records/workflow_records_sqlite.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def get_many(
118118
per_page: Optional[int] = None,
119119
query: Optional[str] = None,
120120
tags: Optional[list[str]] = None,
121+
is_recent: Optional[bool] = None,
121122
) -> PaginatedResults[WorkflowRecordListItemDTO]:
122123
# sanitize!
123124
assert order_by in WorkflowRecordOrderBy
@@ -175,6 +176,11 @@ def get_many(
175176
conditions.append(tags_condition)
176177
params.extend(tags_params)
177178

179+
if is_recent:
180+
conditions.append("opened_at IS NOT NULL")
181+
elif is_recent is False:
182+
conditions.append("opened_at IS NULL")
183+
178184
# Ignore whitespace in the query
179185
stripped_query = query.strip() if query else None
180186
if stripped_query:
@@ -319,13 +325,13 @@ def _sync_default_workflows(self) -> None:
319325
bytes_ = path.read_bytes()
320326
workflow_from_file = WorkflowValidator.validate_json(bytes_)
321327

322-
assert workflow_from_file.id.startswith("default_"), (
323-
f'Invalid default workflow ID (must start with "default_"): {workflow_from_file.id}'
324-
)
328+
assert workflow_from_file.id.startswith(
329+
"default_"
330+
), f'Invalid default workflow ID (must start with "default_"): {workflow_from_file.id}'
325331

326-
assert workflow_from_file.meta.category is WorkflowCategory.Default, (
327-
f"Invalid default workflow category: {workflow_from_file.meta.category}"
328-
)
332+
assert (
333+
workflow_from_file.meta.category is WorkflowCategory.Default
334+
), f"Invalid default workflow category: {workflow_from_file.meta.category}"
329335

330336
workflows_from_file.append(workflow_from_file)
331337

invokeai/frontend/web/src/features/nodes/components/sidePanel/workflow/WorkflowLibrary/WorkflowLibrarySideNav.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ const recentWorkflowsQueryArg = {
153153
per_page: 5,
154154
order_by: 'opened_at',
155155
direction: 'DESC',
156+
is_recent: true,
156157
} satisfies Parameters<typeof useListWorkflowsQuery>[0];
157158

158159
const RecentWorkflows = memo(() => {

invokeai/frontend/web/src/services/api/schema.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21157,7 +21157,7 @@ export type components = {
2115721157
* Opened At
2115821158
* @description The opened timestamp of the workflow.
2115921159
*/
21160-
opened_at: string;
21160+
opened_at: string | null;
2116121161
/** @description The workflow. */
2116221162
workflow: components["schemas"]["Workflow"];
2116321163
};
@@ -21187,7 +21187,7 @@ export type components = {
2118721187
* Opened At
2118821188
* @description The opened timestamp of the workflow.
2118921189
*/
21190-
opened_at: string;
21190+
opened_at: string | null;
2119121191
/**
2119221192
* Description
2119321193
* @description The description of the workflow.
@@ -21238,7 +21238,7 @@ export type components = {
2123821238
* Opened At
2123921239
* @description The opened timestamp of the workflow.
2124021240
*/
21241-
opened_at: string;
21241+
opened_at: string | null;
2124221242
/** @description The workflow. */
2124321243
workflow: components["schemas"]["Workflow"];
2124421244
/**
@@ -24300,6 +24300,8 @@ export interface operations {
2430024300
tags?: string[] | null;
2430124301
/** @description The text to query by (matches name and description) */
2430224302
query?: string | null;
24303+
/** @description Whether to include/exclude recent workflows */
24304+
is_recent?: boolean | null;
2430324305
};
2430424306
header?: never;
2430524307
path?: never;

0 commit comments

Comments
 (0)