Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

feat(db): add quota_pools and quota_consumption migrations (B/F2)

diegosouzapw4f149fb5a341
diff --git a/src/lib/db/migrations/073_quota_pools.sql b/src/lib/db/migrations/073_quota_pools.sql
new file mode 100644
index 0000000000..255a126071
--- /dev/null
+++ b/src/lib/db/migrations/073_quota_pools.sql
@@ -0,0 +1,31 @@
+-- Migration 073: quota_pools + quota_allocations
+--
+-- Creates the two tables that persist quota-sharing pools and per-API-key
+-- allocations within each pool. Idempotent: safe to run more than once.
+-- Foreign key ON DELETE CASCADE ensures allocations are removed when a pool
+-- is deleted. Weight is stored as REAL (0-100 %).
+--
+-- Part of: Group B — Quota Sharing Engine (plan 22, frente F2).
+
+CREATE TABLE IF NOT EXISTS quota_pools (
+ id TEXT PRIMARY KEY,
+ connection_id TEXT NOT NULL,
+ name TEXT NOT NULL,
+ created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE INDEX IF NOT EXISTS idx_quota_pools_connection
+ ON quota_pools(connection_id);
+
+CREATE TABLE IF NOT EXISTS quota_allocations (
+ pool_id TEXT NOT NULL REFERENCES quota_pools(id) ON DELETE CASCADE,
+ api_key_id TEXT NOT NULL,
+ weight REAL NOT NULL CHECK (weight >= 0 AND weight <= 100),
+ cap_value REAL,
+ cap_unit TEXT CHECK (cap_unit IN ('percent','requests','tokens','usd')),
+ policy TEXT NOT NULL CHECK (policy IN ('hard','soft','burst')) DEFAULT 'hard',
+ PRIMARY KEY (pool_id, api_key_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_quota_allocations_apikey
+ ON quota_allocations(api_key_id);
diff --git a/src/lib/db/migrations/074_quota_consumption.sql b/src/lib/db/migrations/074_quota_consumption.sql
new file mode 100644
index 0000000000..71fb2cbafb
--- /dev/null
+++ b/src/lib/db/migrations/074_quota_consumption.sql
@@ -0,0 +1,24 @@
+-- Migration 074: quota_consumption — Sliding Window Counter storage
+--
+-- Stores per-(api_key_id, dimension_key) consumption using 2-bucket sliding
+-- window counters. dimension_key format: "<poolId>:<unit>:<window>".
+-- bucket_index = floor(now_ms / window_ms). consumed and updated_at are
+-- updated atomically via UPSERT (INSERT ... ON CONFLICT DO UPDATE).
+-- Idempotent: safe to run more than once.
+--
+-- Part of: Group B — Quota Sharing Engine (plan 22, frente F2).
+
+CREATE TABLE IF NOT EXISTS quota_consumption (
+ api_key_id TEXT NOT NULL,
+ dimension_key TEXT NOT NULL,
+ bucket_index INTEGER NOT NULL,
+ consumed REAL NOT NULL DEFAULT 0,
+ updated_at INTEGER NOT NULL, -- epoch ms
+ PRIMARY KEY (api_key_id, dimension_key, bucket_index)
+);
+
+CREATE INDEX IF NOT EXISTS idx_quota_consumption_dim_bucket
+ ON quota_consumption(dimension_key, bucket_index);
+
+CREATE INDEX IF NOT EXISTS idx_quota_consumption_updated_at
+ ON quota_consumption(updated_at);
diff --git a/src/lib/db/migrations/075_provider_plans.sql b/src/lib/db/migrations/075_provider_plans.sql
new file mode 100644
index 0000000000..eb39ed29ab
--- /dev/null
+++ b/src/lib/db/migrations/075_provider_plans.sql
@@ -0,0 +1,19 @@
+-- Migration 075: provider_plans — per-connection quota plan overrides
+--
+-- Stores manual or auto-detected quota plans for a specific provider
+-- connection. dimensions_json holds a JSON array of QuotaDimension objects
+-- ({ unit, window, limit }). source distinguishes auto-detected plans from
+-- operator-configured overrides. Idempotent: safe to run more than once.
+--
+-- Part of: Group B — Quota Sharing Engine (plan 22, frente F2).
+
+CREATE TABLE IF NOT EXISTS provider_plans (
+ connection_id TEXT PRIMARY KEY, -- 1:1 with provider_connections; NULL not allowed since it is PK
+ provider TEXT NOT NULL,
+ dimensions_json TEXT NOT NULL, -- JSON array of QuotaDimension
+ source TEXT NOT NULL CHECK (source IN ('auto','manual')) DEFAULT 'manual',
+ updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE INDEX IF NOT EXISTS idx_provider_plans_provider
+ ON provider_plans(provider);