-- AlterTable
ALTER TABLE `installations` ADD COLUMN `ring` ENUM('CANARY', 'EARLY', 'GENERAL') NOT NULL DEFAULT 'GENERAL',
    ADD COLUMN `vendor_owned` BOOLEAN NOT NULL DEFAULT false;

-- CreateTable
CREATE TABLE `releases` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `version` VARCHAR(32) NOT NULL,
    `state` ENUM('DRAFT', 'ROLLING', 'HALTED', 'COMPLETED', 'WITHDRAWN') NOT NULL DEFAULT 'DRAFT',
    `open_ring` ENUM('CANARY', 'EARLY', 'GENERAL') NULL,
    `package_sha256` VARCHAR(64) NOT NULL,
    `package_bytes` BIGINT NOT NULL,
    `download_url` VARCHAR(500) NOT NULL,
    `manifest_signature` TEXT NOT NULL,
    `signing_key_id` VARCHAR(64) NOT NULL,
    `has_schema_migration` BOOLEAN NOT NULL DEFAULT false,
    `mandatory` BOOLEAN NOT NULL DEFAULT false,
    `minimum_from_version` VARCHAR(32) NULL,
    `release_notes_ar` TEXT NULL,
    `halt_reason` VARCHAR(80) NULL,
    `halted_at` DATETIME(3) NULL,
    `halt_detail_ar` TEXT NULL,
    `published_at` DATETIME(3) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    UNIQUE INDEX `releases_version_key`(`version`),
    INDEX `releases_state_open_ring_idx`(`state`, `open_ring`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `update_events` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `release_id` INTEGER NOT NULL,
    `installation_id` INTEGER NOT NULL,
    `ring` ENUM('CANARY', 'EARLY', 'GENERAL') NOT NULL,
    `outcome` ENUM('OFFERED', 'APPLIED', 'FAILED', 'HEALTH_FAILED', 'ROLLED_BACK') NOT NULL,
    `failed_step` VARCHAR(40) NULL,
    `detail_ar` TEXT NULL,
    `from_version` VARCHAR(32) NULL,
    `occurred_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `update_events_release_id_outcome_idx`(`release_id`, `outcome`),
    INDEX `update_events_installation_id_occurred_at_idx`(`installation_id`, `occurred_at`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `update_events` ADD CONSTRAINT `update_events_release_id_fkey` FOREIGN KEY (`release_id`) REFERENCES `releases`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

