Files
Termix/plugins/ai/migrations/sqlite/0001_adopt_ai_tables.sql
T

61 lines
2.6 KiB
SQL

-- ai 0001: adopt_ai_tables
-- Generated by termix-plugin migrations. Review before committing.
-- Hand-edited: keeps the legacy foreign key from messages and proposals to
-- their conversation, which the SDK cannot declare.
CREATE TABLE IF NOT EXISTS "ai_providers" (
"id" integer PRIMARY KEY AUTOINCREMENT,
"user_id" text NOT NULL,
"provider_type" text NOT NULL,
"label" text NOT NULL,
"base_url" text,
"api_key_prefix" text,
"default_model" text,
"enabled" integer NOT NULL DEFAULT 1,
"created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE
);
CREATE UNIQUE INDEX IF NOT EXISTS "idx_ai_providers_user_label" ON "ai_providers" ("user_id", "label");
ALTER TABLE "ai_providers" RENAME TO "p_ai_providers";
CREATE TABLE IF NOT EXISTS "ai_conversations" (
"id" integer PRIMARY KEY AUTOINCREMENT,
"user_id" text NOT NULL,
"title" text,
"provider_id" integer,
"model" text,
"created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "idx_ai_conversations_user" ON "ai_conversations" ("user_id", "updated_at");
ALTER TABLE "ai_conversations" RENAME TO "p_ai_conversations";
CREATE TABLE IF NOT EXISTS "ai_messages" (
"id" integer PRIMARY KEY AUTOINCREMENT,
"conversation_id" integer NOT NULL,
"role" text NOT NULL,
"content" text NOT NULL DEFAULT '',
"tool_calls" text,
"created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY ("conversation_id") REFERENCES "p_ai_conversations" ("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "idx_ai_messages_conversation" ON "ai_messages" ("conversation_id", "created_at");
ALTER TABLE "ai_messages" RENAME TO "p_ai_messages";
CREATE TABLE IF NOT EXISTS "ai_proposals" (
"id" integer PRIMARY KEY AUTOINCREMENT,
"conversation_id" integer NOT NULL,
"user_id" text NOT NULL,
"kind" text NOT NULL,
"summary" text,
"payload" text NOT NULL DEFAULT '{}',
"status" text NOT NULL DEFAULT 'pending',
"applied_at" text,
"result_summary" text,
"created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE,
FOREIGN KEY ("conversation_id") REFERENCES "p_ai_conversations" ("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "idx_ai_proposals_user" ON "ai_proposals" ("user_id", "status");
CREATE INDEX IF NOT EXISTS "idx_ai_proposals_conversation" ON "ai_proposals" ("conversation_id");
ALTER TABLE "ai_proposals" RENAME TO "p_ai_proposals";