MdpDatasetReleaseController.SchemaMigration.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  2. using Admin.NET.Plugin.AiDOP.Entity.DataPlatform;
  3. using Admin.NET.Plugin.ApprovalFlow;
  4. namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  5. public sealed record MdpSchemaMigrationConfirmation(string? EvidenceReference);
  6. public sealed partial class MdpIngestDatasetController
  7. {
  8. private const string MigrationReadyAction = "schema-migration-ready";
  9. [HttpPost("{id:long}/releases/{release:long}/migration-ready")]
  10. [DisplayName("登记 Schema 迁移完成")]
  11. public Task<IActionResult> RecordSchemaMigration(long id, long release, [FromQuery] long? factoryId,
  12. MdpSchemaMigrationConfirmation input) => Locked(id, factoryId, async (dataset, scope) =>
  13. {
  14. if (!await Can(PublishPermission)) return Forbid();
  15. var row = await db.Ado.SqlQuerySingleAsync<Release>(ReleaseSelect + " AND id=@release FOR UPDATE",
  16. new { tenant = scope.TenantId, factory = scope.FactoryId, id, release });
  17. Require(row != null && row.State == "REVIEW" && row.BaseRevision == dataset.Revision
  18. && row.EnvironmentKey == TargetEnvironment, "请在当前配置修订提交候选版本审批");
  19. var approved = await Approval(row!);
  20. Require(approved?.Status == FlowInstanceStatusEnum.Approved && await HasFactoryApproval(approved.Id, scope),
  21. "尚未取得人工审批通过记录");
  22. Require(await RequiresSchemaMigration(dataset, row!), "该版本无需登记 Schema 迁移");
  23. await CheckMigrationWindow(dataset);
  24. var reference = input.EvidenceReference?.Trim();
  25. Require(reference is { Length: > 0 and <= 250 } && !reference.Any(char.IsControl),
  26. "填写调用方迁移完成的工单或联测记录");
  27. await Log(id, release, MigrationReadyAction, MigrationStamp(dataset, row!) + reference);
  28. return NoContent();
  29. });
  30. private async Task EnsureSchemaMigrationReady(MdpIngestDataset dataset, Release row)
  31. {
  32. if (!await RequiresSchemaMigration(dataset, row)) return;
  33. await CheckMigrationWindow(dataset);
  34. Require(await HasMigrationConfirmation(dataset, row), "破坏性 Schema 变更须先登记调用方迁移完成");
  35. }
  36. private async Task<bool> RequiresSchemaMigration(MdpIngestDataset dataset, Release row) =>
  37. RequiresSchemaMigration(dataset, row, await ReadActiveMapping(dataset));
  38. private static bool RequiresSchemaMigration(MdpIngestDataset dataset, Release row, string? mapping) =>
  39. dataset.ActiveSchemaVersion is { Length: > 0 }
  40. && !string.Equals(mapping, row.MappingJson, StringComparison.Ordinal);
  41. private async Task<string?> ReadActiveMapping(MdpIngestDataset dataset)
  42. {
  43. if (dataset.ActiveSchemaVersion is not { Length: > 0 } version)
  44. return null;
  45. var archive = await db.Ado.SqlQuerySingleAsync<string>("""
  46. SELECT mapping_json FROM mdp_ingest_contract_archive
  47. WHERE tenant_id=@TenantId AND factory_id=@FactoryId AND dataset_id=@Id AND schema_version=@version
  48. """, new { dataset.TenantId, dataset.FactoryId, dataset.Id, version });
  49. if (archive is null && new PurchaseOrderSchemaRegistry().Find(dataset.DatasetKey, version) is not null)
  50. archive = "{}";
  51. return archive is null ? null : new PurchaseVersionConfiguration(dataset.DatasetKey, version, archive).MappingJson;
  52. }
  53. private async Task CheckMigrationWindow(MdpIngestDataset dataset)
  54. {
  55. Require(await ReadActiveMapping(dataset) != null, "当前版本的历史映射不可核对");
  56. Require(await Lifecycle(dataset.Id) == "PAUSED", "请先暂停接收,再按暂停后的修订创建迁移候选");
  57. var outstanding = await db.Ado.GetIntAsync("""
  58. SELECT COUNT(*) FROM mdp_ingest_receive_task WHERE dataset_id=@Id AND tenant_id=@TenantId
  59. AND status NOT IN ('SUCCEEDED','PARTIAL','QUARANTINED')
  60. """, new { dataset.Id, dataset.TenantId });
  61. Require(outstanding == 0, "请先处理完该数据集的在途和待处置批次");
  62. }
  63. private async Task<bool> HasMigrationConfirmation(MdpIngestDataset dataset, Release row)
  64. {
  65. var stamp = MigrationStamp(dataset, row);
  66. return await db.Ado.GetIntAsync("""
  67. SELECT COUNT(*) FROM mdp_ingest_release_log WHERE dataset_id=@datasetId AND release_id=@release
  68. AND action=@action AND LEFT(detail,CHAR_LENGTH(@stamp))=@stamp
  69. """, new { datasetId = dataset.Id, release = row.Id, action = MigrationReadyAction, stamp }) > 0;
  70. }
  71. private static string MigrationStamp(MdpIngestDataset dataset, Release row) =>
  72. $"[{dataset.ActiveSchemaVersion} → {row.Version} / {row.BaseRevision}] ";
  73. }