modularizado de prompts
This commit is contained in:
50
db/migrations/20260125100000_prompt_templates.sql
Normal file
50
db/migrations/20260125100000_prompt_templates.sql
Normal file
@@ -0,0 +1,50 @@
|
||||
-- migrate:up
|
||||
|
||||
-- Tabla para almacenar prompts editables por tenant con versionado
|
||||
CREATE TABLE prompt_templates (
|
||||
id SERIAL PRIMARY KEY,
|
||||
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
|
||||
prompt_key VARCHAR(50) NOT NULL, -- 'router', 'greeting', 'orders', 'shipping', 'payment', 'browse'
|
||||
content TEXT NOT NULL,
|
||||
model VARCHAR(50) DEFAULT 'gpt-4-turbo',
|
||||
version INTEGER NOT NULL DEFAULT 1,
|
||||
is_active BOOLEAN DEFAULT true,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
created_by TEXT, -- email o identificador del usuario que creó el prompt
|
||||
|
||||
CONSTRAINT uq_prompt_version UNIQUE(tenant_id, prompt_key, version)
|
||||
);
|
||||
|
||||
-- Índice para búsqueda rápida del prompt activo
|
||||
CREATE INDEX idx_prompt_active ON prompt_templates(tenant_id, prompt_key)
|
||||
WHERE is_active = true;
|
||||
|
||||
-- Índice para listar versiones
|
||||
CREATE INDEX idx_prompt_versions ON prompt_templates(tenant_id, prompt_key, version DESC);
|
||||
|
||||
-- Función para auto-incrementar versión al insertar
|
||||
CREATE OR REPLACE FUNCTION increment_prompt_version()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
-- Calcular siguiente versión
|
||||
NEW.version := COALESCE(
|
||||
(SELECT MAX(version) + 1 FROM prompt_templates
|
||||
WHERE tenant_id = NEW.tenant_id AND prompt_key = NEW.prompt_key),
|
||||
1
|
||||
);
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Trigger para auto-versión
|
||||
CREATE TRIGGER prompt_version_trigger
|
||||
BEFORE INSERT ON prompt_templates
|
||||
FOR EACH ROW EXECUTE FUNCTION increment_prompt_version();
|
||||
|
||||
-- migrate:down
|
||||
|
||||
DROP TRIGGER IF EXISTS prompt_version_trigger ON prompt_templates;
|
||||
DROP FUNCTION IF EXISTS increment_prompt_version();
|
||||
DROP INDEX IF EXISTS idx_prompt_versions;
|
||||
DROP INDEX IF EXISTS idx_prompt_active;
|
||||
DROP TABLE IF EXISTS prompt_templates;
|
||||
Reference in New Issue
Block a user