Added db migrations

This commit is contained in:
2026-01-05 21:54:15 +01:00
commit 0a57915447
20 changed files with 439 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
obj/
Debug/
.idea/
.vscode/
*.suo
*.user
*.userosscache
*.sln.docstates

View File

@@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

View File

@@ -0,0 +1,28 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dc_bot.API", "dc_bot.API\dc_bot.API.csproj", "{473D45A5-BC19-4DC3-A2A2-E57B75C6C0F2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dc_bot.db", "dc_bot.db\dc_bot.db.csproj", "{BFAA7A97-736C-4E78-B83C-2F97D4E6797F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "utils", "utils\utils.csproj", "{3BF8EE60-57BA-4526-AC5A-47E3149CEDB4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{473D45A5-BC19-4DC3-A2A2-E57B75C6C0F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{473D45A5-BC19-4DC3-A2A2-E57B75C6C0F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{473D45A5-BC19-4DC3-A2A2-E57B75C6C0F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{473D45A5-BC19-4DC3-A2A2-E57B75C6C0F2}.Release|Any CPU.Build.0 = Release|Any CPU
{BFAA7A97-736C-4E78-B83C-2F97D4E6797F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BFAA7A97-736C-4E78-B83C-2F97D4E6797F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFAA7A97-736C-4E78-B83C-2F97D4E6797F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFAA7A97-736C-4E78-B83C-2F97D4E6797F}.Release|Any CPU.Build.0 = Release|Any CPU
{3BF8EE60-57BA-4526-AC5A-47E3149CEDB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3BF8EE60-57BA-4526-AC5A-47E3149CEDB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3BF8EE60-57BA-4526-AC5A-47E3149CEDB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3BF8EE60-57BA-4526-AC5A-47E3149CEDB4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,23 @@
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["dc_bot.API/dc_bot.API.csproj", "dc_bot.API/"]
RUN dotnet restore "dc_bot.API/dc_bot.API.csproj"
COPY . .
WORKDIR "/src/dc_bot.API"
RUN dotnet build "./dc_bot.API.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./dc_bot.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "dc_bot.API.dll"]

View File

@@ -0,0 +1,34 @@
using dc_bot.db;
using Microsoft.EntityFrameworkCore;
using utils;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContextPool<AppDbContext>(opt =>
opt.UseNpgsql(ConnectionStringHelper.GetConnectionString()));
builder.Services.AddScoped<MigrationService>();
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
context.Database.EnsureCreated();
var migrationService = scope.ServiceProvider.GetRequiredService<MigrationService>();
await migrationService.Migrate();
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.Run();

View File

@@ -0,0 +1,28 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5002",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DB_HOST": "localhost",
"DB_PORT": "5438",
"DB_NAME": "dc_bot",
"DB_USER": "dc_bot",
"DB_PASSWORD": "dc_bot"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7067;http://localhost:5002",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
</ItemGroup>
<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\dc_bot.db\dc_bot.db.csproj" />
<ProjectReference Include="..\utils\utils.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,26 @@
using dc_bot.db.Model;
using dc_bot.db.Model.administration;
using dc_bot.db.Model.system;
using Microsoft.EntityFrameworkCore;
namespace dc_bot.db;
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
public required DbSet<ExecutedMigration> ExecutedMigrations { get; set; }
public required DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ExecutedMigration>(e =>
{
e.ToTable("_executed_migrations", "system");
e.HasKey(e => e.MigrationId);
});
modelBuilder.Entity<User>(e =>
{
e.ToTable("users", "administration");
});
}
}

View File

@@ -0,0 +1,74 @@
using System.Runtime.CompilerServices;
using dc_bot.db.Model.system;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace dc_bot.db;
public class MigrationService
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly string MigrationDirectory = Environment.GetEnvironmentVariable("DB_MIGRATION_DIR") ??
Path.Combine(AppContext.BaseDirectory, "Scripts");
public MigrationService(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
}
private List<string> GetMigrations()
{
if (!Directory.Exists(MigrationDirectory))
return new List<string>();
var files = Directory.GetFiles(MigrationDirectory, "*.sql")
.Select(Path.GetFileName)
.OrderBy(f => f)
.ToList();
return files;
}
public async Task Migrate()
{
using var scope = _scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
var appliedMigrations = await context.ExecutedMigrations
.Select(m => m.MigrationId)
.ToListAsync();
var appliedMigrationNames = appliedMigrations
.Select(m => m.Replace(".sql", string.Empty))
.ToHashSet(StringComparer.OrdinalIgnoreCase);
var migrations = GetMigrations()
.Where(m => !appliedMigrationNames.Contains(m))
.ToList();
foreach (var migration in migrations)
{
try
{
var migrationPath = Path.Combine(MigrationDirectory, migration);
var sql = await File.ReadAllTextAsync(migrationPath);
await context.Database.ExecuteSqlRawAsync(sql);
var executedMigration = new ExecutedMigration
{
MigrationId = migration,
Created = DateTime.UtcNow,
Updated = DateTime.UtcNow
};
context.ExecutedMigrations.Add(executedMigration);
await context.SaveChangesAsync();
}
catch (Exception ex)
{
Console.WriteLine($"Error applying migration {migration}: {ex.Message}");
throw;
}
}
}
}

