mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-07-12 16:03:27 +08:00
feat(DatabaseInitializer): add database migration logic and clean up initialization process
This commit is contained in:
@@ -38,7 +38,9 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
|
||||
var userCount = await context.Users.CountAsync();
|
||||
if (userCount == 0)
|
||||
{
|
||||
var role = await context.Roles.FirstOrDefaultAsync(r => r.Id == 1);
|
||||
user.RoleId = 1;
|
||||
user.Role = role;
|
||||
}
|
||||
context.Users.Add(user);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Foxel.Models.DataBase;
|
||||
using Foxel.Services.Configuration;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -18,6 +16,9 @@ public class DatabaseInitializer(
|
||||
{
|
||||
logger.LogInformation("开始检查数据库初始化状态...");
|
||||
|
||||
// 执行数据库迁移
|
||||
await MigrateDatabaseAsync();
|
||||
|
||||
// 检查是否已经完成初始化
|
||||
if (await configService.ExistsAsync(InitializationFlag) &&
|
||||
configService[InitializationFlag] == "true")
|
||||
@@ -41,7 +42,8 @@ public class DatabaseInitializer(
|
||||
// 初始化GitHub认证配置
|
||||
await EnsureConfigExistsAsync("Authentication:GitHubClientId", "placeholder_replace_with_actual_github_client_id");
|
||||
await EnsureConfigExistsAsync("Authentication:GitHubClientSecret", "placeholder_replace_with_actual_github_client_secret");
|
||||
await EnsureConfigExistsAsync("Authentication:GitHubClientSecret", "");
|
||||
await EnsureConfigExistsAsync("Authentication:GitHubCallbackUrl", "");
|
||||
|
||||
// 初始化AI相关配置
|
||||
await EnsureConfigExistsAsync("AI:ApiEndpoint", "");
|
||||
await EnsureConfigExistsAsync("AI:ApiKey", "");
|
||||
@@ -63,6 +65,24 @@ public class DatabaseInitializer(
|
||||
logger.LogInformation("数据库配置初始化完成");
|
||||
}
|
||||
|
||||
private async Task MigrateDatabaseAsync()
|
||||
{
|
||||
logger.LogInformation("开始执行数据库迁移...");
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync();
|
||||
await context.Database.MigrateAsync();
|
||||
logger.LogInformation("数据库迁移完成");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning("数据库迁移过程中出现警告或错误: {Error}", ex.Message);
|
||||
logger.LogInformation("尝试确保数据库已创建...");
|
||||
await using var context = await contextFactory.CreateDbContextAsync();
|
||||
await context.Database.EnsureCreatedAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task EnsureConfigExistsAsync(string key, string value)
|
||||
{
|
||||
if (!await configService.ExistsAsync(key))
|
||||
@@ -93,25 +113,4 @@ public class DatabaseInitializer(
|
||||
}
|
||||
logger.LogInformation("请注意,第一个注册的用户将自动成为管理员");
|
||||
}
|
||||
|
||||
private string GenerateRandomPassword(int length)
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
var random = new Random();
|
||||
var password = new StringBuilder(length);
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
password.Append(chars[random.Next(chars.Length)]);
|
||||
}
|
||||
|
||||
return password.ToString();
|
||||
}
|
||||
|
||||
private string HashPassword(string password)
|
||||
{
|
||||
using var sha256 = SHA256.Create();
|
||||
var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));
|
||||
return Convert.ToBase64String(hashedBytes);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user