-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:add db persistance.(on development).
- Loading branch information
1 parent
bfe8ca9
commit 437c1e0
Showing
16 changed files
with
1,068 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/LLMService.DataProvider.Migrator/ChatDbContextFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using LLMService.DataProvider.Relational.Data; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Design; | ||
using Microsoft.Extensions.Configuration; | ||
using Org.BouncyCastle.Tls; | ||
|
||
namespace LLMService.DataProvider.Migrator | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
internal class ChatDbContextFactory : ChatDbContextFactoryGeneric<ChatDbContext> | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <seealso cref="Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory{TContext}" /> | ||
internal class ChatDbContextFactoryGeneric<TContext> : IDesignTimeDbContextFactory<TContext> | ||
where TContext : DbContext | ||
{ | ||
const string connectionKey = "DbConnection"; | ||
const string assemblyName = "LLMService.DataProvider.Migrator"; | ||
|
||
/// <summary> | ||
/// Creates a new instance of a derived context. | ||
/// </summary> | ||
/// <param name="args">Arguments provided by the design-time service.</param> | ||
/// <returns> | ||
/// An instance of <typeparamref name="TContext" />. | ||
/// </returns> | ||
public TContext CreateDbContext(string[] args) | ||
{ | ||
// | ||
var configuration = new ConfigurationBuilder() | ||
.SetBasePath(Directory.GetCurrentDirectory()) | ||
.AddJsonFile("appsettings.json") | ||
.AddCommandLine(args) | ||
.Build(); | ||
var dbContextBuilder = new DbContextOptionsBuilder<TContext>(); | ||
var connectionString = configuration.GetConnectionString(connectionKey); | ||
|
||
string migrationDbProvider = configuration["DbProvider"] ?? "SqlServer"; | ||
//string contextTypeName = configuration["contextType"] ?? nameof(TContext); | ||
/* | ||
* Sample DI code goes here: | ||
services.AddDbContext<TContext>( | ||
options => _ = provider switch | ||
{ | ||
"Sqlite" => options.UseSqlite( | ||
configuration.GetConnectionString("SqliteConnection"), | ||
x => x.MigrationsAssembly("SqliteMigrations")), | ||
"SqlServer" => options.UseSqlServer( | ||
configuration.GetConnectionString("SqlServerConnection"), | ||
x => x.MigrationsAssembly("SqlServerMigrations")), | ||
_ => throw new Exception($"Unsupported provider: {provider}") | ||
}); | ||
* | ||
*/ | ||
|
||
if (migrationDbProvider.Contains("SqlServer", StringComparison.InvariantCultureIgnoreCase)){ | ||
dbContextBuilder.UseSqlServer(connectionString, action => action.MigrationsAssembly(assemblyName)); | ||
} | ||
else if(migrationDbProvider.Contains("MySql", StringComparison.InvariantCultureIgnoreCase)) | ||
{ | ||
dbContextBuilder.UseMySQL(connectionString, action => action.MigrationsAssembly(assemblyName)); | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException("Invalid DbProvider"); | ||
} | ||
|
||
var dbContextInstance = (TContext)Activator.CreateInstance(typeof(TContext), dbContextBuilder.Options); | ||
|
||
return dbContextInstance; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/LLMService.DataProvider.Migrator/LLMService.DataProvider.Migrator.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\LLMService.DataProvider.Relational\LLMService.DataProvider.Relational.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> | ||
<PackageReference Include="MySql.EntityFrameworkCore" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<Folder Include="Migrations\" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# 项目说明 | ||
|
||
本项目为独立的数据库迁移项目,支持在`appsettings.json`中配置数据库提供程序,目前支持`SqlServer`和`MySql`. | ||
|
||
## 目录结构 | ||
|
||
例如,有3次迁移Migration1,Migration2,Migration3,文件结构大致如下: | ||
|
||
``` | ||
. | ||
├── Migrations /*迁移文件夹*/ | ||
│ ├── 时间戳1_Migration1.cs /*第1次迁移*/ | ||
│ ├── 时间戳2_Migration2.cs /*第2次迁移*/ | ||
│ ├── 时间戳3_Migration3.cs /*第3次迁移*/ | ||
│ ├── ChatDbContextModelSnapshot.cs /*迁移快照文件*/ | ||
│ └── Scripts /*迁移脚本文件夹*/ | ||
│ ├── Migration1.sql /*第1次迁移脚本*/ | ||
│ ├── Migration2.sql /*第2次迁移脚本*/ | ||
│ └── Migration3.sql /*第3次迁移脚本*/ | ||
├── ChatDbContextFactory.cs /*迁移配置程序*/ | ||
└── README.md /*说明文件*/ | ||
``` | ||
|
||
#### 创建迁移 | ||
|
||
以`NewMigration`为迁移名称 | ||
|
||
##### VisualStudio 包管理器命令行 | ||
``` | ||
add-migration -c ChatDbContext NewMigration | ||
``` | ||
|
||
#### 创建迁移脚本 | ||
|
||
`NewMigration`为本次迁移名称,`LastMigration`为上次迁移名称(可选),加入`-from` 参数创建`LastMigration`之后所做的增量脚本 | ||
|
||
##### VisualStudio 包管理器命令行 | ||
|
||
需要注意当前命令行执行的目录. | ||
|
||
+ 仅创建迁移增量脚本 | ||
``` | ||
script-migration -o .\src\LLMService.DataProvider.Migrator\Migrations\Scripts\NewMigration.sql -Idempotent -from LastMigration | ||
``` | ||
|
||
+ 创建整个数据库的建库脚本 | ||
|
||
`DbProvider`参数可选值:SqlServer,MySql | ||
``` | ||
script-dbcontext -c ChatDbContext -Args "--DbProvider SqlServer" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"ConnectionStrings": { | ||
"DbConnection": "Data Source=(localdb)\\mssqllocaldb;Database=LLMServiceHub_Db;Trusted_Connection=True;MultipleActiveResultSets=true" | ||
//"DbConnection": "Server=.,3306;Database=LLMServiceHub_Db;Uid=sa;Pwd=pass;" | ||
}, | ||
//database provider,optional values:SqlServer|MySql | ||
"DbProvider": "SqlServer" | ||
} | ||
|
Oops, something went wrong.