| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 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<IActionResult> 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<Release>(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),
- "尚未取得人工审批通过记录");
- Require(await RequiresSchemaMigration(dataset, row!), "该版本无需登记 Schema 迁移");
- await CheckMigrationWindow(dataset);
- var reference = input.EvidenceReference?.Trim();
- Require(reference is { Length: > 0 and <= 250 } && !reference.Any(char.IsControl),
- "填写调用方迁移完成的工单或联测记录");
- await Log(id, release, MigrationReadyAction, MigrationStamp(dataset, row!) + reference);
- return NoContent();
- });
- private async Task EnsureSchemaMigrationReady(MdpIngestDataset dataset, Release row)
- {
- if (!await RequiresSchemaMigration(dataset, row)) return;
- await CheckMigrationWindow(dataset);
- Require(await HasMigrationConfirmation(dataset, row), "破坏性 Schema 变更须先登记调用方迁移完成");
- }
- private async Task<bool> RequiresSchemaMigration(MdpIngestDataset dataset, Release row) =>
- RequiresSchemaMigration(dataset, row, await ReadActiveMapping(dataset));
- private static bool RequiresSchemaMigration(MdpIngestDataset dataset, Release row, string? mapping) =>
- dataset.ActiveSchemaVersion is { Length: > 0 }
- && !string.Equals(mapping, row.MappingJson, StringComparison.Ordinal);
- private async Task<string?> ReadActiveMapping(MdpIngestDataset dataset)
- {
- if (dataset.ActiveSchemaVersion is not { Length: > 0 } version)
- return null;
- var archive = await db.Ado.SqlQuerySingleAsync<string>("""
- 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).MappingJson;
- }
- private async Task CheckMigrationWindow(MdpIngestDataset dataset)
- {
- Require(await ReadActiveMapping(dataset) != 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<bool> HasMigrationConfirmation(MdpIngestDataset dataset, Release row)
- {
- var stamp = MigrationStamp(dataset, 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(MdpIngestDataset dataset, Release row) =>
- $"[{dataset.ActiveSchemaVersion} → {row.Version} / {row.BaseRevision}] ";
- }
|