新增分段管理
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using MiGu.Server.Wms;
|
||||
using MiGu.Server.SimpleFields;
|
||||
|
||||
namespace MiGu.Server.Persistence;
|
||||
|
||||
@@ -38,6 +40,7 @@ public static class PlatformPersistence
|
||||
|
||||
services.AddScoped<WmsReferenceValidator>();
|
||||
services.AddScoped<WmsService>();
|
||||
services.AddScoped<SimpleFieldService>();
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -46,6 +49,97 @@ public static class PlatformPersistence
|
||||
using var scope = services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<PlatformDbContext>();
|
||||
await db.Database.EnsureCreatedAsync();
|
||||
// EnsureCreated 只在「库文件不存在」时建表;已有 platform.db 时新增实体不会自动补表。
|
||||
await EnsureSimpleFieldsTableAsync(db);
|
||||
}
|
||||
|
||||
/// <summary>为已存在的数据库补建 simple_fields 表(幂等)。</summary>
|
||||
private static async Task EnsureSimpleFieldsTableAsync(PlatformDbContext db)
|
||||
{
|
||||
if (db.Database.IsSqlite())
|
||||
{
|
||||
await db.Database.ExecuteSqlRawAsync("""
|
||||
CREATE TABLE IF NOT EXISTS simple_fields (
|
||||
id TEXT NOT NULL CONSTRAINT PK_simple_fields PRIMARY KEY,
|
||||
car_type TEXT NOT NULL DEFAULT '',
|
||||
field_type TEXT NOT NULL,
|
||||
"key" TEXT NOT NULL,
|
||||
value TEXT NOT NULL DEFAULT '',
|
||||
data_type TEXT NOT NULL DEFAULT '',
|
||||
chinese TEXT,
|
||||
english TEXT,
|
||||
other TEXT NOT NULL DEFAULT '',
|
||||
is_default INTEGER NOT NULL,
|
||||
create_time TEXT NOT NULL,
|
||||
update_time TEXT NOT NULL
|
||||
);
|
||||
""");
|
||||
// 须先删旧索引 (field_type, other, key):把 other 清空为「其他语言」后会与旧唯一约束冲突。
|
||||
await db.Database.ExecuteSqlRawAsync("DROP INDEX IF EXISTS IX_simple_fields_field_type_other_key;");
|
||||
await db.Database.ExecuteSqlRawAsync("""
|
||||
UPDATE simple_fields SET car_type = other
|
||||
WHERE (car_type IS NULL OR car_type = '') AND other <> '';
|
||||
""");
|
||||
await db.Database.ExecuteSqlRawAsync("""
|
||||
UPDATE simple_fields SET other = ''
|
||||
WHERE other <> '' AND other = car_type;
|
||||
""");
|
||||
await db.Database.ExecuteSqlRawAsync("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS IX_simple_fields_car_type_field_type_key
|
||||
ON simple_fields (car_type, field_type, "key");
|
||||
""");
|
||||
return;
|
||||
}
|
||||
|
||||
// 非 SQLite:表不存在时尝试按当前模型创建(已有库不会走 EnsureCreated)。
|
||||
if (!await TableExistsAsync(db, "simple_fields"))
|
||||
{
|
||||
var creator = db.GetService<Microsoft.EntityFrameworkCore.Storage.IRelationalDatabaseCreator>();
|
||||
await creator.CreateTablesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查表是否存在
|
||||
/// </summary>
|
||||
/// <param name="db">数据库上下文</param>
|
||||
/// <param name="table">表名</param>
|
||||
/// <returns>表是否存在</returns>
|
||||
private static async Task<bool> TableExistsAsync(PlatformDbContext db, string table)
|
||||
{
|
||||
var conn = db.Database.GetDbConnection();
|
||||
if (conn.State != System.Data.ConnectionState.Open)
|
||||
await conn.OpenAsync();
|
||||
try
|
||||
{
|
||||
await using var cmd = conn.CreateCommand();
|
||||
if (db.Database.IsSqlServer())
|
||||
{
|
||||
cmd.CommandText = "SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @t";
|
||||
var p = cmd.CreateParameter(); p.ParameterName = "@t"; p.Value = table; cmd.Parameters.Add(p);
|
||||
}
|
||||
else if (db.Database.IsNpgsql())
|
||||
{
|
||||
cmd.CommandText = "SELECT 1 FROM information_schema.tables WHERE table_name = @t";
|
||||
var p = cmd.CreateParameter(); p.ParameterName = "@t"; p.Value = table; cmd.Parameters.Add(p);
|
||||
}
|
||||
else if (db.Database.IsMySql())
|
||||
{
|
||||
cmd.CommandText = "SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = @t";
|
||||
var p = cmd.CreateParameter(); p.ParameterName = "@t"; p.Value = table; cmd.Parameters.Add(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var result = await cmd.ExecuteScalarAsync();
|
||||
return result != null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (conn.State == System.Data.ConnectionState.Open)
|
||||
await conn.CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private static string ResolveConnectionString(IConfiguration configuration, IWebHostEnvironment env, string provider)
|
||||
|
||||
Reference in New Issue
Block a user