Files
ComfyAssets-ComfyUI_PromptM…/tests/test_prompt_archive_schema.py
T
Vito d02a911748 feat(library): delete or archive selected prompts, Show archived prompts and Restore (#443) (#470)
* feat(library): set the category on many selected prompts (#437)

The Prompt library shelf gets the concept's Images selection checkbox on each
card and its .bulkbar (N selected, Set category, Clear), recorded as D148.
Set category opens the shared one-value dialog and posts
{prompt_ids, category, expected_epoch} to POST /prompt_manager/bulk/category:
the server checks the storage epoch the shelf was read under, then sets the
category on the Library parents in one BEGIN IMMEDIATE transaction, writing
only rows that change. A restored storage answers 409 storage-changed and
writes nothing. The legacy admin and images_filter bodies are unchanged.

This replaces the legacy admin page's bulk Set Category, a cutover
prerequisite (#334).

Closes #437

* feat(library): add tags to many selected prompts and select every loaded prompt (#442)

The Library selection bar (D148) gets Add tags and select-all of the loaded
shelf, the legacy admin page's bulk Add Tags and select-all checkbox.

Add tags posts {prompt_ids, tags, expected_epoch} to
POST /prompt_manager/bulk/tags: in one BEGIN IMMEDIATE transaction the server
checks the storage epoch, normalizes the names as image tags are, gives each
Library parent only the names it lacks (casefold), finds or inserts tag rows
by exact name only when some prompt gets them, and skips prompts that would
pass 20 tags. Prompt tags only. Both bulk/tags forms refuse a non-object body
with 400.

Ctrl/Cmd+A in the grid and "Select all N loaded" in the bar select every
loaded prompt; the offer follows each loaded batch.

Closes #442

* feat(vocabulary): tag search, tag sort and an Untagged prompts entry (#444)

Tags & categories gains a server-side tag search and sort (keyset pages,
cursor scoped to the query) and an "Untagged prompts" row whose Browse opens
the Library with tag_mode=untagged. Refine this view gets Tag match, saved
Library views keep it, and the routes echo the applied query so a stale
server is an error, never an unfiltered list. Recorded as D149.

Refs #444

* feat(library): filter prompts by output folder and browse tags with Any (#444)

Refine this view on the Prompt library gains the concept's Folder (folders
below an output root, read when the dialog opens) and a Tag match select
(All / Any). The listing takes folder and tag_mode, matches folders on the
normalized file_path under every spelling of each output root, echoes both
so a stale server is an error, and saved Library views keep them. Recorded
as D150.

Refs #444

* docs(library): record Add tags and select-all as D151 (#442)

* feat(library): archive paired and delete unpaired selected prompts on the server (#443)

A prompt_archive table marks Library parents hidden from the shelf; older
builds ignore it. New routes /prompt_manager/library/delete/preview,
/library/delete and /library/restore classify a selection (delete,
archive, protected, already archived, not found), execute exactly what was
reviewed under a records hold, take a verified safety backup (disk checked)
before any delete, and restore archived prompts. The shelf, search and the
shelf-facing counts honor the archive (?shelf=active|archived); archiving
repairs a stale prompt hash so scans find the archived row.

* feat(library): Delete… on the selection bar, Show archived prompts and Restore (#443)

The Library bar's Delete… previews the selection and opens the shared
review: what is deleted (in no saved pair), archived (paired: hidden,
restorable) or kept (tagged __protected__), the safety backup and what
cannot be undone. Show archived prompts (N) lists archived prompts with
Restore on their bar; an archived prompt's page shows an Archived notice
with Restore. TESTING.md records D152; the route snapshot gains the three
workspace routes.

* test(library): cover the Delete… review failure and close-only paths; record coverage floors (#443)

* fix(library): let saved-view defaults fill the missing #444 filters

libraryViewState passed `?? 'all'` and `?? ''` fallbacks that
normalizeViewState already applies to an undefined value. The redundant
branches were never taken in the page, which dropped saved-views.mjs below
its 95% branch floor. A unit now covers a shelf without the new filters.

Refs #444

* fix(library): lay out a loaded page of prompts without waiting for an animation frame (#443)

A starved renderer (headless WebKit under CPU load, a background tab) can
hold requestAnimationFrame for seconds. The shelf laid out each loaded page
only in its next frame, so the grid stayed empty after the first load or a
delete's reload while the caption already showed the new count. A loaded
batch is now laid out at once; measurements still flush per frame.

The regression test holds every animation frame and fails before the fix.

* test(saved-views): give the D153 Library view the tag_mode key (#444)

The Library saved-view state now holds tag_mode, and the API requires
every key, so main's new "Highest rated" spec posts it too.

Refs #444

* test(saved-views): give the D153 Library view the folder key (#444)

Refs #444

* fix(library): name the saved pair, not negative variants, as the archive reason (#443)

A prompt in a saved pair with no negative variants was described in the
Delete… review as "1 prompt with negative variants: hidden, restorable".
Paired prompts are archived because of the pair, so the Archive row now
reads "N prompts in a saved pair: hidden, restorable" and the first
sentence explains the pair. Found in the live test of #470.

* fix(library): nest Refine folders by path segment; backslash separates only on Windows (#444)

Live test B1: the Folder list indented a server list sorted as full-path
text, so "-" and "_" sorted before "/" and children landed under the wrong
parent (kiko/char under kiko-v6), with case variants interleaved. The
options are now a tree built by segment: each folder directly under its own
parent, case variants distinct siblings.

On POSIX a folder named "Metadata\Klein..." was listed but filtered to no
prompts, because normalize_folder read "\" as a separator. It now does so
only on Windows (ntpath), as the listing already did.

Refs #444

* fix(library): say why an archived prompt is archived, and name variants only when it has any (#443)

The archived prompt page's notice always said "This prompt and its
negative variants are hidden", also for a saved-pair prompt with none.
It now says the prompt is archived because it is in a saved pair, and
counts its negative variants only when there are any. From the live
test of #470.
2026-10-29 21:09:22 +00:00

197 lines
6.9 KiB
Python

"""#443: the Library parent archive table, its migration and its validation."""
import shutil
import sqlite3
import tempfile
import unittest
from pathlib import Path
from database.models import PromptModel
from database.operations import PromptDatabase
from database.prompt_archive import (
TABLE,
migrate_prompt_archive,
prompt_archive_schema_error,
)
from utils.hashing import generate_prompt_hash
def _tables(conn):
return {
row[0]
for row in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")
}
class TestPromptArchiveSchema(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.path = str(Path(self.temp.name) / "archive.db")
self.db = PromptDatabase(self.path)
def tearDown(self):
self.db.close_all()
self.temp.cleanup()
def conn(self):
return self.db.model.get_connection()
def save(self, text):
return self.db.save_prompt(text, prompt_hash=generate_prompt_hash(text))
def test_startup_creates_the_table_and_it_validates(self):
self.assertIn(TABLE, _tables(self.conn()))
self.assertIsNone(prompt_archive_schema_error(self.conn(), allow_missing=False))
def test_migration_is_idempotent(self):
conn = self.conn()
migrate_prompt_archive(conn)
migrate_prompt_archive(conn)
self.assertIsNone(prompt_archive_schema_error(conn, allow_missing=False))
def test_a_missing_table_is_accepted_only_when_allowed(self):
conn = sqlite3.connect(":memory:")
self.addCleanup(conn.close)
self.assertIsNone(prompt_archive_schema_error(conn))
self.assertEqual(
prompt_archive_schema_error(conn, allow_missing=False),
"Missing prompt archive schema",
)
def test_deleting_a_prompt_drops_its_archive_row(self):
pid = self.save("Archived then deleted")
conn = self.conn()
conn.execute(
f"INSERT INTO {TABLE}(prompt_id, archived_at) VALUES (?, ?)",
(pid, "2026-10-28T00:00:00+00:00"),
)
conn.commit()
self.db.delete_prompt(pid)
self.assertEqual(conn.execute(f"SELECT COUNT(*) FROM {TABLE}").fetchone()[0], 0)
def _broken(self, *statements):
conn = sqlite3.connect(":memory:")
self.addCleanup(conn.close)
conn.execute("CREATE TABLE prompts (id INTEGER PRIMARY KEY)")
for statement in statements:
conn.execute(statement)
return conn
def test_unsupported_structures_are_refused(self):
good = (
f"CREATE TABLE {TABLE} (prompt_id INTEGER PRIMARY KEY, "
"archived_at TEXT NOT NULL, "
"FOREIGN KEY(prompt_id) REFERENCES prompts(id) ON DELETE CASCADE)"
)
cases = {
"other definition": (
f"CREATE TABLE {TABLE} (prompt_id INTEGER PRIMARY KEY, "
"archived_at TEXT)",
),
"no cascade": (
f"CREATE TABLE {TABLE} (prompt_id INTEGER PRIMARY KEY, "
"archived_at TEXT NOT NULL, "
"FOREIGN KEY(prompt_id) REFERENCES prompts(id))",
),
"extra index": (good, f"CREATE INDEX x ON {TABLE}(archived_at)"),
"trigger": (
good,
f"CREATE TRIGGER t AFTER INSERT ON {TABLE} BEGIN SELECT 1; END",
),
"view name": (f"CREATE VIEW {TABLE} AS SELECT 1",),
}
for name, statements in cases.items():
with self.subTest(name):
self.assertIsNotNone(
prompt_archive_schema_error(self._broken(*statements))
)
def test_invalid_values_and_dangling_rows_are_refused(self):
good = (
f"CREATE TABLE {TABLE} (prompt_id INTEGER PRIMARY KEY, "
"archived_at TEXT NOT NULL, "
"FOREIGN KEY(prompt_id) REFERENCES prompts(id) ON DELETE CASCADE)"
)
for name, row in {
"number": "(1, 5)",
"blob": "(1, x'00')",
"long": f"(1, '{'x' * 65}')",
"dangling": "(99, '2026-10-28T00:00:00+00:00')",
}.items():
with self.subTest(name):
conn = self._broken(good, "INSERT INTO prompts(id) VALUES (1)")
conn.execute(f"INSERT INTO {TABLE} VALUES {row}")
self.assertIsNotNone(prompt_archive_schema_error(conn))
def test_a_refused_structure_stops_the_migration_without_changes(self):
conn = self._broken(
f"CREATE TABLE {TABLE} (prompt_id INTEGER PRIMARY KEY, archived_at TEXT)"
)
with self.assertRaises(sqlite3.DatabaseError):
migrate_prompt_archive(conn)
sql = conn.execute(
"SELECT sql FROM sqlite_master WHERE name=?", (TABLE,)
).fetchone()[0]
self.assertNotIn("NOT NULL", sql)
class TestPromptArchiveBackups(unittest.TestCase):
"""Restore validation accepts an older backup and refuses a damaged table."""
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.root = Path(self.temp.name)
self.db = PromptDatabase(str(self.root / "live.db"))
def tearDown(self):
self.db.close_all()
self.temp.cleanup()
def copy(self, name):
target = self.root / name
self.db.model.backup_database(str(target))
return str(target)
def test_a_backup_without_the_table_verifies_and_migrates(self):
path = self.copy("old.db")
with sqlite3.connect(path) as conn:
conn.execute(f"DROP TABLE {TABLE}")
conn.close()
ok, reason = self.db.model.verify_database_file(path)
self.assertTrue(ok, reason)
model = PromptModel(path)
try:
self.assertIn(TABLE, _tables(model.get_connection()))
finally:
model.close_all()
def test_a_backup_with_a_damaged_table_is_refused(self):
path = self.copy("bad.db")
with sqlite3.connect(path) as conn:
conn.execute(f"DROP TABLE {TABLE}")
conn.execute(f"CREATE TABLE {TABLE} (prompt_id INTEGER, archived_at TEXT)")
conn.close()
ok, reason = self.db.model.verify_database_file(path)
self.assertFalse(ok)
self.assertIn("archive", reason.lower())
def test_an_archived_prompt_survives_a_backup_copy(self):
pid = self.db.save_prompt("Kept", prompt_hash=generate_prompt_hash("Kept"))
conn = self.db.model.get_connection()
conn.execute(
f"INSERT INTO {TABLE}(prompt_id, archived_at) VALUES (?, ?)",
(pid, "2026-10-28T00:00:00+00:00"),
)
conn.commit()
path = self.copy("copy.db")
shutil.copy(path, path + ".2")
with sqlite3.connect(path + ".2") as other:
self.assertEqual(
other.execute(f"SELECT prompt_id FROM {TABLE}").fetchall(), [(pid,)]
)
other.close()
if __name__ == "__main__":
unittest.main()