using FluentMigrator; namespace WmsLite.Api.Database.Migrations; /// /// 修复曾用 SqlSugar CodeFirst 加列但现有行为 NULL 的库(NOT NULL 约束失败)。 /// [Migration(6, "Backfill order line received_qty / line_status nulls")] public class BackfillOrderLineQtyAndStatus0006 : Migration { private static readonly string[] Tables = { "inbound_order_line", "outbound_order_line", "stocktake_order_line" }; public override void Up() { foreach (var table in Tables) { if (!Schema.Table(table).Exists()) { continue; } if (Schema.Table(table).Column("received_qty").Exists()) { Execute.Sql($"UPDATE {table} SET received_qty = 0 WHERE received_qty IS NULL;"); Execute.Sql($"ALTER TABLE {table} ALTER COLUMN received_qty SET DEFAULT 0;"); Execute.Sql($"ALTER TABLE {table} ALTER COLUMN received_qty SET NOT NULL;"); } if (Schema.Table(table).Column("line_status").Exists()) { Execute.Sql($"UPDATE {table} SET line_status = 1 WHERE line_status IS NULL;"); Execute.Sql($"ALTER TABLE {table} ALTER COLUMN line_status SET DEFAULT 1;"); Execute.Sql($"ALTER TABLE {table} ALTER COLUMN line_status SET NOT NULL;"); } } } public override void Down() { } }