Aug 7 - Update: knowledge base MT19
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
"""phase53 — admin-controlled ordering for support knowledge entries
|
||||
|
||||
Adds to `support_knowledge`:
|
||||
|
||||
sort_order INT NOT NULL DEFAULT 0
|
||||
|
||||
Knowledge entries are injected into the support chat's Groq system prompt, and
|
||||
the combined text is capped, so the ORDER entries appear in decides which
|
||||
guidance the assistant reaches for first and which is truncated away. Before
|
||||
this the order was `id` — i.e. whenever the entry happened to be created — with
|
||||
no way for an admin to promote an important entry short of deleting and
|
||||
re-adding it.
|
||||
|
||||
Ordering is `sort_order ASC, id ASC` everywhere it is read, so ties break
|
||||
deterministically and existing entries (all defaulting to 0) keep their current
|
||||
relative order on upgrade. Nothing is reshuffled by running this.
|
||||
|
||||
Uses an INFORMATION_SCHEMA existence check — safe to re-run.
|
||||
"""
|
||||
|
||||
revision = 'phase53_knowledge_sort_order'
|
||||
down_revision = 'phase52_user_ui_theme'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
def _column_exists(conn, table, column):
|
||||
return conn.execute(sa.text(
|
||||
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS "
|
||||
"WHERE TABLE_SCHEMA = DATABASE() "
|
||||
"AND TABLE_NAME = :t AND COLUMN_NAME = :c"
|
||||
), {"t": table, "c": column}).scalar() > 0
|
||||
|
||||
|
||||
def _table_exists(conn, table):
|
||||
return conn.execute(sa.text(
|
||||
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES "
|
||||
"WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = :t"
|
||||
), {"t": table}).scalar() > 0
|
||||
|
||||
|
||||
def upgrade():
|
||||
bind = op.get_bind()
|
||||
# Guarded on the table too: support_knowledge arrived in phase40, and a
|
||||
# tenant provisioned from an older baseline could reach this revision
|
||||
# without it.
|
||||
if not _table_exists(bind, 'support_knowledge'):
|
||||
return
|
||||
if not _column_exists(bind, 'support_knowledge', 'sort_order'):
|
||||
op.execute(sa.text(
|
||||
"ALTER TABLE support_knowledge "
|
||||
"ADD COLUMN sort_order INT NOT NULL DEFAULT 0"
|
||||
))
|
||||
# Idempotent repair only — never resets a deliberate ordering.
|
||||
op.execute(sa.text(
|
||||
"UPDATE support_knowledge SET sort_order = 0 WHERE sort_order IS NULL"
|
||||
))
|
||||
|
||||
|
||||
def downgrade():
|
||||
bind = op.get_bind()
|
||||
if (_table_exists(bind, 'support_knowledge')
|
||||
and _column_exists(bind, 'support_knowledge', 'sort_order')):
|
||||
op.execute(sa.text(
|
||||
"ALTER TABLE support_knowledge DROP COLUMN sort_order"
|
||||
))
|
||||
Reference in New Issue
Block a user