From 1a73981d3c40bbf3235f2cbac907bcb774f53a8d Mon Sep 17 00:00:00 2001 From: Admin Date: Wed, 23 Sep 2026 02:59:31 +0000 Subject: [PATCH] refine: update server/db.js --- server/db.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/server/db.js b/server/db.js index a835e34..049b537 100644 --- a/server/db.js +++ b/server/db.js @@ -21,6 +21,7 @@ export async function initDb() { CREATE TABLE IF NOT EXISTS templates ( id text PRIMARY KEY, tenant_id text NOT NULL, + template_key text, name text NOT NULL, type text NOT NULL CHECK (type IN ('email', 'report')), status text NOT NULL DEFAULT 'draft', @@ -33,6 +34,14 @@ export async function initDb() { updated_at timestamptz NOT NULL DEFAULT now() ) `) + await db.query(`ALTER TABLE templates ADD COLUMN IF NOT EXISTS template_key text`) + await db.query(` + UPDATE templates + SET template_key = 'template-' || substr(replace(id, '-', ''), 1, 12) + WHERE template_key IS NULL + `) + await db.query(`ALTER TABLE templates ALTER COLUMN template_key SET NOT NULL`) + await db.query(` CREATE TABLE IF NOT EXISTS template_versions ( id text PRIMARY KEY, @@ -45,8 +54,31 @@ export async function initDb() { UNIQUE(template_id, version) ) `) + + await db.query(` + CREATE TABLE IF NOT EXISTS api_keys ( + id text PRIMARY KEY, + tenant_id text NOT NULL, + name text NOT NULL, + key_prefix text NOT NULL, + key_hash text NOT NULL UNIQUE, + created_by text, + created_at timestamptz NOT NULL DEFAULT now(), + last_used_at timestamptz, + revoked_at timestamptz + ) + `) + await db.query(` CREATE INDEX IF NOT EXISTS templates_tenant_updated_idx ON templates (tenant_id, updated_at DESC) `) + await db.query(` + CREATE UNIQUE INDEX IF NOT EXISTS templates_tenant_key_idx + ON templates (tenant_id, template_key) + `) + await db.query(` + CREATE INDEX IF NOT EXISTS api_keys_tenant_created_idx + ON api_keys (tenant_id, created_at DESC) + `) }