Add v1 migrations

This commit is contained in:
Owen
2026-08-17 16:50:51 -04:00
parent 92916cd9db
commit 7d10b63dc5
2 changed files with 729 additions and 0 deletions
+364
View File
@@ -20,10 +20,374 @@ export default async function migration() {
db.pragma("foreign_keys = OFF");
db.transaction(() => {
db.prepare(
`ALTER TABLE 'sites' RENAME COLUMN "subnet" TO "exitNodeSubnet";`
).run();
db.prepare(
`
CREATE TABLE 'aiBudgetBreachEvents' (
'id' integer PRIMARY KEY AUTOINCREMENT NOT NULL,
'orgId' text NOT NULL,
'budgetId' integer NOT NULL,
'enforcement' text NOT NULL,
'unit' text NOT NULL,
'period' text NOT NULL,
'amount' real NOT NULL,
'usageAmount' real NOT NULL,
'blocked' integer NOT NULL,
'requestUserId' text,
'createdAt' integer NOT NULL,
FOREIGN KEY ('orgId') REFERENCES 'orgs'('orgId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('budgetId') REFERENCES 'aiBudgets'('budgetId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('requestUserId') REFERENCES 'user'('id') ON UPDATE no action ON DELETE set null
);
`
).run();
db.prepare(
`
CREATE INDEX 'idx_ai_budget_breach_events_budget_created' ON 'aiBudgetBreachEvents' ('budgetId','createdAt');
`
).run();
db.prepare(
`
CREATE TABLE 'aiBudgets' (
'budgetId' integer PRIMARY KEY AUTOINCREMENT NOT NULL,
'orgId' text NOT NULL,
'providerId' integer,
'modelId' integer,
'resourceId' integer,
'siteResourceId' integer,
'roleId' integer,
'virtualApiKeyId' text,
'amount' real NOT NULL,
'unit' text NOT NULL,
'period' text DEFAULT 'monthly' NOT NULL,
'enforcement' text DEFAULT 'hard' NOT NULL,
'enabled' integer DEFAULT true NOT NULL,
'createdAt' integer NOT NULL,
'updatedAt' integer NOT NULL,
FOREIGN KEY ('orgId') REFERENCES 'orgs'('orgId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('providerId') REFERENCES 'aiProviders'('providerId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('modelId') REFERENCES 'aiModels'('modelId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('resourceId') REFERENCES 'resources'('resourceId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('siteResourceId') REFERENCES 'siteResources'('siteResourceId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('roleId') REFERENCES 'roles'('roleId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('virtualApiKeyId') REFERENCES 'virtualApiKeys'('virtualApiKeyId') ON UPDATE no action ON DELETE cascade
);
`
).run();
db.prepare(
`CREATE UNIQUE INDEX 'ai_budget_provider_uniq' ON 'aiBudgets' ('providerId','unit','period');`
).run();
db.prepare(
`CREATE UNIQUE INDEX 'ai_budget_model_uniq' ON 'aiBudgets' ('modelId','unit','period');`
).run();
db.prepare(
`CREATE UNIQUE INDEX 'ai_budget_resource_uniq' ON 'aiBudgets' ('resourceId','unit','period');`
).run();
db.prepare(
`CREATE UNIQUE INDEX 'ai_budget_site_resource_uniq' ON 'aiBudgets' ('siteResourceId','unit','period');`
).run();
db.prepare(
`CREATE UNIQUE INDEX 'ai_budget_role_uniq' ON 'aiBudgets' ('roleId','unit','period');`
).run();
db.prepare(
`CREATE UNIQUE INDEX 'ai_budget_virtual_api_key_uniq' ON 'aiBudgets' ('virtualApiKeyId','unit','period');`
).run();
db.prepare(
`
CREATE TABLE 'aiModels' (
'modelId' integer PRIMARY KEY AUTOINCREMENT NOT NULL,
'providerId' integer NOT NULL,
'modelKey' text NOT NULL,
'name' text NOT NULL,
'listType' text DEFAULT 'allow' NOT NULL,
'enabled' integer DEFAULT true NOT NULL,
'createdAt' integer NOT NULL,
'updatedAt' integer NOT NULL,
FOREIGN KEY ('providerId') REFERENCES 'aiProviders'('providerId') ON UPDATE no action ON DELETE cascade
);
`
).run();
db.prepare(
`
CREATE UNIQUE INDEX 'ai_model_provider_key_uniq' ON 'aiModels' ('providerId','modelKey');
`
).run();
db.prepare(
`
CREATE TABLE 'aiProviders' (
'providerId' integer PRIMARY KEY AUTOINCREMENT NOT NULL,
'orgId' text NOT NULL,
'name' text NOT NULL,
'niceId' text NOT NULL,
'type' text NOT NULL,
'upstreamUrl' text,
'apiKey' text,
'apiKeyLastChars' text,
'authType' text NOT NULL,
'routingMode' text DEFAULT 'url' NOT NULL,
'capabilities' text DEFAULT '[]' NOT NULL,
'headers' text,
'skipTlsVerification' integer DEFAULT false NOT NULL,
'enabled' integer DEFAULT true NOT NULL,
'createdAt' integer NOT NULL,
'updatedAt' integer NOT NULL,
FOREIGN KEY ('orgId') REFERENCES 'orgs'('orgId') ON UPDATE no action ON DELETE cascade
);
`
).run();
db.prepare(
`
CREATE INDEX 'idx_aiProviders_orgId_niceId' ON 'aiProviders' ('orgId','niceId');
`
).run();
db.prepare(
`
CREATE TABLE 'aiSessionLog' (
'id' integer PRIMARY KEY AUTOINCREMENT NOT NULL,
'sessionId' text NOT NULL,
'orgId' text,
'providerId' integer,
'capability' text NOT NULL,
'resourceId' integer,
'siteResourceId' integer,
'userId' text,
'virtualApiKeyId' text,
'requestedModel' text,
'isStream' integer DEFAULT false NOT NULL,
'requestBody' text,
'responseBody' text,
'normalizedRequest' text,
'normalizedResponse' text,
'truncated' integer DEFAULT false NOT NULL,
'statusCode' integer,
'createdAt' integer NOT NULL,
FOREIGN KEY ('orgId') REFERENCES 'orgs'('orgId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('providerId') REFERENCES 'aiProviders'('providerId') ON UPDATE no action ON DELETE set null,
FOREIGN KEY ('resourceId') REFERENCES 'resources'('resourceId') ON UPDATE no action ON DELETE set null,
FOREIGN KEY ('siteResourceId') REFERENCES 'siteResources'('siteResourceId') ON UPDATE no action ON DELETE set null,
FOREIGN KEY ('userId') REFERENCES 'user'('id') ON UPDATE no action ON DELETE set null,
FOREIGN KEY ('virtualApiKeyId') REFERENCES 'virtualApiKeys'('virtualApiKeyId') ON UPDATE no action ON DELETE set null
);
`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_session_log_org_created' ON 'aiSessionLog' ('orgId','createdAt');`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_session_log_org_provider_created' ON 'aiSessionLog' ('orgId','providerId','createdAt');`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_session_log_org_resource_created' ON 'aiSessionLog' ('orgId','resourceId','createdAt');`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_session_log_org_site_resource_created' ON 'aiSessionLog' ('orgId','siteResourceId','createdAt');`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_session_log_org_user_created' ON 'aiSessionLog' ('orgId','userId','createdAt');`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_session_log_org_virtual_api_key_created' ON 'aiSessionLog' ('orgId','virtualApiKeyId','createdAt');`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_session_log_session' ON 'aiSessionLog' ('sessionId');`
).run();
db.prepare(
`
CREATE TABLE 'aiUsageRecords' (
'id' integer PRIMARY KEY AUTOINCREMENT NOT NULL,
'orgId' text NOT NULL,
'providerId' integer,
'resourceId' integer,
'siteResourceId' integer,
'userId' text,
'virtualApiKeyId' text,
'sessionId' text,
'requestedModel' text NOT NULL,
'promptTokens' integer DEFAULT 0 NOT NULL,
'cacheReadTokens' integer DEFAULT 0 NOT NULL,
'cacheWriteTokens' integer DEFAULT 0 NOT NULL,
'completionTokens' integer DEFAULT 0 NOT NULL,
'reasoningTokens' integer DEFAULT 0 NOT NULL,
'totalTokens' integer DEFAULT 0 NOT NULL,
'costUsd' real,
'estimated' integer DEFAULT false NOT NULL,
'createdAt' integer NOT NULL,
FOREIGN KEY ('orgId') REFERENCES 'orgs'('orgId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('providerId') REFERENCES 'aiProviders'('providerId') ON UPDATE no action ON DELETE set null,
FOREIGN KEY ('resourceId') REFERENCES 'resources'('resourceId') ON UPDATE no action ON DELETE set null,
FOREIGN KEY ('siteResourceId') REFERENCES 'siteResources'('siteResourceId') ON UPDATE no action ON DELETE set null,
FOREIGN KEY ('userId') REFERENCES 'user'('id') ON UPDATE no action ON DELETE set null,
FOREIGN KEY ('virtualApiKeyId') REFERENCES 'virtualApiKeys'('virtualApiKeyId') ON UPDATE no action ON DELETE set null
);
`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_usage_records_org_provider_created' ON 'aiUsageRecords' ('orgId','providerId','createdAt');`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_usage_records_org_resource_created' ON 'aiUsageRecords' ('orgId','resourceId','createdAt');`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_usage_records_org_site_resource_created' ON 'aiUsageRecords' ('orgId','siteResourceId','createdAt');`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_usage_records_org_user_created' ON 'aiUsageRecords' ('orgId','userId','createdAt');`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_usage_records_org_virtual_api_key_created' ON 'aiUsageRecords' ('orgId','virtualApiKeyId','createdAt');`
).run();
db.prepare(
`CREATE INDEX 'idx_ai_usage_records_session' ON 'aiUsageRecords' ('sessionId');`
).run();
db.prepare(
`
CREATE TABLE 'resourceAiModels' (
'resourceId' integer NOT NULL,
'modelId' integer NOT NULL,
'listType' text DEFAULT 'allow' NOT NULL,
PRIMARY KEY('resourceId', 'modelId'),
FOREIGN KEY ('resourceId') REFERENCES 'resources'('resourceId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('modelId') REFERENCES 'aiModels'('modelId') ON UPDATE no action ON DELETE cascade
);
`
).run();
db.prepare(
`
CREATE TABLE 'resourceAiProviders' (
'resourceId' integer NOT NULL,
'providerId' integer NOT NULL,
'accessMode' text DEFAULT 'inherit' NOT NULL,
'enabled' integer DEFAULT true NOT NULL,
PRIMARY KEY('resourceId', 'providerId'),
FOREIGN KEY ('resourceId') REFERENCES 'resources'('resourceId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('providerId') REFERENCES 'aiProviders'('providerId') ON UPDATE no action ON DELETE cascade
);
`
).run();
db.prepare(
`
CREATE TABLE 'siteResourceAiModels' (
'siteResourceId' integer NOT NULL,
'modelId' integer NOT NULL,
'listType' text DEFAULT 'allow' NOT NULL,
PRIMARY KEY('siteResourceId', 'modelId'),
FOREIGN KEY ('siteResourceId') REFERENCES 'siteResources'('siteResourceId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('modelId') REFERENCES 'aiModels'('modelId') ON UPDATE no action ON DELETE cascade
);
`
).run();
db.prepare(
`
CREATE TABLE 'siteResourceAiProviders' (
'siteResourceId' integer NOT NULL,
'providerId' integer NOT NULL,
'accessMode' text DEFAULT 'inherit' NOT NULL,
'enabled' integer DEFAULT true NOT NULL,
PRIMARY KEY('siteResourceId', 'providerId'),
FOREIGN KEY ('siteResourceId') REFERENCES 'siteResources'('siteResourceId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('providerId') REFERENCES 'aiProviders'('providerId') ON UPDATE no action ON DELETE cascade
);
`
).run();
db.prepare(
`
CREATE TABLE 'virtualApiKeyResources' (
'virtualApiKeyId' text NOT NULL,
'resourceId' integer NOT NULL,
PRIMARY KEY('virtualApiKeyId', 'resourceId'),
FOREIGN KEY ('virtualApiKeyId') REFERENCES 'virtualApiKeys'('virtualApiKeyId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('resourceId') REFERENCES 'resources'('resourceId') ON UPDATE no action ON DELETE cascade
);
`
).run();
db.prepare(
`
CREATE TABLE 'virtualApiKeys' (
'virtualApiKeyId' text PRIMARY KEY NOT NULL,
'orgId' text NOT NULL,
'kind' text NOT NULL,
'userId' text,
'name' text,
'description' text,
'token' text NOT NULL,
'lastChars' text NOT NULL,
'allResources' integer DEFAULT false NOT NULL,
'expiresAt' integer,
'lastUsedAt' integer,
'createdAt' integer NOT NULL,
'createdByUserId' text,
FOREIGN KEY ('orgId') REFERENCES 'orgs'('orgId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('userId') REFERENCES 'user'('id') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('createdByUserId') REFERENCES 'user'('id') ON UPDATE no action ON DELETE set null
);
`
).run();
db.prepare(
`
CREATE UNIQUE INDEX 'virtual_api_key_user_identity_uniq' ON 'virtualApiKeys' ('orgId','userId') WHERE "virtualApiKeys"."kind" = 'user';
`
).run();
db.prepare(
`ALTER TABLE 'roles' ADD COLUMN 'sshSudoModeNew' text DEFAULT 'full';`
).run();
db.prepare(
`UPDATE 'roles' SET "sshSudoModeNew" = "sshSudoMode";`
).run();
db.prepare(`ALTER TABLE 'roles' DROP COLUMN "sshSudoMode";`).run();
db.prepare(
`ALTER TABLE 'roles' RENAME COLUMN "sshSudoModeNew" TO "sshSudoMode";`
).run();
db.prepare(
`
CREATE TABLE '__new_targets' (
'targetId' integer PRIMARY KEY AUTOINCREMENT NOT NULL,
'resourceId' integer,
'providerId' integer,
'siteId' integer NOT NULL,
'ip' text NOT NULL,
'method' text,
'port' integer NOT NULL,
'internalPort' integer,
'enabled' integer DEFAULT true NOT NULL,
'path' text,
'pathMatchType' text,
'rewritePath' text,
'rewritePathType' text,
'priority' integer DEFAULT 100 NOT NULL,
'mode' text DEFAULT 'http' NOT NULL,
'authToken' text,
FOREIGN KEY ('resourceId') REFERENCES 'resources'('resourceId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('providerId') REFERENCES 'aiProviders'('providerId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('siteId') REFERENCES 'sites'('siteId') ON UPDATE no action ON DELETE cascade
);
`
).run();
db.prepare(
`INSERT INTO '__new_targets'("targetId", "resourceId", "providerId", "siteId", "ip", "method", "port", "internalPort", "enabled", "path", "pathMatchType", "rewritePath", "rewritePathType", "priority", "mode", "authToken") SELECT "targetId", "resourceId", "providerId", "siteId", "ip", "method", "port", "internalPort", "enabled", "path", "pathMatchType", "rewritePath", "rewritePathType", "priority", "mode", "authToken" FROM 'targets';`
).run();
db.prepare(`DROP TABLE 'targets';`).run();
db.prepare(
`ALTER TABLE '__new_targets' RENAME TO 'targets';`
).run();
db.prepare(
`ALTER TABLE 'subscriptions' ADD 'override' integer DEFAULT false;`
).run();
db.prepare(
`ALTER TABLE 'clients' ADD 'exitNodeSubnet' text;`
).run();
db.prepare(
`ALTER TABLE 'orgs' ADD 'settingsLogRetentionDaysAISessions' integer DEFAULT 7 NOT NULL;`
).run();
db.prepare(
`ALTER TABLE 'siteResources' ADD 'requiresExitNodeConnection' integer DEFAULT false NOT NULL;`
).run();
const insertRoleAction = db.prepare(`
INSERT INTO 'roleActions' ("roleId", "actionId", "orgId")