-- 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` int AUTO_INCREMENT PRIMARY KEY, `user_id` varchar(255) NOT NULL, `name` text NOT NULL, `description` text, `enabled` boolean NOT NULL DEFAULT true, `definition` text NOT NULL, `definition_version` int NOT NULL DEFAULT 1, `concurrency_policy` text NOT NULL DEFAULT ('skip'), `max_run_seconds` int NOT NULL DEFAULT 300, `dry_run` boolean NOT NULL DEFAULT false, `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 ); RENAME TABLE `automations` TO `p_automations_automations`; CREATE TABLE IF NOT EXISTS `automation_trigger_state` ( `id` int AUTO_INCREMENT PRIMARY KEY, `automation_id` int NOT NULL, `state_key` varchar(255) NOT NULL, `breach_started_at` text, `last_fired_at` text, `last_value` double, `last_observed_state` text, `updated_at` text NOT NULL DEFAULT (CURRENT_TIMESTAMP), FOREIGN KEY (`automation_id`) REFERENCES `p_automations_automations` (`id`) ON DELETE CASCADE ); RENAME TABLE `automation_trigger_state` TO `p_automations_trigger_state`; CREATE TABLE IF NOT EXISTS `automation_schedules` ( `id` int AUTO_INCREMENT PRIMARY KEY, `automation_id` int NOT NULL, `cron` text, `interval_seconds` int, `timezone` text, `next_due_at` varchar(255), `last_tick_at` text, FOREIGN KEY (`automation_id`) REFERENCES `p_automations_automations` (`id`) ON DELETE CASCADE ); RENAME TABLE `automation_schedules` TO `p_automations_schedules`; CREATE TABLE IF NOT EXISTS `automation_runs` ( `id` int AUTO_INCREMENT PRIMARY KEY, `automation_id` int NOT NULL, `user_id` varchar(255) NOT NULL, `trigger_type` text NOT NULL, `trigger_context` text, `status` text NOT NULL, `started_at` varchar(255) NOT NULL DEFAULT (CURRENT_TIMESTAMP), `finished_at` text, `duration_ms` int, `error` text, `dry_run` boolean NOT NULL DEFAULT false, `parent_run_id` int, FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE, FOREIGN KEY (`automation_id`) REFERENCES `p_automations_automations` (`id`) ON DELETE CASCADE ); RENAME TABLE `automation_runs` TO `p_automations_runs`; CREATE TABLE IF NOT EXISTS `automation_run_steps` ( `id` int AUTO_INCREMENT PRIMARY KEY, `run_id` int NOT NULL, `step_index` int 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` boolean NOT NULL DEFAULT false, FOREIGN KEY (`run_id`) REFERENCES `p_automations_runs` (`id`) ON DELETE CASCADE ); RENAME TABLE `automation_run_steps` TO `p_automations_run_steps`; CREATE TABLE IF NOT EXISTS `automation_channels` ( `id` int AUTO_INCREMENT PRIMARY KEY, `automation_id` int NOT NULL, `channel_id` int 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 ); RENAME TABLE `automation_channels` TO `p_automations_channels`;