From 3feb71493f846f02bcfce351e32c464e0d190373 Mon Sep 17 00:00:00 2001 From: shiyu Date: Fri, 23 May 2025 21:31:04 +0800 Subject: [PATCH] feat(DatabaseInitializer): add database migration logic and clean up initialization process --- Dockerfile | 19 +++------ Program.cs | 2 + Services/Auth/AuthService.cs | 2 + Services/Initializer/DatabaseInitializer.cs | 47 ++++++++++----------- 4 files changed, 32 insertions(+), 38 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4026277..f9c856c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,29 +1,21 @@ FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base WORKDIR /app -EXPOSE 8080 -EXPOSE 8081 EXPOSE 80 FROM oven/bun:alpine AS build-frontend -WORKDIR /src/View -COPY View/package*.json ./ +WORKDIR /src/Web +COPY Web/package*.json ./ RUN bun install -COPY View/ ./ +COPY Web/ ./ RUN bun run build FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build ARG BUILD_CONFIGURATION=Release -ENV DEFAULT_CONNECTION="Host=yourhost;Username=foxel;Password=foxel;Database=foxel" WORKDIR /src COPY ["Foxel.csproj", "./"] RUN dotnet restore "Foxel.csproj" COPY . . WORKDIR "/src/" -# 安装EF Core工具并执行数据库迁移 -RUN dotnet tool install --global dotnet-ef -ENV PATH="$PATH:/root/.dotnet/tools" -RUN dotnet ef migrations add InitialCreate -- --environment Production -RUN dotnet ef database update -- --environment Production RUN dotnet build "./Foxel.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build AS publish @@ -32,11 +24,10 @@ RUN dotnet publish "./Foxel.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:U FROM base AS final WORKDIR /app -ENV DEFAULT_CONNECTION="Host=yourhost;Username=foxel;Password=foxel;Database=foxel" COPY --from=publish /app/publish . RUN apt-get update && apt-get install -y nginx && rm -rf /var/lib/apt/lists/* -COPY --from=build-frontend /src/View/dist /var/www/html -COPY /View/nginx.conf /etc/nginx/nginx.conf +COPY --from=build-frontend /src/Web/dist /var/www/html +COPY /Web/nginx.conf /etc/nginx/nginx.conf RUN mkdir -p /var/lib/nginx/body /var/cache/nginx /var/run/nginx /app/Uploads \ && chown -R $APP_UID:$APP_UID /var/lib/nginx /var/cache/nginx /var/run/nginx /var/log/nginx /etc/nginx /var/www/html /app/Uploads \ diff --git a/Program.cs b/Program.cs index 001f6f7..dccc7d4 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,8 @@ +using Foxel; using Foxel.Extensions; using Foxel.Services.Initializer; using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); var environment = builder.Environment; diff --git a/Services/Auth/AuthService.cs b/Services/Auth/AuthService.cs index 48bf1bf..7b8c6eb 100644 --- a/Services/Auth/AuthService.cs +++ b/Services/Auth/AuthService.cs @@ -38,7 +38,9 @@ public class AuthService(IDbContextFactory 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(); diff --git a/Services/Initializer/DatabaseInitializer.cs b/Services/Initializer/DatabaseInitializer.cs index f1bdbe9..03336d5 100644 --- a/Services/Initializer/DatabaseInitializer.cs +++ b/Services/Initializer/DatabaseInitializer.cs @@ -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); - } }