refine: update server/db.js
This commit is contained in:
parent
4fdcc9b031
commit
1a73981d3c
32
server/db.js
32
server/db.js
@ -21,6 +21,7 @@ export async function initDb() {
|
|||||||
CREATE TABLE IF NOT EXISTS templates (
|
CREATE TABLE IF NOT EXISTS templates (
|
||||||
id text PRIMARY KEY,
|
id text PRIMARY KEY,
|
||||||
tenant_id text NOT NULL,
|
tenant_id text NOT NULL,
|
||||||
|
template_key text,
|
||||||
name text NOT NULL,
|
name text NOT NULL,
|
||||||
type text NOT NULL CHECK (type IN ('email', 'report')),
|
type text NOT NULL CHECK (type IN ('email', 'report')),
|
||||||
status text NOT NULL DEFAULT 'draft',
|
status text NOT NULL DEFAULT 'draft',
|
||||||
@ -33,6 +34,14 @@ export async function initDb() {
|
|||||||
updated_at timestamptz NOT NULL DEFAULT now()
|
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(`
|
await db.query(`
|
||||||
CREATE TABLE IF NOT EXISTS template_versions (
|
CREATE TABLE IF NOT EXISTS template_versions (
|
||||||
id text PRIMARY KEY,
|
id text PRIMARY KEY,
|
||||||
@ -45,8 +54,31 @@ export async function initDb() {
|
|||||||
UNIQUE(template_id, version)
|
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(`
|
await db.query(`
|
||||||
CREATE INDEX IF NOT EXISTS templates_tenant_updated_idx
|
CREATE INDEX IF NOT EXISTS templates_tenant_updated_idx
|
||||||
ON templates (tenant_id, updated_at DESC)
|
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)
|
||||||
|
`)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user