Files
WMSlite/WmsLite.Api/Database/migrations/0001_CreateCurrentSchema.cs
T
zhougfandCursor fa1b286463 Add WmsLite monorepo with JWT auth, tenant context, and project READMEs.
Implement API security baseline (sys_user, JWT, audit fields, transactions,
unique constraints) and Web real login with 401 handling. Document setup
and development workflows in root, WmsLite.Api, and WmsLite.Web README files.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-24 14:17:47 +08:00

221 lines
8.0 KiB
C#

using FluentMigrator;
using FluentMigrator.Builders.Create.Table;
namespace WmsLite.Api.Database.Migrations;
[Migration(1, "Create current WmsLite schema baseline")]
public class CreateCurrentSchema0001 : Migration
{
public override void Up()
{
CreateWarehouseTable();
CreateZoneTable();
CreateLocationTable();
CreateMaterialTable();
CreateContainerTable();
CreateInventoryTable();
CreateInventoryTransactionTable();
CreateSysTemplateTable();
CreateHttpAccessLogTable();
}
public override void Down()
{
Delete.Table("http_access_log");
Delete.Table("sys_template");
Delete.Table("inventory_transaction");
Delete.Table("inventory");
Delete.Table("container");
Delete.Table("material");
Delete.Table("location");
Delete.Table("zone");
Delete.Table("warehouse");
}
private void CreateWarehouseTable()
{
if (Schema.Table("warehouse").Exists())
{
return;
}
Create.Table("warehouse")
.WithBaseColumns()
.WithColumn("code").AsString(64).NotNullable()
.WithColumn("name").AsString(256).NotNullable()
.WithColumn("attrs").AsCustom("jsonb").Nullable();
}
private void CreateZoneTable()
{
if (Schema.Table("zone").Exists())
{
return;
}
Create.Table("zone")
.WithBaseColumns()
.WithColumn("warehouse_code").AsString(64).NotNullable()
.WithColumn("code").AsString(64).NotNullable()
.WithColumn("name").AsString(256).NotNullable()
.WithColumn("attrs").AsCustom("jsonb").Nullable();
}
private void CreateLocationTable()
{
if (Schema.Table("location").Exists())
{
return;
}
Create.Table("location")
.WithBaseColumns()
.WithColumn("warehouse_code").AsString(64).NotNullable()
.WithColumn("zone_code").AsString(64).NotNullable()
.WithColumn("code").AsString(64).NotNullable()
.WithColumn("name").AsString(256).Nullable()
.WithColumn("row_no").AsInt32().Nullable()
.WithColumn("column_no").AsInt32().Nullable()
.WithColumn("layer_no").AsInt32().Nullable()
.WithColumn("height").AsDecimal().Nullable()
.WithColumn("location_status").AsInt32().NotNullable()
.WithColumn("is_outer").AsBoolean().NotNullable()
.WithColumn("container_code").AsString(64).Nullable()
.WithColumn("attrs").AsCustom("jsonb").Nullable();
}
private void CreateMaterialTable()
{
if (Schema.Table("material").Exists())
{
return;
}
Create.Table("material")
.WithBaseColumns()
.WithColumn("code").AsString(64).NotNullable()
.WithColumn("name").AsString(256).NotNullable()
.WithColumn("spec").AsString(512).Nullable()
.WithColumn("unit").AsString(32).NotNullable()
.WithColumn("attrs").AsCustom("jsonb").Nullable();
}
private void CreateContainerTable()
{
if (Schema.Table("container").Exists())
{
return;
}
Create.Table("container")
.WithBaseColumns()
.WithColumn("code").AsString(64).NotNullable()
.WithColumn("name").AsString(256).Nullable()
.WithColumn("container_type").AsString(64).Nullable()
.WithColumn("attrs").AsCustom("jsonb").Nullable();
}
private void CreateInventoryTable()
{
if (Schema.Table("inventory").Exists())
{
return;
}
Create.Table("inventory")
.WithBaseColumns()
.WithColumn("inventory_kind").AsInt32().NotNullable()
.WithColumn("material_code").AsString(64).Nullable()
.WithColumn("container_code").AsString(64).Nullable()
.WithColumn("warehouse_code").AsString(64).NotNullable()
.WithColumn("zone_code").AsString(64).NotNullable()
.WithColumn("location_code").AsString(64).NotNullable()
.WithColumn("qty").AsDecimal().NotNullable()
.WithColumn("pending_inbound_qty").AsDecimal().NotNullable()
.WithColumn("pending_outbound_qty").AsDecimal().NotNullable()
.WithColumn("frozen_qty").AsDecimal().NotNullable()
.WithColumn("inventory_status").AsInt32().NotNullable()
.WithColumn("unit").AsString(32).NotNullable()
.WithColumn("attrs").AsCustom("jsonb").Nullable();
}
private void CreateInventoryTransactionTable()
{
if (Schema.Table("inventory_transaction").Exists())
{
return;
}
Create.Table("inventory_transaction")
.WithBaseColumns()
.WithColumn("inventory_id").AsInt64().NotNullable()
.WithColumn("movement_type").AsInt32().NotNullable()
.WithColumn("inventory_status").AsInt32().NotNullable()
.WithColumn("biz_no").AsString(128).Nullable()
.WithColumn("before_qty").AsDecimal().NotNullable()
.WithColumn("after_qty").AsDecimal().NotNullable()
.WithColumn("before_pending_inbound_qty").AsDecimal().NotNullable()
.WithColumn("after_pending_inbound_qty").AsDecimal().NotNullable()
.WithColumn("before_pending_outbound_qty").AsDecimal().NotNullable()
.WithColumn("after_pending_outbound_qty").AsDecimal().NotNullable()
.WithColumn("before_frozen_qty").AsDecimal().NotNullable()
.WithColumn("after_frozen_qty").AsDecimal().NotNullable()
.WithColumn("change_qty").AsDecimal().NotNullable()
.WithColumn("attrs").AsCustom("jsonb").Nullable();
}
private void CreateSysTemplateTable()
{
if (Schema.Table("sys_template").Exists())
{
return;
}
Create.Table("sys_template")
.WithBaseColumns()
.WithColumn("biz_type").AsString(64).NotNullable()
.WithColumn("biz_name").AsString(128).NotNullable()
.WithColumn("config_json").AsCustom("jsonb").Nullable();
}
private void CreateHttpAccessLogTable()
{
if (Schema.Table("http_access_log").Exists())
{
return;
}
Create.Table("http_access_log")
.WithColumn("id").AsInt64().PrimaryKey().NotNullable()
.WithColumn("trace_id").AsString(64).NotNullable()
.WithColumn("http_method").AsString(16).NotNullable()
.WithColumn("path").AsString(512).NotNullable()
.WithColumn("query_string").AsCustom("text").Nullable()
.WithColumn("status_code").AsInt32().NotNullable()
.WithColumn("request_body").AsCustom("text").Nullable()
.WithColumn("response_body").AsCustom("text").Nullable()
.WithColumn("duration_ms").AsInt32().Nullable()
.WithColumn("client_ip").AsString(64).Nullable()
.WithColumn("user_id").AsString(128).Nullable()
.WithColumn("create_time").AsDateTime().Nullable();
}
}
public static class MigrationTableExtensions
{
public static ICreateTableColumnOptionOrWithColumnSyntax WithBaseColumns(this ICreateTableWithColumnOrSchemaOrDescriptionSyntax table)
{
return table
.WithColumn("id").AsInt64().PrimaryKey().NotNullable()
.WithColumn("tenant_id").AsString(64).NotNullable()
.WithColumn("owner_code").AsString(64).NotNullable()
.WithColumn("create_time").AsDateTime().Nullable()
.WithColumn("create_by").AsString().Nullable()
.WithColumn("update_by").AsString().Nullable()
.WithColumn("update_time").AsDateTime().Nullable()
.WithColumn("is_deleted").AsBoolean().NotNullable()
.WithColumn("delete_time").AsDateTime().Nullable()
.WithColumn("delete_by").AsString().Nullable();
}
}