Files
backend/database/migrations/076_ai_prompt_templates_global.sql
T
Alireza HassaniandCursor 91a20a89d6 Add public AI prompt templates for websites.
Manage reusable templates in Super Admin, attach editable per-site prompts with Meshkee API public links that require no auth.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-26 15:28:41 +03:30

56 lines
1.7 KiB
SQL

-- Make AI prompt templates global; assign to websites via join + pack public id.
CREATE TABLE IF NOT EXISTS domain_ai_prompt_templates (
domain_id BIGINT NOT NULL REFERENCES domains (id) ON DELETE CASCADE,
template_id BIGINT NOT NULL REFERENCES ai_prompt_templates (id) ON DELETE CASCADE,
sort_order INT NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (domain_id, template_id)
);
CREATE INDEX IF NOT EXISTS idx_domain_ai_prompt_templates_template_id
ON domain_ai_prompt_templates (template_id);
ALTER TABLE domains
ADD COLUMN IF NOT EXISTS ai_prompts_public_id VARCHAR(32);
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint WHERE conname = 'domains_ai_prompts_public_id_unique'
) THEN
ALTER TABLE domains
ADD CONSTRAINT domains_ai_prompts_public_id_unique UNIQUE (ai_prompts_public_id);
END IF;
END $$;
-- Preserve any existing per-domain template links (from 075).
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_name = 'ai_prompt_templates' AND column_name = 'domain_id'
) THEN
INSERT INTO domain_ai_prompt_templates (domain_id, template_id, sort_order)
SELECT domain_id, id, 0
FROM ai_prompt_templates
WHERE domain_id IS NOT NULL
ON CONFLICT DO NOTHING;
UPDATE domains d
SET ai_prompts_public_id = t.public_id
FROM (
SELECT DISTINCT ON (domain_id) domain_id, public_id
FROM ai_prompt_templates
WHERE public_id IS NOT NULL
ORDER BY domain_id, id
) t
WHERE d.id = t.domain_id
AND d.ai_prompts_public_id IS NULL;
END IF;
END $$;
ALTER TABLE ai_prompt_templates DROP COLUMN IF EXISTS domain_id;
ALTER TABLE ai_prompt_templates DROP COLUMN IF EXISTS public_id;