-- Migration: database-backed pipeline templates for Builder/Studio save flows.

CREATE EXTENSION IF NOT EXISTS pgcrypto;

CREATE TABLE IF NOT EXISTS athena_pipeline_templates (
    id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    pipeline_template_id uuid NOT NULL DEFAULT gen_random_uuid() UNIQUE,
    template_name text NOT NULL,
    definition jsonb NOT NULL,
    metadata jsonb NOT NULL DEFAULT '{}'::jsonb,
    created_at timestamptz NOT NULL DEFAULT now(),
    updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE UNIQUE INDEX IF NOT EXISTS idx_athena_pipeline_templates_name
    ON athena_pipeline_templates (lower(template_name));
