using System.Text.Json; using Admin.NET.Plugin.AiDOP.DataPlatform.Core; using Admin.NET.Plugin.AiDOP.Entity.DataPlatform; using Admin.NET.Plugin.ApprovalFlow; namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress; public sealed record MdpSchemaMigrationConfirmation(string? EvidenceReference); public sealed partial class MdpIngestDatasetController { private const string MigrationReadyAction = "schema-migration-ready"; [HttpPost("{id:long}/releases/{release:long}/migration-ready")] [DisplayName("登记 Schema 迁移完成")] public Task RecordSchemaMigration(long id, long release, [FromQuery] long? factoryId, MdpSchemaMigrationConfirmation input) => Locked(id, factoryId, async (dataset, scope) => { if (!await Can(PublishPermission)) return Forbid(); var row = await db.Ado.SqlQuerySingleAsync(ReleaseSelect + " AND id=@release FOR UPDATE", new { tenant = scope.TenantId, factory = scope.FactoryId, id, release }); Require(row != null && row.State == "REVIEW" && row.BaseRevision == dataset.Revision && row.EnvironmentKey == TargetEnvironment, "请在当前配置修订提交候选版本审批"); var approved = await Approval(row!); Require(approved?.Status == FlowInstanceStatusEnum.Approved && await HasFactoryApproval(approved.Id, scope), "尚未取得人工审批通过记录"); var current = await EffectiveSchemaVersion(dataset, row!.SourceId); Require(await RequiresSchemaMigration(dataset, row, current), "该版本无需登记 Schema 迁移"); await CheckMigrationWindow(dataset, current); var reference = input.EvidenceReference?.Trim(); Require(reference is { Length: > 0 and <= 250 } && !reference.Any(char.IsControl), "填写调用方迁移完成的工单或联测记录"); await Log(id, release, MigrationReadyAction, MigrationStamp(current, row) + reference); return NoContent(); }); private async Task EnsureSchemaMigrationReady(MdpIngestDataset dataset, Release row) { var current = await EffectiveSchemaVersion(dataset, row.SourceId); if (!await RequiresSchemaMigration(dataset, row, current)) return; await CheckMigrationWindow(dataset, current); Require(await HasMigrationConfirmation(dataset, row, current), "破坏性 Schema 变更须先登记调用方迁移完成"); } private async Task EffectiveSchemaVersion(MdpIngestDataset dataset, long sourceId) => await new MdpIngestDatasetRegistry(db).ReadSourceVersion(dataset, sourceId) ?? dataset.ActiveSchemaVersion; private async Task RequiresSchemaMigration(MdpIngestDataset dataset, Release row) => await RequiresSchemaMigration(dataset, row, await EffectiveSchemaVersion(dataset, row.SourceId)); private async Task RequiresSchemaMigration(MdpIngestDataset dataset, Release row, string? activeVersion) { if (activeVersion is not { Length: > 0 }) return false; var active = await ReadActiveConfiguration(dataset, activeVersion); if (active == null) return true; var candidate = new PurchaseVersionConfiguration(dataset.DatasetKey, row.Version, row.MappingJson); if (candidate.Package == null && active.Package == null) return !string.Equals(active.MappingJson, candidate.MappingJson, StringComparison.Ordinal); return !DescribePurchaseSource(candidate).AcceptsPreviousSource(DescribePurchaseSource(active)); } private static GovernancePackage DescribePurchaseSource(PurchaseVersionConfiguration configuration) => configuration.Package ?? GovernancePackage.Parse(JsonSerializer.Serialize(new { format = GovernancePackage.Format, rules = configuration.Names.Select(x => new GovernanceRule(x.Key, x.Value)) }, GovernancePackage.JsonOptions), GovernancePackage.PurchaseFields); private async Task ReadActiveConfiguration(MdpIngestDataset dataset, string version) { var archive = await db.Ado.SqlQuerySingleAsync(""" SELECT mapping_json FROM mdp_ingest_contract_archive WHERE tenant_id=@TenantId AND factory_id=@FactoryId AND dataset_id=@Id AND schema_version=@version """, new { dataset.TenantId, dataset.FactoryId, dataset.Id, version }); if (archive is null && new PurchaseOrderSchemaRegistry().Find(dataset.DatasetKey, version) is not null) archive = "{}"; return archive is null ? null : new PurchaseVersionConfiguration(dataset.DatasetKey, version, archive); } private async Task CheckMigrationWindow(MdpIngestDataset dataset, string? current) { Require(current is { } version && await ReadActiveConfiguration(dataset, version) != null, "当前版本的历史映射不可核对"); Require(await Lifecycle(dataset.Id) == "PAUSED", "请先暂停接收,再按暂停后的修订创建迁移候选"); var outstanding = await db.Ado.GetIntAsync(""" SELECT COUNT(*) FROM mdp_ingest_receive_task WHERE dataset_id=@Id AND tenant_id=@TenantId AND status NOT IN ('SUCCEEDED','PARTIAL','QUARANTINED') """, new { dataset.Id, dataset.TenantId }); Require(outstanding == 0, "请先处理完该数据集的在途和待处置批次"); } private async Task HasMigrationConfirmation(MdpIngestDataset dataset, Release row) => await HasMigrationConfirmation(dataset, row, await EffectiveSchemaVersion(dataset, row.SourceId)); private async Task HasMigrationConfirmation(MdpIngestDataset dataset, Release row, string? current) { var stamp = MigrationStamp(current, row); return await db.Ado.GetIntAsync(""" SELECT COUNT(*) FROM mdp_ingest_release_log WHERE dataset_id=@datasetId AND release_id=@release AND action=@action AND LEFT(detail,CHAR_LENGTH(@stamp))=@stamp """, new { datasetId = dataset.Id, release = row.Id, action = MigrationReadyAction, stamp }) > 0; } private static string MigrationStamp(string? current, Release row) => $"[{current} → {row.Version} / {row.BaseRevision}] "; }