From e3d3724ef67be2b5d1a46c83268cde4bef3cada2 Mon Sep 17 00:00:00 2001 From: Young Date: Sat, 19 Oct 2024 17:27:29 +0800 Subject: [PATCH] added database seed --- src/Infrastructure/Seed/DatabaseContext.cs | 23 ++++++ .../Seed/DatabaseContextExtensions.cs | 22 ++++++ src/Infrastructure/Seed/DatabaseSeed.cs | 76 +++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 src/Infrastructure/Seed/DatabaseContext.cs create mode 100644 src/Infrastructure/Seed/DatabaseContextExtensions.cs create mode 100644 src/Infrastructure/Seed/DatabaseSeed.cs diff --git a/src/Infrastructure/Seed/DatabaseContext.cs b/src/Infrastructure/Seed/DatabaseContext.cs new file mode 100644 index 0000000..037b359 --- /dev/null +++ b/src/Infrastructure/Seed/DatabaseContext.cs @@ -0,0 +1,23 @@ +using SqlSugar; + +namespace Infrastructure.Seed; + +public class DatabaseContext +{ + public DatabaseContext(ISqlSugarClient sqlSugarClient) + { + if (sqlSugarClient is SqlSugarScope scope) + { + Database = scope; + } + } + + public SqlSugarScope Database { get; private set; } + + public DbType DbType { get; set; } + + public SimpleClient GetEntityDatabase() where T : class, new() + { + return new SimpleClient(Database); + } +} \ No newline at end of file diff --git a/src/Infrastructure/Seed/DatabaseContextExtensions.cs b/src/Infrastructure/Seed/DatabaseContextExtensions.cs new file mode 100644 index 0000000..20dbe48 --- /dev/null +++ b/src/Infrastructure/Seed/DatabaseContextExtensions.cs @@ -0,0 +1,22 @@ +namespace Infrastructure.Seed; + +public static class DatabaseContextExtensions +{ + public static IServiceCollection GenerateTablesByClass(this IServiceCollection services, + DatabaseSeed databaseSeed + ) where T : class, new() + { + ArgumentNullException.ThrowIfNull(services); + databaseSeed.GenerateTablesByClass(); + return services; + } + + public static IServiceCollection GenerateSeedAsync(this IServiceCollection services, + DatabaseSeed databaseSeed, string seedFile) + where T : class, new() + { + ArgumentNullException.ThrowIfNull(services); + databaseSeed.GenerateSeedAsync(seedFile); + return services; + } +} \ No newline at end of file diff --git a/src/Infrastructure/Seed/DatabaseSeed.cs b/src/Infrastructure/Seed/DatabaseSeed.cs new file mode 100644 index 0000000..3c326e8 --- /dev/null +++ b/src/Infrastructure/Seed/DatabaseSeed.cs @@ -0,0 +1,76 @@ +using Infrastructure.Utils; +using SqlSugar; + +namespace Infrastructure.Seed; + +public class DatabaseSeed(DatabaseContext databaseContext, ILogger logger) +{ + public void GenerateTablesByClass() where T : class, new() + { + if (databaseContext.DbType == DbType.Oracle) + { + throw new InvalidOperationException("暂不支持Oracle数据库"); + } + else + { + databaseContext.Database.DbMaintenance.CreateDatabase(); + } + + try + { + var modelType = typeof(T); + var types = modelType.Assembly.DefinedTypes.Where(ti => + ti.Namespace == modelType.Namespace + & ti.IsClass + && ti.GetCustomAttribute() != null) + .Select(ti => ti.AsType()); + + foreach (var type in types) + { + var tableName = type.GetCustomAttribute()?.TableName ?? type.Name; + Console.WriteLine($"table is initializing: {tableName}"); + databaseContext.Database.CodeFirst.InitTables(type); + } + } + catch (Exception e) + { + logger.LogError(e.Message); + throw; + } + } + + public async void GenerateSeedAsync(string seedFile) where T : class, new() + { + if (string.IsNullOrEmpty(seedFile)) + { + throw new ArgumentException("Value cannot be null or empty.", nameof(seedFile)); + } + + if (!File.Exists(seedFile)) + { + throw new ArgumentException("seed file not exist", nameof(seedFile)); + } + + try + { + if (await databaseContext.Database.Queryable().AnyAsync()) + { + return; + } + + var json = await File.ReadAllTextAsync(seedFile, Encoding.UTF8); + if (string.IsNullOrEmpty(json)) + { + return; + } + + var data = json.Deserialize>(); + await databaseContext.GetEntityDatabase().InsertRangeAsync(data); + } + catch (Exception e) + { + logger.LogError(e.Message); + throw; + } + } +} \ No newline at end of file