sync update from LAN repo (source dhk-1 @ 3e87be55)

This commit is contained in:
gozeros committed 2026-10-21 15:06:08 +08:00
1 parent ab6068a7c1
commit 42f2a26fa8
1482 files changed
+227759 -9584

No files matched your search

@@ -0,0 +1,74 @@
"""Create durable public-document library tables.
Revision ID: 20260914_01
Revises: 20260903_01, 20260907_01
Create Date: 2026-09-14
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20260914_01"
down_revision = ("20260903_01", "20260907_01")
branch_labels = None
depends_on = None
def upgrade() -> None:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table("gongwen_documents"):
op.create_table(
"gongwen_documents",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("user_id", sa.String(length=64), nullable=True),
sa.Column("title", sa.String(length=256), nullable=False, server_default=""),
sa.Column("document_type", sa.String(length=48), nullable=False, server_default="notice"),
sa.Column("source", sa.String(length=24), nullable=False, server_default="upload"),
sa.Column("status", sa.String(length=32), nullable=False, server_default="checking"),
sa.Column("template_key", sa.String(length=96), nullable=True),
sa.Column("research_session_id", sa.String(length=64), nullable=True),
sa.Column("original_version_id", sa.String(length=64), nullable=True),
sa.Column("latest_version_id", sa.String(length=64), nullable=True),
sa.Column("issue_counts", sa.Text(), nullable=True),
sa.Column("error", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_gongwen_documents_user_id", "gongwen_documents", ["user_id"])
op.create_index("ix_gongwen_documents_document_type", "gongwen_documents", ["document_type"])
op.create_index("ix_gongwen_documents_source", "gongwen_documents", ["source"])
op.create_index("ix_gongwen_documents_status", "gongwen_documents", ["status"])
op.create_index("ix_gongwen_documents_research_session_id", "gongwen_documents", ["research_session_id"])
if not inspector.has_table("gongwen_document_versions"):
op.create_table(
"gongwen_document_versions",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("document_id", sa.String(length=64), nullable=False),
sa.Column("version_no", sa.Integer(), nullable=False),
sa.Column("kind", sa.String(length=32), nullable=False),
sa.Column("file_name", sa.String(length=512), nullable=False),
sa.Column("storage_key", sa.String(length=1024), nullable=False),
sa.Column("content_type", sa.String(length=128), nullable=False, server_default="application/octet-stream"),
sa.Column("size_bytes", sa.Integer(), nullable=False, server_default="0"),
sa.Column("sha256", sa.String(length=64), nullable=False, server_default=""),
sa.Column("review_snapshot", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint("document_id", "version_no", name="uq_gongwen_document_version_no"),
)
op.create_index("ix_gongwen_document_versions_document_id", "gongwen_document_versions", ["document_id"])
op.create_index(
"ix_gongwen_document_versions_document_created",
"gongwen_document_versions",
["document_id", "created_at"],
)
def downgrade() -> None:
inspector = sa.inspect(op.get_bind())
if inspector.has_table("gongwen_document_versions"):
op.drop_table("gongwen_document_versions")
if inspector.has_table("gongwen_documents"):
op.drop_table("gongwen_documents")
@@ -0,0 +1,88 @@
"""Persist mapping-scoped links for local LLMWiki graph expansion.
Revision ID: 20260916_01
Revises: 20260903_01, 20260907_01
Create Date: 2026-09-16
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20260916_01"
down_revision = ("20260903_01", "20260907_01")
branch_labels = None
depends_on = None
def upgrade() -> None:
inspector = sa.inspect(op.get_bind())
if inspector.has_table("llmwiki_wiki_edges"):
return
op.create_table(
"llmwiki_wiki_edges",
sa.Column("id", sa.String(length=36), primary_key=True),
sa.Column(
"knowledge_base_mapping_id",
sa.String(length=36),
sa.ForeignKey("llmwiki_knowledge_bases.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"source_page_id",
sa.String(length=36),
sa.ForeignKey("llmwiki_wiki_pages.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"target_page_id",
sa.String(length=36),
sa.ForeignKey("llmwiki_wiki_pages.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"edge_type",
sa.String(length=32),
nullable=False,
server_default="wiki_link",
),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint(
"knowledge_base_mapping_id",
"source_page_id",
"target_page_id",
"edge_type",
name="uq_llmwiki_wiki_edge_mapping_pages_type",
),
)
op.create_index(
"ix_llmwiki_wiki_edges_knowledge_base_mapping_id",
"llmwiki_wiki_edges",
["knowledge_base_mapping_id"],
)
op.create_index(
"ix_llmwiki_wiki_edges_source_page_id",
"llmwiki_wiki_edges",
["source_page_id"],
)
op.create_index(
"ix_llmwiki_wiki_edges_target_page_id",
"llmwiki_wiki_edges",
["target_page_id"],
)
op.create_index(
"ix_llmwiki_wiki_edge_mapping_source",
"llmwiki_wiki_edges",
["knowledge_base_mapping_id", "source_page_id"],
)
op.create_index(
"ix_llmwiki_wiki_edge_mapping_target",
"llmwiki_wiki_edges",
["knowledge_base_mapping_id", "target_page_id"],
)
def downgrade() -> None:
if sa.inspect(op.get_bind()).has_table("llmwiki_wiki_edges"):
op.drop_table("llmwiki_wiki_edges")
@@ -0,0 +1,240 @@
"""Add answer-level feedback reasons and agent score dimensions.
Revision ID: 20260916_02
Revises: 20260916_01
Create Date: 2026-09-16
"""
from __future__ import annotations
import hashlib
import sqlalchemy as sa
from alembic import op
revision = "20260916_02"
down_revision = "20260916_01"
branch_labels = None
depends_on = None
_TABLE = "feedback"
_OLD_INDEX = "uq_feedback_thread_run_user"
_TARGET_INDEX = "uq_feedback_target_key"
_AGENT_INDEX = "ix_feedback_agent_created"
def _columns() -> set[str]:
return {
str(column["name"])
for column in sa.inspect(op.get_bind()).get_columns(_TABLE)
}
def _indexes() -> set[str]:
return {
str(index["name"])
for index in sa.inspect(op.get_bind()).get_indexes(_TABLE)
if index.get("name")
}
def _json_type() -> sa.types.TypeEngine:
return sa.Text() if op.get_bind().dialect.name == "mysql" else sa.JSON()
def _empty_reasons_value() -> str | list[str]:
# PortableJSON is TEXT on MySQL, where binding a Python list directly to
# a raw migration Text column is invalid. SQLite/PostgreSQL JSON columns
# should receive the native list so their type processors serialize it.
return "[]" if op.get_bind().dialect.name == "mysql" else []
def _target_key(
*,
user_id: str | None,
thread_id: str,
run_id: str,
message_id: str | None,
agent_id: str,
) -> str:
message_key = message_id or f"run:{run_id}"
raw = "\x1f".join(
(user_id or "", thread_id, message_key, agent_id or "lead_agent")
)
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
def _backfill() -> None:
bind = op.get_bind()
feedback = sa.table(
_TABLE,
sa.column("feedback_id", sa.String(64)),
sa.column("run_id", sa.String(64)),
sa.column("thread_id", sa.String(64)),
sa.column("user_id", sa.String(64)),
sa.column("message_id", sa.String(64)),
sa.column("agent_id", sa.String(128)),
sa.column("target_key", sa.String(64)),
sa.column("reasons", _json_type()),
sa.column("created_at", sa.DateTime(timezone=True)),
sa.column("updated_at", sa.DateTime(timezone=True)),
)
rows = list(
bind.execute(
sa.select(
feedback.c.feedback_id,
feedback.c.run_id,
feedback.c.thread_id,
feedback.c.user_id,
feedback.c.message_id,
feedback.c.agent_id,
feedback.c.created_at,
)
).mappings()
)
run_agents: dict[str, str] = {}
inspector = sa.inspect(bind)
if rows and inspector.has_table("runs"):
run_columns = {str(column["name"]) for column in inspector.get_columns("runs")}
if "assistant_id" in run_columns:
runs = sa.table(
"runs",
sa.column("run_id", sa.String(64)),
sa.column("assistant_id", sa.String(128)),
)
run_ids = sorted({str(row["run_id"]) for row in rows})
run_agents = {
str(row.run_id): str(row.assistant_id)
for row in bind.execute(
sa.select(runs.c.run_id, runs.c.assistant_id).where(
runs.c.run_id.in_(run_ids)
)
)
if row.assistant_id
}
seen_target_keys: set[str] = set()
for row in rows:
agent_id = str(
run_agents.get(str(row["run_id"]))
or row.get("agent_id")
or "lead_agent"
)[:128]
target_key = _target_key(
user_id=row.get("user_id"),
thread_id=str(row["thread_id"]),
run_id=str(row["run_id"]),
message_id=row.get("message_id"),
agent_id=agent_id,
)
# SQL unique indexes historically allowed several NULL-user rows.
# Keep those legacy records without blocking the new unique digest;
# one canonical row retains the deterministic future-upsert key.
if target_key in seen_target_keys:
legacy_raw = f"{target_key}\x1flegacy:{row['feedback_id']}"
target_key = hashlib.sha256(legacy_raw.encode("utf-8")).hexdigest()
else:
seen_target_keys.add(target_key)
bind.execute(
sa.update(feedback)
.where(feedback.c.feedback_id == row["feedback_id"])
.values(
agent_id=agent_id,
target_key=target_key,
reasons=_empty_reasons_value(),
updated_at=row.get("created_at"),
)
)
def upgrade() -> None:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table(_TABLE):
return
columns = _columns()
if "agent_id" not in columns:
op.add_column(
_TABLE,
sa.Column(
"agent_id",
sa.String(length=128),
nullable=True,
server_default="lead_agent",
),
)
if "target_key" not in columns:
op.add_column(
_TABLE,
sa.Column("target_key", sa.String(length=64), nullable=True),
)
if "reasons" not in columns:
op.add_column(
_TABLE,
sa.Column("reasons", _json_type(), nullable=True),
)
if "updated_at" not in columns:
op.add_column(
_TABLE,
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
)
_backfill()
# SQLite needs a batch table rebuild for nullability changes; Alembic uses
# native ALTER statements on MySQL/PostgreSQL where possible.
with op.batch_alter_table(_TABLE) as batch:
batch.alter_column(
"agent_id",
existing_type=sa.String(length=128),
nullable=False,
server_default=None,
)
batch.alter_column(
"target_key",
existing_type=sa.String(length=64),
nullable=False,
)
batch.alter_column(
"reasons",
existing_type=_json_type(),
nullable=False,
)
batch.alter_column(
"updated_at",
existing_type=sa.DateTime(timezone=True),
nullable=False,
)
indexes = _indexes()
if _OLD_INDEX in indexes:
op.drop_index(_OLD_INDEX, table_name=_TABLE)
indexes = _indexes()
if _TARGET_INDEX not in indexes:
op.create_index(_TARGET_INDEX, _TABLE, ["target_key"], unique=True)
if _AGENT_INDEX not in indexes:
op.create_index(_AGENT_INDEX, _TABLE, ["agent_id", "created_at"])
def downgrade() -> None:
if not sa.inspect(op.get_bind()).has_table(_TABLE):
return
indexes = _indexes()
if _AGENT_INDEX in indexes:
op.drop_index(_AGENT_INDEX, table_name=_TABLE)
if _TARGET_INDEX in indexes:
op.drop_index(_TARGET_INDEX, table_name=_TABLE)
if _OLD_INDEX not in _indexes():
op.create_index(
_OLD_INDEX,
_TABLE,
["thread_id", "run_id", "user_id"],
unique=True,
mysql_length={"user_id": 63},
)
columns = _columns()
with op.batch_alter_table(_TABLE) as batch:
for name in ("updated_at", "reasons", "target_key", "agent_id"):
if name in columns:
batch.drop_column(name)
@@ -0,0 +1,25 @@
"""Merge feedback, public-document, and assistant-knowledge migration heads.
Revision ID: 20260916_03
Revises: 20260916_02, 20260909_01, 20260914_01
Create Date: 2026-09-16
This merge used to share revision id ``20260916_01`` with the LLMWiki graph
edge migration. Giving the no-op merge its own id preserves the graph-edge
revision for already stamped databases while restoring an unambiguous graph.
"""
from __future__ import annotations
revision = "20260916_03"
down_revision = ("20260916_02", "20260909_01", "20260914_01")
branch_labels = None
depends_on = None
def upgrade() -> None:
"""Record the merge; all parent revisions already own their DDL."""
def downgrade() -> None:
"""Splitting the migration history requires no schema change."""
@@ -0,0 +1,49 @@
"""Add editable starter questions and retrieval settings to LLMWiki mappings.
Revision ID: 20260920_01
Revises: 20260903_01, 20260907_01
Create Date: 2026-09-20
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from deerflow.persistence.types import PortableJSON
revision = "20260920_01"
down_revision = ("20260903_01", "20260907_01")
branch_labels = None
depends_on = None
def _columns() -> set[str]:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table("llmwiki_knowledge_bases"):
return set()
return {str(column["name"]) for column in inspector.get_columns("llmwiki_knowledge_bases")}
def upgrade() -> None:
columns = _columns()
if not columns:
return
if "test_questions" not in columns:
op.add_column(
"llmwiki_knowledge_bases",
sa.Column("test_questions", PortableJSON(), nullable=True),
)
if "retrieval_settings" not in columns:
op.add_column(
"llmwiki_knowledge_bases",
sa.Column("retrieval_settings", PortableJSON(), nullable=True),
)
def downgrade() -> None:
columns = _columns()
if "retrieval_settings" in columns:
op.drop_column("llmwiki_knowledge_bases", "retrieval_settings")
if "test_questions" in columns:
op.drop_column("llmwiki_knowledge_bases", "test_questions")
@@ -0,0 +1,194 @@
"""Reconcile LLMWiki schema after historical duplicate revision ids.
Revision ID: 20260920_02
Revises: 20260920_01, 20260916_03
Create Date: 2026-09-20
Older source snapshots used ``20260916_01`` and ``20260920_01`` twice. A
database stamped with either value can therefore be missing the schema from
the other migration that carried the same id. This merge is deliberately
idempotent and restores every affected table, constraint, index, and JSON
column before recording the new unambiguous head.
"""
from __future__ import annotations
from collections.abc import Iterable
import sqlalchemy as sa
from alembic import op
from deerflow.persistence.types import PortableJSON
revision = "20260920_02"
down_revision = ("20260920_01", "20260916_03")
branch_labels = None
depends_on = None
_EDGE_TABLE = "llmwiki_wiki_edges"
_EDGE_UNIQUE = "uq_llmwiki_wiki_edge_mapping_pages_type"
_EDGE_COLUMNS = (
"knowledge_base_mapping_id",
"source_page_id",
"target_page_id",
"edge_type",
)
_EDGE_FOREIGN_KEYS = (
(
"fk_llmwiki_wiki_edges_mapping",
"knowledge_base_mapping_id",
"llmwiki_knowledge_bases",
),
(
"fk_llmwiki_wiki_edges_source_page",
"source_page_id",
"llmwiki_wiki_pages",
),
(
"fk_llmwiki_wiki_edges_target_page",
"target_page_id",
"llmwiki_wiki_pages",
),
)
_EDGE_INDEXES = (
(
"ix_llmwiki_wiki_edges_knowledge_base_mapping_id",
("knowledge_base_mapping_id",),
),
("ix_llmwiki_wiki_edges_source_page_id", ("source_page_id",)),
("ix_llmwiki_wiki_edges_target_page_id", ("target_page_id",)),
(
"ix_llmwiki_wiki_edge_mapping_source",
("knowledge_base_mapping_id", "source_page_id"),
),
(
"ix_llmwiki_wiki_edge_mapping_target",
("knowledge_base_mapping_id", "target_page_id"),
),
)
def _create_edges_table() -> None:
op.create_table(
_EDGE_TABLE,
sa.Column("id", sa.String(length=36), primary_key=True),
sa.Column("knowledge_base_mapping_id", sa.String(length=36), nullable=False),
sa.Column("source_page_id", sa.String(length=36), nullable=False),
sa.Column("target_page_id", sa.String(length=36), nullable=False),
sa.Column(
"edge_type",
sa.String(length=32),
nullable=False,
server_default="wiki_link",
),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(
["knowledge_base_mapping_id"],
["llmwiki_knowledge_bases.id"],
name="fk_llmwiki_wiki_edges_mapping",
ondelete="CASCADE",
),
sa.ForeignKeyConstraint(
["source_page_id"],
["llmwiki_wiki_pages.id"],
name="fk_llmwiki_wiki_edges_source_page",
ondelete="CASCADE",
),
sa.ForeignKeyConstraint(
["target_page_id"],
["llmwiki_wiki_pages.id"],
name="fk_llmwiki_wiki_edges_target_page",
ondelete="CASCADE",
),
sa.UniqueConstraint(*_EDGE_COLUMNS, name=_EDGE_UNIQUE),
)
def _foreign_key_shape(item: dict) -> tuple[tuple[str, ...], str | None, tuple[str, ...]]:
return (
tuple(str(name) for name in item.get("constrained_columns") or ()),
item.get("referred_table"),
tuple(str(name) for name in item.get("referred_columns") or ()),
)
def _ensure_edge_constraints(inspector: sa.Inspector) -> None:
unique_names = {
str(item["name"])
for item in inspector.get_unique_constraints(_EDGE_TABLE)
if item.get("name")
}
foreign_key_shapes = {
_foreign_key_shape(item) for item in inspector.get_foreign_keys(_EDGE_TABLE)
}
missing_foreign_keys = [
(name, column, referred_table)
for name, column, referred_table in _EDGE_FOREIGN_KEYS
if ((column,), referred_table, ("id",)) not in foreign_key_shapes
]
if _EDGE_UNIQUE in unique_names and not missing_foreign_keys:
return
with op.batch_alter_table(_EDGE_TABLE) as batch:
if _EDGE_UNIQUE not in unique_names:
batch.create_unique_constraint(_EDGE_UNIQUE, list(_EDGE_COLUMNS))
for name, column, referred_table in missing_foreign_keys:
batch.create_foreign_key(
name,
referred_table,
[column],
["id"],
ondelete="CASCADE",
)
def _ensure_edge_indexes(inspector: sa.Inspector) -> None:
index_names = {
str(item["name"])
for item in inspector.get_indexes(_EDGE_TABLE)
if item.get("name")
}
for name, columns in _EDGE_INDEXES:
if name not in index_names:
op.create_index(name, _EDGE_TABLE, list(columns))
def _ensure_edges() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if not inspector.has_table(_EDGE_TABLE):
_create_edges_table()
else:
_ensure_edge_constraints(inspector)
_ensure_edge_indexes(sa.inspect(bind))
def _columns(table: str) -> set[str]:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table(table):
return set()
return {str(item["name"]) for item in inspector.get_columns(table)}
def _ensure_json_columns(table: str, columns: Iterable[str]) -> None:
existing = _columns(table)
if not existing:
return
for name in columns:
if name not in existing:
op.add_column(table, sa.Column(name, PortableJSON(), nullable=True))
def upgrade() -> None:
_ensure_edges()
_ensure_json_columns(
"llmwiki_knowledge_bases",
("test_questions", "retrieval_settings"),
)
_ensure_json_columns("llmwiki_wiki_sync_states", ("last_sync_result",))
def downgrade() -> None:
columns = _columns("llmwiki_wiki_sync_states")
if "last_sync_result" in columns:
op.drop_column("llmwiki_wiki_sync_states", "last_sync_result")
@@ -0,0 +1,105 @@
"""Index task deep-link thread lookups outside the metadata JSON blob.
Revision ID: 20261015_01
Revises: 20260920_02
Create Date: 2026-10-15
``threads_meta.metadata_json`` remains the public source of truth. The new
columns are maintained projections for the high-frequency task workspace
lookup, whose task rows are shared across users and therefore cannot use the
ordinary ``user_id`` index.
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect, text
revision = "20261015_01"
down_revision = "20260920_02"
branch_labels = None
depends_on = None
_TABLE = "threads_meta"
_COLUMNS = (
("task_id", 128),
("agent_id", 128),
("action_id", 128),
)
_INDEXES = (
("ix_threads_meta_task_agent_updated", ("task_id", "agent_id", "updated_at", "thread_id")),
("ix_threads_meta_task_action_agent_updated", ("task_id", "action_id", "agent_id", "updated_at", "thread_id")),
("ix_threads_meta_user_updated", ("user_id", "updated_at", "thread_id")),
)
def _has_table(table_name: str) -> bool:
return table_name in inspect(op.get_bind()).get_table_names()
def _has_column(column_name: str) -> bool:
return any(column["name"] == column_name for column in inspect(op.get_bind()).get_columns(_TABLE))
def _has_index(index_name: str) -> bool:
return index_name in {index["name"] for index in inspect(op.get_bind()).get_indexes(_TABLE)}
def _json_scalar_expression(key: str) -> str | None:
"""Return a portable-enough JSON scalar extractor for supported engines."""
dialect = op.get_bind().dialect.name
if dialect == "sqlite":
return f"json_extract(metadata_json, '$.{key}')"
if dialect == "mysql":
# PortableJSON is LONGTEXT on MySQL, but JSON_EXTRACT accepts valid
# JSON text and returns the quoted JSON scalar until unquoted here.
return f"JSON_UNQUOTE(JSON_EXTRACT(metadata_json, '$.{key}'))"
if dialect in {"postgresql", "opengauss"}:
return f"metadata_json ->> '{key}'"
return None
def _backfill_projections() -> None:
expressions = {
"task_id": _json_scalar_expression("taskId"),
"agent_id": _json_scalar_expression("agent_id"),
"action_id": _json_scalar_expression("actionId"),
}
if any(expression is None for expression in expressions.values()):
return
assignments = ", ".join(
f"{column} = {expression}"
for column, expression in expressions.items()
)
# Only fresh projection columns are NULL. This makes a retry safe and
# never overwrites values maintained by a newer running application.
op.get_bind().execute(
text(
f"UPDATE {_TABLE} SET {assignments} "
"WHERE task_id IS NULL AND agent_id IS NULL AND action_id IS NULL"
)
)
def upgrade() -> None:
if not _has_table(_TABLE):
return
for column_name, length in _COLUMNS:
if not _has_column(column_name):
op.add_column(_TABLE, sa.Column(column_name, sa.String(length), nullable=True))
_backfill_projections()
for index_name, columns in _INDEXES:
if not _has_index(index_name):
op.create_index(index_name, _TABLE, list(columns))
def downgrade() -> None:
if not _has_table(_TABLE):
return
for index_name, _columns in reversed(_INDEXES):
if _has_index(index_name):
op.drop_index(index_name, table_name=_TABLE)
for column_name, _length in reversed(_COLUMNS):
if _has_column(column_name):
op.drop_column(_TABLE, column_name)
@@ -0,0 +1,58 @@
"""Add dedicated single-agent task deep-link conversation index.
Revision ID: 20261019_01
Revises: 20261015_01
Create Date: 2026-10-19
This is intentionally schema-only. Existing ``threads_meta`` task rows are not
backfilled; only conversations created/updated with the new ``taskGoPath``
metadata marker enter the dedicated history.
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
revision = "20261019_01"
down_revision = "20261015_01"
branch_labels = None
depends_on = None
_TABLE = "task_deeplink_threads"
_INDEX = "ix_task_deeplink_threads_scope"
def _has_table() -> bool:
return _TABLE in inspect(op.get_bind()).get_table_names()
def upgrade() -> None:
if _has_table():
return
op.create_table(
_TABLE,
sa.Column("thread_id", sa.String(64), primary_key=True),
sa.Column("task_id", sa.String(128), nullable=False),
sa.Column("source_task_id", sa.String(128), nullable=False),
sa.Column("go_path", sa.String(16), nullable=False),
sa.Column("agent_id", sa.String(128), nullable=True),
sa.Column("created_by", sa.String(64), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(
_INDEX,
_TABLE,
["task_id", "go_path", "updated_at", "thread_id"],
)
def downgrade() -> None:
if not _has_table():
return
indexes = {index["name"] for index in inspect(op.get_bind()).get_indexes(_TABLE)}
if _INDEX in indexes:
op.drop_index(_INDEX, table_name=_TABLE)
op.drop_table(_TABLE)
@@ -0,0 +1,53 @@
"""Add snapshots supplied to the b5-1 task-analysis deep link.
Revision ID: 20261020_01
Revises: 20261019_01
Create Date: 2026-10-20
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
revision = "20261020_01"
down_revision = "20261019_01"
branch_labels = None
depends_on = None
_TABLE = "task_deeplink_details"
def _has_table() -> bool:
return _TABLE in inspect(op.get_bind()).get_table_names()
def _json_type() -> sa.types.TypeEngine:
# Keep the migration consistent with PortableJSON, whose MySQL form is
# TEXT for compatibility with older deployments.
return sa.Text() if op.get_bind().dialect.name == "mysql" else sa.JSON()
def upgrade() -> None:
if _has_table():
return
op.create_table(
_TABLE,
sa.Column("detail_type", sa.String(32), primary_key=True),
sa.Column("detail_id", sa.String(128), primary_key=True),
sa.Column("task_name", sa.String(512), nullable=False),
sa.Column("task_content", sa.Text(), nullable=False),
sa.Column("overview", sa.Text(), nullable=False),
sa.Column("extra_json", _json_type(), nullable=False),
sa.Column("version", sa.Integer(), nullable=False),
sa.Column("created_by", sa.String(64), nullable=True),
sa.Column("updated_by", sa.String(64), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
def downgrade() -> None:
if _has_table():
op.drop_table(_TABLE)