-- Apply after unified Raw, dataset release and purchase handoff migrations. CREATE TABLE IF NOT EXISTS mdp_edge_device ( device_id CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY, tenant_id BIGINT NOT NULL, factory_id BIGINT NOT NULL, dataset_id BIGINT NOT NULL, source_id BIGINT NOT NULL, device_name VARCHAR(120) NOT NULL, state VARCHAR(16) NOT NULL, active_slot TINYINT NULL, activation_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL, activation_expires DATETIME(6) NULL, certificate_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL, certificate_expires DATETIME(6) NULL, next_certificate_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL, next_certificate_expires DATETIME(6) NULL, config_generation BIGINT NOT NULL DEFAULT 0, config_json JSON NULL, position_text MEDIUMTEXT NULL, task_phase VARCHAR(16) NULL, seen_at DATETIME(6) NULL, report_json JSON NULL, page_count INT NOT NULL DEFAULT 0, row_count BIGINT NOT NULL DEFAULT 0, created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), UNIQUE KEY uk_edge_binding (tenant_id,factory_id,dataset_id,source_id,active_slot), UNIQUE KEY uk_edge_activation (activation_hash), UNIQUE KEY uk_edge_certificate (certificate_hash), UNIQUE KEY uk_edge_pending_certificate (next_certificate_hash), KEY ix_edge_devices (tenant_id,factory_id,dataset_id) ) ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS mdp_edge_receipt ( batch_id CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY, device_id CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, tenant_id BIGINT NOT NULL, factory_id BIGINT NOT NULL, dataset_id BIGINT NOT NULL, source_id BIGINT NOT NULL, generation BIGINT NOT NULL, idem_key VARBINARY(128) NOT NULL, digest CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, position_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL, receipt_json JSON NOT NULL, created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), KEY ix_edge_receipt_position (device_id,generation,position_hash), KEY ix_edge_receipt_binding (tenant_id,factory_id,dataset_id,source_id,created_at) ) ENGINE=InnoDB; -- Recovery records reference the existing Raw batch and device. CREATE TABLE IF NOT EXISTS mdp_edge_api_rejected_page ( batch_id CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY, device_id CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, generation BIGINT NOT NULL, position_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, effective_url VARCHAR(2048) NULL, next_link VARCHAR(2048) NULL, link_diagnostic VARCHAR(80) NULL, settlement VARCHAR(16) NULL, response_path VARCHAR(112) NULL, settled_at DATETIME(6) NULL, KEY ix_edge_api_reject_position (device_id,generation,position_hash), KEY ix_edge_api_reject_settlement (device_id,settled_at) ) ENGINE=InnoDB;