-- automations 0001: adopt_automations -- Generated by termix-plugin migrations. Review before committing. -- Hand-edited: keeps the legacy foreign keys the SDK cannot declare -- (links between these tables, and a channel link to notification_channels). CREATE TABLE IF NOT EXISTS "automations" ( "id" integer PRIMARY KEY AUTOINCREMENT, "user_id" text NOT NULL, "name" text NOT NULL, "description" text, "enabled" integer NOT NULL DEFAULT 1, "definition" text NOT NULL, "definition_version" integer NOT NULL DEFAULT 1, "concurrency_policy" text NOT NULL DEFAULT 'skip', "max_run_seconds" integer NOT NULL DEFAULT 300, "dry_run" integer NOT NULL DEFAULT 0, "last_run_at" text, "last_run_status" text, "created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS "idx_automations_user" ON "automations" ("user_id", "enabled"); ALTER TABLE "automations" RENAME TO "p_automations_automations"; CREATE TABLE IF NOT EXISTS "automation_trigger_state" ( "id" integer PRIMARY KEY AUTOINCREMENT, "automation_id" integer NOT NULL, "state_key" text NOT NULL, "breach_started_at" text, "last_fired_at" text, "last_value" real, "last_observed_state" text, "updated_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY ("automation_id") REFERENCES "p_automations_automations" ("id") ON DELETE CASCADE ); CREATE UNIQUE INDEX IF NOT EXISTS "idx_automation_trigger_state_key" ON "automation_trigger_state" ("automation_id", "state_key"); ALTER TABLE "automation_trigger_state" RENAME TO "p_automations_trigger_state"; CREATE TABLE IF NOT EXISTS "automation_schedules" ( "id" integer PRIMARY KEY AUTOINCREMENT, "automation_id" integer NOT NULL, "cron" text, "interval_seconds" integer, "timezone" text, "next_due_at" text, "last_tick_at" text, FOREIGN KEY ("automation_id") REFERENCES "p_automations_automations" ("id") ON DELETE CASCADE ); CREATE UNIQUE INDEX IF NOT EXISTS "idx_automation_schedules_automation" ON "automation_schedules" ("automation_id"); CREATE INDEX IF NOT EXISTS "idx_automation_schedules_due" ON "automation_schedules" ("next_due_at"); ALTER TABLE "automation_schedules" RENAME TO "p_automations_schedules"; CREATE TABLE IF NOT EXISTS "automation_runs" ( "id" integer PRIMARY KEY AUTOINCREMENT, "automation_id" integer NOT NULL, "user_id" text NOT NULL, "trigger_type" text NOT NULL, "trigger_context" text, "status" text NOT NULL, "started_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, "finished_at" text, "duration_ms" integer, "error" text, "dry_run" integer NOT NULL DEFAULT 0, "parent_run_id" integer, FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE, FOREIGN KEY ("automation_id") REFERENCES "p_automations_automations" ("id") ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS "idx_automation_runs_automation" ON "automation_runs" ("automation_id", "started_at"); CREATE INDEX IF NOT EXISTS "idx_automation_runs_user" ON "automation_runs" ("user_id", "started_at"); ALTER TABLE "automation_runs" RENAME TO "p_automations_runs"; CREATE TABLE IF NOT EXISTS "automation_run_steps" ( "id" integer PRIMARY KEY AUTOINCREMENT, "run_id" integer NOT NULL, "step_index" integer NOT NULL, "step_id" text NOT NULL, "step_type" text NOT NULL, "status" text NOT NULL, "started_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP, "finished_at" text, "output" text, "error" text, "truncated" integer NOT NULL DEFAULT 0, FOREIGN KEY ("run_id") REFERENCES "p_automations_runs" ("id") ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS "idx_automation_run_steps_run" ON "automation_run_steps" ("run_id", "step_index"); ALTER TABLE "automation_run_steps" RENAME TO "p_automations_run_steps"; CREATE TABLE IF NOT EXISTS "automation_channels" ( "id" integer PRIMARY KEY AUTOINCREMENT, "automation_id" integer NOT NULL, "channel_id" integer NOT NULL, FOREIGN KEY ("automation_id") REFERENCES "p_automations_automations" ("id") ON DELETE CASCADE, FOREIGN KEY ("channel_id") REFERENCES "notification_channels" ("id") ON DELETE CASCADE ); CREATE UNIQUE INDEX IF NOT EXISTS "idx_automation_channels_pair" ON "automation_channels" ("automation_id", "channel_id"); ALTER TABLE "automation_channels" RENAME TO "p_automations_channels";