simonw--llm
a196db1ed0
Test / test (3.10) (push) Has been cancelled
Test / test (3.11) (push) Has been cancelled
Test / test (3.7) (push) Has been cancelled
Test / test (3.8) (push) Has been cancelled
Test / test (3.9) (push) Has been cancelled
92 行
2.0 KiB
Python
92 行
2.0 KiB
Python
import datetime
|
|
|
|
MIGRATIONS = []
|
|
migration = MIGRATIONS.append
|
|
|
|
|
|
def migrate(db):
|
|
ensure_migrations_table(db)
|
|
already_applied = {r["name"] for r in db["_llm_migrations"].rows}
|
|
for fn in MIGRATIONS:
|
|
name = fn.__name__
|
|
if name not in already_applied:
|
|
fn(db)
|
|
db["_llm_migrations"].insert(
|
|
{"name": name, "applied_at": str(datetime.datetime.utcnow())}
|
|
)
|
|
already_applied.add(name)
|
|
|
|
|
|
def ensure_migrations_table(db):
|
|
if not db["_llm_migrations"].exists():
|
|
db["_llm_migrations"].create(
|
|
{
|
|
"name": str,
|
|
"applied_at": str,
|
|
},
|
|
pk="name",
|
|
)
|
|
|
|
|
|
@migration
|
|
def m001_initial(db):
|
|
# Ensure the original table design exists, so other migrations can run
|
|
if db["log"].exists():
|
|
# It needs to have the chat_id column
|
|
if not "chat_id" in db["log"].columns_dict:
|
|
db["log"].add_column("chat_id")
|
|
return
|
|
db["log"].create(
|
|
{
|
|
"provider": str,
|
|
"system": str,
|
|
"prompt": str,
|
|
"chat_id": str,
|
|
"response": str,
|
|
"model": str,
|
|
"timestamp": str,
|
|
}
|
|
)
|
|
|
|
|
|
@migration
|
|
def m002_id_primary_key(db):
|
|
db["log"].transform(pk="id")
|
|
|
|
|
|
@migration
|
|
def m003_chat_id_foreign_key(db):
|
|
db["log"].transform(types={"chat_id": int})
|
|
db["log"].add_foreign_key("chat_id", "log", "id")
|
|
|
|
|
|
@migration
|
|
def m004_column_order(db):
|
|
db["log"].transform(
|
|
column_order=(
|
|
"id",
|
|
"model",
|
|
"timestamp",
|
|
"prompt",
|
|
"system",
|
|
"response",
|
|
"chat_id",
|
|
)
|
|
)
|
|
|
|
|
|
@migration
|
|
def m004_drop_provider(db):
|
|
db["log"].transform(drop=("provider",))
|
|
|
|
|
|
@migration
|
|
def m005_debug(db):
|
|
db["log"].add_column("debug", str)
|
|
db["log"].add_column("duration_ms", int)
|
|
|
|
|
|
@migration
|
|
def m006_prompt_json(db):
|
|
db["log"].add_column("prompt_json", str)
|