View File

@@ -0,0 +1,9 @@
namespace dc_bot.db.Model;
public interface IDbModel
{
public bool Deleted { get; set; }
public Double EditorId { get; set; }
public DateTimeOffset Created { get; set; }
public DateTimeOffset Updated { get; set; }
}

View File

@@ -0,0 +1,12 @@
namespace dc_bot.db.Model.administration;
public class User : IDbModel
{
public Double Id { get; set; }
public required string KeycloakId { get; init; }
public bool Deleted { get; set; }
public double EditorId { get; set; }
public DateTimeOffset Created { get; set; }
public DateTimeOffset Updated { get; set; }
}

View File

@@ -0,0 +1,10 @@
using Microsoft.EntityFrameworkCore;
namespace dc_bot.db.Model.system;
public class ExecutedMigration
{
public required string MigrationId { get; set; }
public DateTimeOffset Created { get; set; }
public DateTimeOffset Updated { get; set; }
}

View File

@@ -0,0 +1,9 @@
CREATE SCHEMA IF NOT EXISTS public;
CREATE SCHEMA IF NOT EXISTS system;
CREATE TABLE IF NOT EXISTS system._executed_migrations
(
MigrationId VARCHAR(255) PRIMARY KEY,
Created timestamptz NOT NULL DEFAULT NOW(),
Updated timestamptz NOT NULL DEFAULT NOW()
);

View File

@@ -0,0 +1,37 @@
CREATE OR REPLACE FUNCTION public.history_trigger_function()
RETURNS TRIGGER AS
$$
DECLARE
schema_name TEXT;
history_table_name TEXT;
BEGIN
-- Construct the name of the history table based on the current table
schema_name := TG_TABLE_SCHEMA;
history_table_name := TG_TABLE_NAME || '_history';
IF (TG_OP = 'INSERT') THEN
RETURN NEW;
END IF;
-- Insert the old row into the history table on UPDATE or DELETE
IF (TG_OP = 'UPDATE' OR TG_OP = 'DELETE') THEN
EXECUTE format(
'INSERT INTO %I.%I SELECT ($1).*',
schema_name,
history_table_name
)
USING OLD;
END IF;
-- For UPDATE, update the UpdatedUtc column and return the new row
IF (TG_OP = 'UPDATE') THEN
NEW.updated := NOW(); -- Update the UpdatedUtc column
RETURN NEW;
END IF;
-- For DELETE, return OLD to allow the deletion
IF (TG_OP = 'DELETE') THEN
RETURN OLD;
END IF;
END;
$$ LANGUAGE plpgsql;

View File

@@ -0,0 +1,26 @@
CREATE SCHEMA IF NOT EXISTS administration;
CREATE TABLE IF NOT EXISTS administration.users
(
Id SERIAL PRIMARY KEY,
KeycloakId UUID NOT NULL,
-- for history
Deleted BOOLEAN NOT NULL DEFAULT FALSE,
EditorId INT NULL REFERENCES administration.users (Id),
Created timestamptz NOT NULL DEFAULT NOW(),
Updated timestamptz NOT NULL DEFAULT NOW(),
CONSTRAINT UC_KeycloakId UNIQUE (KeycloakId)
);
CREATE TABLE IF NOT EXISTS administration.users_history
(
LIKE administration.users
);
CREATE TRIGGER users_history_trigger
BEFORE INSERT OR UPDATE OR DELETE
ON administration.users
FOR EACH ROW
EXECUTE FUNCTION public.history_trigger_function();

View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Update="Scripts\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,15 @@
namespace utils;
public static class ConnectionStringHelper
{
public static string GetConnectionString()
{
var host = Environment.GetEnvironmentVariable("DB_HOST") ?? "localhost";
var port = Environment.GetEnvironmentVariable("DB_PORT") ?? "5432";
var database = Environment.GetEnvironmentVariable("DB_NAME") ?? "dc_bot";
var username = Environment.GetEnvironmentVariable("DB_USER") ?? "postgres";
var password = Environment.GetEnvironmentVariable("DB_PASSWORD") ?? "password";
return $"Host={host};Port={port};Database={database};Username={username};Password={password}";
}
}

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>