CREATE TABLE `relay_migrations` ( `id` text PRIMARY KEY NOT NULL, `tenant_id` text NOT NULL, `source_node_id` text NOT NULL, `target_node_id` text NOT NULL, `source_generation` integer NOT NULL, `target_generation` integer NOT NULL, `state` text DEFAULT 'quiescing' NOT NULL, `backup_ref` text, `manifest_sha256` text, `requested_by` text NOT NULL, `reason` text NOT NULL, `started_at` text NOT NULL, `updated_at` text NOT NULL, `switched_at` text, `completed_at` text, `last_error_code` text, FOREIGN KEY (`tenant_id`) REFERENCES `tenant_instances`(`id`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (`source_node_id`) REFERENCES `relay_nodes`(`id`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (`target_node_id`) REFERENCES `relay_nodes`(`id`) ON UPDATE no action ON DELETE no action, CHECK (`target_generation` = `source_generation` + 1), CHECK (`source_node_id` <> `target_node_id`), CHECK (`state` IN ('quiescing', 'copying', 'switching', 'draining', 'completed', 'failed')) ); --> statement-breakpoint CREATE UNIQUE INDEX `idx_relay_migrations_one_active` ON `relay_migrations` (`tenant_id`) WHERE `state` IN ('quiescing', 'copying', 'switching', 'draining'); --> statement-breakpoint CREATE INDEX `idx_relay_migrations_source_state` ON `relay_migrations` (`source_node_id`, `state`, `updated_at`); --> statement-breakpoint CREATE INDEX `idx_relay_migrations_target_state` ON `relay_migrations` (`target_node_id`, `state`, `updated_at`);