-- DentalCare v0.8.1.2
-- Controlled Active Roles and Deterministic Update Ledger
-- The application updater applies these changes idempotently through
-- setup_apply_v0812(). This file is retained for release traceability and
-- fresh-installer schema review.

CREATE TABLE IF NOT EXISTS `{{prefix}}system_migration_ledger` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `version` VARCHAR(40) NOT NULL,
  `migration_key` VARCHAR(120) NOT NULL,
  `checksum_sha256` CHAR(64) NULL,
  `status` VARCHAR(30) NOT NULL DEFAULT 'completed',
  `applied_by` BIGINT UNSIGNED NULL,
  `applied_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `error_reference` VARCHAR(40) NULL,
  `error_message` TEXT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_system_migration_key` (`migration_key`),
  KEY `idx_system_migration_version` (`version`,`status`,`applied_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
