-- Generated by aether-data-schema from schema/logical/*.toml.
-- Do not edit generated files directly; edit logical schema or explicit overrides instead.

CREATE TABLE IF NOT EXISTS public.background_task_runs (
    id character varying(64) NOT NULL,
    task_key character varying(200) NOT NULL,
    kind character varying(32) NOT NULL,
    trigger character varying(64) NOT NULL,
    status character varying(32) NOT NULL,
    attempt integer DEFAULT 0 NOT NULL,
    max_attempts integer DEFAULT 0 NOT NULL,
    owner_instance character varying(200),
    progress_percent integer DEFAULT 0 NOT NULL,
    progress_message text,
    payload_json jsonb,
    result_json jsonb,
    error_message text,
    cancel_requested boolean DEFAULT false NOT NULL,
    created_by character varying(200),
    created_at_unix_secs bigint NOT NULL,
    started_at_unix_secs bigint,
    finished_at_unix_secs bigint,
    updated_at_unix_secs bigint NOT NULL
);

ALTER TABLE ONLY public.background_task_runs ADD CONSTRAINT background_task_runs_pkey PRIMARY KEY (id);
CREATE INDEX IF NOT EXISTS idx_background_task_runs_task_key ON public.background_task_runs USING btree (task_key);
CREATE INDEX IF NOT EXISTS idx_background_task_runs_status ON public.background_task_runs USING btree (status);
CREATE INDEX IF NOT EXISTS idx_background_task_runs_kind ON public.background_task_runs USING btree (kind);
CREATE INDEX IF NOT EXISTS idx_background_task_runs_created_at ON public.background_task_runs USING btree (created_at_unix_secs);

CREATE TABLE IF NOT EXISTS public.background_task_events (
    id character varying(64) NOT NULL,
    run_id character varying(64) NOT NULL,
    event_type character varying(64) NOT NULL,
    message text NOT NULL,
    payload_json jsonb,
    created_at_unix_secs bigint NOT NULL
);

ALTER TABLE ONLY public.background_task_events ADD CONSTRAINT background_task_events_pkey PRIMARY KEY (id);
CREATE INDEX IF NOT EXISTS idx_background_task_events_run_id ON public.background_task_events USING btree (run_id, created_at_unix_secs);
ALTER TABLE ONLY public.background_task_events ADD CONSTRAINT fk_background_task_events_run FOREIGN KEY (run_id) REFERENCES public.background_task_runs(id) ON DELETE CASCADE;

