fix(sqlite): add MCP metadata migration

main
hahahahahayesyeseys 2026-10-08 14:14:39 +00:00 committed by lyingbug
parent 0466c294ef
commit 3f9af2afa9
4 changed files with 77 additions and 1 deletions

View File

@ -0,0 +1,57 @@
package database
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestSQLiteMigrationsUpgradeV33PreservesMCPServices(t *testing.T) {
repoRoot := sqliteRepoRoot(t)
chdirAndRestore(t, copySQLiteMigrationsThrough(t, repoRoot, 33))
dbPath := filepath.Join(t.TempDir(), "upgrade-mcp.db")
opts := MigrationOptions{SQLiteDBPath: dbPath}
require.NoError(t, RunMigrationsWithOptions("sqlite3://unused", opts))
db := openSQLiteDB(t, dbPath)
_, err := db.Exec(`INSERT INTO mcp_services (id, tenant_id, name, description, transport_type)
VALUES ('existing', 1, 'Existing service', 'Legacy documentation', 'sse')`)
require.NoError(t, err)
chdirAndRestore(t, repoRoot)
require.NoError(t, RunMigrationsWithOptions("sqlite3://unused", opts))
// Starting again must not replay the column addition or change existing data.
require.NoError(t, RunMigrationsWithOptions("sqlite3://unused", opts))
var name, description, instructions string
require.NoError(t, db.QueryRow(`SELECT name, description, usage_instructions
FROM mcp_services WHERE id = 'existing'`).Scan(&name, &description, &instructions))
require.Equal(t, "Existing service", name)
require.Equal(t, "Legacy documentation", description)
require.Empty(t, instructions)
_, err = db.Exec(`INSERT INTO mcp_services (id, tenant_id, name, transport_type, usage_instructions)
VALUES ('new', 1, 'New service', 'sse', 'Use for contracts')`)
require.NoError(t, err)
_, err = db.Exec(`UPDATE mcp_services SET usage_instructions = 'Updated instructions' WHERE id = 'new'`)
require.NoError(t, err)
require.NoError(t, db.QueryRow(`SELECT usage_instructions FROM mcp_services WHERE id = 'new'`).Scan(&instructions))
require.Equal(t, "Updated instructions", instructions)
// Directory refresh uses a tenant/service/principal upsert and JSON tool counts.
const upsert = `INSERT INTO mcp_metadata
(tenant_id, service_id, principal, config_fingerprint, tools, synced_at)
VALUES (1, 'new', ?, 'fingerprint', ?, CURRENT_TIMESTAMP)
ON CONFLICT(tenant_id, service_id, principal) DO UPDATE SET tools = excluded.tools`
_, err = db.Exec(upsert, "", `[]`)
require.NoError(t, err)
_, err = db.Exec(upsert, "", `[{"name":"contracts"}]`)
require.NoError(t, err)
_, err = db.Exec(upsert, "user-a", `[]`)
require.NoError(t, err)
var count int
require.NoError(t, db.QueryRow(`SELECT json_array_length(tools) FROM mcp_metadata
WHERE tenant_id = 1 AND service_id = 'new' AND principal = ''`).Scan(&count))
require.Equal(t, 1, count)
require.NoError(t, db.QueryRow(`SELECT COUNT(*) FROM mcp_metadata WHERE service_id = 'new'`).Scan(&count))
require.Equal(t, 2, count)
}

View File

@ -30,6 +30,7 @@ var versionedSQLiteTables = []string{
"browser_task_interruptions",
"fork_snapshot_leases",
"mcp_endpoints",
"mcp_metadata",
"message_artifacts",
"tenant_skills",
"tenant_skill_snapshots",
@ -59,6 +60,7 @@ var versionedSQLiteColumns = map[string][]string{
"chunks": {"source_locators"}, // 000033
"mcp_oauth_tokens": {"principal_type", "principal_id"}, // 000064
"mcp_tool_approvals": {"enabled"}, // 000091
"mcp_services": {"usage_instructions"}, // 000092
"message_artifacts": {"deleted_at"}, // 000107
"tenant_skills": {
"envs", "served", "catalog_id", "install_session_id", "install_message_id",
@ -69,7 +71,7 @@ var versionedSQLiteColumns = map[string][]string{
}, // 000028
}
const expectedSQLiteMigrationVersion = 33
const expectedSQLiteMigrationVersion = 34
func TestSQLiteMigrationsCreateVersionedSchema(t *testing.T) {
repoRoot := sqliteRepoRoot(t)

View File

@ -0,0 +1,2 @@
DROP TABLE IF EXISTS mcp_metadata;
ALTER TABLE mcp_services DROP COLUMN usage_instructions;

View File

@ -0,0 +1,15 @@
ALTER TABLE mcp_services ADD COLUMN usage_instructions TEXT NOT NULL DEFAULT '';
CREATE TABLE IF NOT EXISTS mcp_metadata (
tenant_id INTEGER NOT NULL,
service_id VARCHAR(36) NOT NULL REFERENCES mcp_services(id) ON DELETE CASCADE,
principal VARCHAR(255) NOT NULL DEFAULT '',
config_fingerprint VARCHAR(64) NOT NULL,
tools TEXT NOT NULL,
instructions TEXT NOT NULL DEFAULT '',
server_name TEXT NOT NULL DEFAULT '',
server_version TEXT NOT NULL DEFAULT '',
server_description TEXT NOT NULL DEFAULT '',
synced_at DATETIME NOT NULL,
PRIMARY KEY (tenant_id, service_id, principal)
);