mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Merge pull request #128 from aaronpo97/127-api-container-production-db
127: Add docker configuration for a production/development database and an API container
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://raw.githubusercontent.com/belav/csharpier/main/src/CSharpier.Cli/schema.json",
|
|
||||||
"printWidth": 80,
|
"printWidth": 80,
|
||||||
"useTabs": false,
|
"useTabs": false,
|
||||||
"tabWidth": 4,
|
"tabWidth": 4,
|
||||||
|
|||||||
7
.gitignore
vendored
7
.gitignore
vendored
@@ -481,4 +481,9 @@ FodyWeavers.xsd
|
|||||||
.fake
|
.fake
|
||||||
.idea
|
.idea
|
||||||
|
|
||||||
*.feature.cs
|
*.feature.cs
|
||||||
|
|
||||||
|
|
||||||
|
database
|
||||||
|
|
||||||
|
.env.*
|
||||||
75
docker-compose.dev.yaml
Normal file
75
docker-compose.dev.yaml
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
services:
|
||||||
|
sqlserver:
|
||||||
|
image: mcr.microsoft.com/mssql/server:2022-latest
|
||||||
|
platform: linux/amd64
|
||||||
|
container_name: dev-env-sqlserver
|
||||||
|
environment:
|
||||||
|
ACCEPT_EULA: "Y"
|
||||||
|
SA_PASSWORD: "${SA_PASSWORD}"
|
||||||
|
MSSQL_PID: "Express"
|
||||||
|
ports:
|
||||||
|
- "1433:1433"
|
||||||
|
volumes:
|
||||||
|
- sqlserverdata-dev:/var/opt/mssql
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P '${SA_PASSWORD}' -C -Q 'SELECT 1' || exit 1"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 12
|
||||||
|
start_period: 30s
|
||||||
|
networks:
|
||||||
|
- devnet
|
||||||
|
database.migrations:
|
||||||
|
image: database.migrations
|
||||||
|
container_name: dev-env-database-migrations
|
||||||
|
depends_on:
|
||||||
|
sqlserver:
|
||||||
|
condition: service_healthy
|
||||||
|
build:
|
||||||
|
context: ./src/Core/Database
|
||||||
|
dockerfile: Database.Migrations/Dockerfile
|
||||||
|
args:
|
||||||
|
BUILD_CONFIGURATION: Release
|
||||||
|
APP_UID: 1000
|
||||||
|
environment:
|
||||||
|
DOTNET_RUNNING_IN_CONTAINER: "true"
|
||||||
|
DB_CONNECTION_STRING: "${DB_CONNECTION_STRING}"
|
||||||
|
MASTER_DB_CONNECTION_STRING: "${MASTER_DB_CONNECTION_STRING}"
|
||||||
|
restart: "no"
|
||||||
|
networks:
|
||||||
|
- devnet
|
||||||
|
api.core:
|
||||||
|
image: api.core
|
||||||
|
container_name: dev-env-api-core
|
||||||
|
depends_on:
|
||||||
|
sqlserver:
|
||||||
|
condition: service_healthy
|
||||||
|
build:
|
||||||
|
context: ./src/Core
|
||||||
|
dockerfile: API/API.Core/Dockerfile
|
||||||
|
args:
|
||||||
|
BUILD_CONFIGURATION: Release
|
||||||
|
APP_UID: 1000
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
- "8081:8081"
|
||||||
|
environment:
|
||||||
|
ASPNETCORE_ENVIRONMENT: "Development"
|
||||||
|
ASPNETCORE_URLS: "http://0.0.0.0:8080"
|
||||||
|
DOTNET_RUNNING_IN_CONTAINER: "true"
|
||||||
|
DB_CONNECTION_STRING: "${DB_CONNECTION_STRING}"
|
||||||
|
restart: unless-stopped
|
||||||
|
networks:
|
||||||
|
- devnet
|
||||||
|
volumes:
|
||||||
|
- nuget-cache-dev:/root/.nuget/packages
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
sqlserverdata-dev:
|
||||||
|
driver: local
|
||||||
|
nuget-cache-dev:
|
||||||
|
driver: local
|
||||||
|
|
||||||
|
networks:
|
||||||
|
devnet:
|
||||||
|
driver: bridge
|
||||||
76
docker-compose.prod.yaml
Normal file
76
docker-compose.prod.yaml
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
services:
|
||||||
|
sqlserver:
|
||||||
|
image: mcr.microsoft.com/mssql/server:2022-latest
|
||||||
|
platform: linux/amd64
|
||||||
|
container_name: prod-env-sqlserver
|
||||||
|
environment:
|
||||||
|
ACCEPT_EULA: "Y"
|
||||||
|
SA_PASSWORD: "${SA_PASSWORD}"
|
||||||
|
MSSQL_PID: "Express"
|
||||||
|
volumes:
|
||||||
|
- sqlserverdata-prod:/var/opt/mssql
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P '${SA_PASSWORD}' -C -Q 'SELECT 1' || exit 1"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 12
|
||||||
|
start_period: 30s
|
||||||
|
networks:
|
||||||
|
- prodnet
|
||||||
|
|
||||||
|
database.migrations:
|
||||||
|
image: database.migrations
|
||||||
|
container_name: prod-env-database-migrations
|
||||||
|
depends_on:
|
||||||
|
sqlserver:
|
||||||
|
condition: service_healthy
|
||||||
|
build:
|
||||||
|
context: ./src/Core/Database
|
||||||
|
dockerfile: Database.Migrations/Dockerfile
|
||||||
|
args:
|
||||||
|
BUILD_CONFIGURATION: Release
|
||||||
|
APP_UID: 1000
|
||||||
|
environment:
|
||||||
|
DOTNET_RUNNING_IN_CONTAINER: "true"
|
||||||
|
DB_CONNECTION_STRING: "${DB_CONNECTION_STRING}"
|
||||||
|
MASTER_DB_CONNECTION_STRING: "${MASTER_DB_CONNECTION_STRING}"
|
||||||
|
restart: "no"
|
||||||
|
networks:
|
||||||
|
- prodnet
|
||||||
|
|
||||||
|
api.core:
|
||||||
|
image: api.core
|
||||||
|
container_name: prod-env-api-core
|
||||||
|
depends_on:
|
||||||
|
sqlserver:
|
||||||
|
condition: service_healthy
|
||||||
|
build:
|
||||||
|
context: ./src/Core
|
||||||
|
dockerfile: API/API.Core/Dockerfile
|
||||||
|
args:
|
||||||
|
BUILD_CONFIGURATION: Release
|
||||||
|
APP_UID: 1000
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
- "8081:8081"
|
||||||
|
environment:
|
||||||
|
ASPNETCORE_ENVIRONMENT: "Production"
|
||||||
|
ASPNETCORE_URLS: "http://0.0.0.0:8080"
|
||||||
|
DOTNET_RUNNING_IN_CONTAINER: "true"
|
||||||
|
MASTER_DB_CONNECTION_STRING: "${MASTER_DB_CONNECTION_STRING}"
|
||||||
|
DB_CONNECTION_STRING: "${DB_CONNECTION_STRING}"
|
||||||
|
restart: unless-stopped
|
||||||
|
networks:
|
||||||
|
- prodnet
|
||||||
|
volumes:
|
||||||
|
- nuget-cache-prod:/root/.nuget/packages
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
sqlserverdata-prod:
|
||||||
|
driver: local
|
||||||
|
nuget-cache-prod:
|
||||||
|
driver: local
|
||||||
|
|
||||||
|
networks:
|
||||||
|
prodnet:
|
||||||
|
driver: bridge
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
services:
|
|
||||||
sqlserver:
|
|
||||||
image: mcr.microsoft.com/mssql/server:2022-latest
|
|
||||||
platform: linux/amd64
|
|
||||||
container_name: sqlserver
|
|
||||||
env_file:
|
|
||||||
- .env
|
|
||||||
environment:
|
|
||||||
ACCEPT_EULA: "Y"
|
|
||||||
SA_PASSWORD: "${SA_PASSWORD}"
|
|
||||||
ports:
|
|
||||||
- "1433:1433"
|
|
||||||
volumes:
|
|
||||||
- sqlserverdata:/var/opt/mssql
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD", "/opt/mssql-tools/bin/sqlcmd", "-S", "localhost", "-U", "sa", "-P", "${SA_PASSWORD}", "-Q", "SELECT 1" ]
|
|
||||||
interval: 10s
|
|
||||||
timeout: 5s
|
|
||||||
retries: 12
|
|
||||||
networks:
|
|
||||||
- devnet
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
sqlserverdata:
|
|
||||||
nuget-cache:
|
|
||||||
|
|
||||||
networks:
|
|
||||||
devnet:
|
|
||||||
driver: bridge
|
|
||||||
25
src/Core/.dockerignore
Normal file
25
src/Core/.dockerignore
Normal 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
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<RootNamespace>WebAPI</RootNamespace>
|
<RootNamespace>WebAPI</RootNamespace>
|
||||||
|
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -19,4 +20,10 @@
|
|||||||
<ProjectReference Include="..\..\Repository\Repository.Core\Repository.Core.csproj" />
|
<ProjectReference Include="..\..\Repository\Repository.Core\Repository.Core.csproj" />
|
||||||
<ProjectReference Include="..\..\Service\Service.Core\Service.Core.csproj" />
|
<ProjectReference Include="..\..\Service\Service.Core\Service.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="..\..\.dockerignore">
|
||||||
|
<Link>.dockerignore</Link>
|
||||||
|
</Content>
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
26
src/Core/API/API.Core/Dockerfile
Normal file
26
src/Core/API/API.Core/Dockerfile
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base
|
||||||
|
ARG APP_UID=1000
|
||||||
|
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 ["API/API.Core/API.Core.csproj", "API/API.Core/"]
|
||||||
|
COPY ["Repository/Repository.Core/Repository.Core.csproj", "Repository/Repository.Core/"]
|
||||||
|
COPY ["Service/Service.Core/Service.Core.csproj", "Service/Service.Core/"]
|
||||||
|
RUN dotnet restore "API/API.Core/API.Core.csproj"
|
||||||
|
COPY . .
|
||||||
|
WORKDIR "/src/API/API.Core"
|
||||||
|
RUN dotnet build "./API.Core.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||||
|
|
||||||
|
FROM build AS publish
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
RUN dotnet publish "./API.Core.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||||
|
|
||||||
|
FROM base AS final
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=publish /app/publish .
|
||||||
|
ENTRYPOINT ["dotnet", "API.Core.dll"]
|
||||||
@@ -10,6 +10,17 @@ builder.Services.AddEndpointsApiExplorer();
|
|||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen();
|
||||||
builder.Services.AddOpenApi();
|
builder.Services.AddOpenApi();
|
||||||
|
|
||||||
|
// Add health checks
|
||||||
|
builder.Services.AddHealthChecks();
|
||||||
|
|
||||||
|
// Configure logging for container output
|
||||||
|
builder.Logging.ClearProviders();
|
||||||
|
builder.Logging.AddConsole();
|
||||||
|
if (!builder.Environment.IsProduction())
|
||||||
|
{
|
||||||
|
builder.Logging.AddDebug();
|
||||||
|
}
|
||||||
|
|
||||||
// Dependency Injection
|
// Dependency Injection
|
||||||
builder.Services.AddSingleton<ISqlConnectionFactory, DefaultSqlConnectionFactory>();
|
builder.Services.AddSingleton<ISqlConnectionFactory, DefaultSqlConnectionFactory>();
|
||||||
builder.Services.AddScoped<IUserAccountRepository, UserAccountRepository>();
|
builder.Services.AddScoped<IUserAccountRepository, UserAccountRepository>();
|
||||||
@@ -17,6 +28,7 @@ builder.Services.AddScoped<IUserService, UserService>();
|
|||||||
builder.Services.AddScoped<IUserCredentialRepository, UserCredentialRepository>();
|
builder.Services.AddScoped<IUserCredentialRepository, UserCredentialRepository>();
|
||||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||||
builder.Services.AddScoped<IJwtService, JwtService>();
|
builder.Services.AddScoped<IJwtService, JwtService>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
@@ -24,6 +36,18 @@ app.UseSwaggerUI();
|
|||||||
app.MapOpenApi();
|
app.MapOpenApi();
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
// Health check endpoint (used by Docker health checks and orchestrators)
|
||||||
|
app.MapHealthChecks("/health");
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
app.MapFallbackToController("Handle404", "NotFound");
|
app.MapFallbackToController("Handle404", "NotFound");
|
||||||
|
|
||||||
|
// Graceful shutdown handling
|
||||||
|
var lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
|
||||||
|
lifetime.ApplicationStopping.Register(() =>
|
||||||
|
{
|
||||||
|
app.Logger.LogInformation("Application is shutting down gracefully...");
|
||||||
|
});
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
@@ -2,7 +2,18 @@
|
|||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Information",
|
||||||
|
"Microsoft.EntityFrameworkCore": "Information"
|
||||||
|
},
|
||||||
|
"Console": {
|
||||||
|
"IncludeScopes": true,
|
||||||
|
"TimestampFormat": "yyyy-MM-ddTHH:mm:ss.fffZ"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*",
|
||||||
|
"Jwt": {
|
||||||
|
"ExpirationMinutes": 120,
|
||||||
|
"Issuer": "biergarten-api",
|
||||||
|
"Audience": "biergarten-users"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/Core/API/API.Core/appsettings.Production.json
Normal file
19
src/Core/API/API.Core/appsettings.Production.json
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Warning",
|
||||||
|
"Microsoft.AspNetCore": "Warning",
|
||||||
|
"Microsoft.EntityFrameworkCore": "Error"
|
||||||
|
},
|
||||||
|
"Console": {
|
||||||
|
"IncludeScopes": false,
|
||||||
|
"TimestampFormat": "yyyy-MM-ddTHH:mm:ss.fffZ"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*",
|
||||||
|
"Jwt": {
|
||||||
|
"ExpirationMinutes": 60,
|
||||||
|
"Issuer": "biergarten-api",
|
||||||
|
"Audience": "biergarten-users"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,23 @@
|
|||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning",
|
||||||
|
"Microsoft.EntityFrameworkCore": "Information"
|
||||||
|
},
|
||||||
|
"Console": {
|
||||||
|
"IncludeScopes": true,
|
||||||
|
"TimestampFormat": "yyyy-MM-ddTHH:mm:ss.fffZ"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*",
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"DefaultConnection": ""
|
||||||
|
},
|
||||||
|
"Jwt": {
|
||||||
|
"SecretKey": "",
|
||||||
|
"ExpirationMinutes": 60,
|
||||||
|
"Issuer": "biergarten-api",
|
||||||
|
"Audience": "biergarten-users"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
<PackageReference Include="xunit" Version="2.9.2" />
|
<PackageReference Include="xunit" Version="2.9.2" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||||
<PackageReference Include="FluentAssertions" Version="6.9.0" />
|
<PackageReference Include="FluentAssertions" Version="6.9.0" />
|
||||||
|
<PackageReference Include="dbup" Version="5.0.41" />
|
||||||
|
|
||||||
<!-- Reqnroll core, xUnit adapter and code-behind generator -->
|
<!-- Reqnroll core, xUnit adapter and code-behind generator -->
|
||||||
<PackageReference Include="Reqnroll" Version="3.3.3" />
|
<PackageReference Include="Reqnroll" Version="3.3.3" />
|
||||||
@@ -33,5 +34,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\API.Core\API.Core.csproj" />
|
<ProjectReference Include="..\API.Core\API.Core.csproj" />
|
||||||
|
<ProjectReference Include="..\..\Database\Database.Core\Database.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
<Solution>
|
<Solution>
|
||||||
<Folder Name="/API/" >
|
<Folder Name="/API/">
|
||||||
<Project Path="API/API.Core/API.Core.csproj"/>
|
<Project Path="API/API.Core/API.Core.csproj" />
|
||||||
<Project Path="API/API.Specs/API.Specs.csproj"/>
|
<Project Path="API/API.Specs/API.Specs.csproj" />
|
||||||
</Folder>
|
</Folder>
|
||||||
<Folder Name="/Database/">
|
<Folder Name="/Database/">
|
||||||
<Project Path="Database/Database.Core/Database.Core.csproj" />
|
<Project Path="Database/Database.Migrations/Database.Migrations.csproj" />
|
||||||
<Project Path="Database/Database.Seed/Database.Seed.csproj" />
|
<Project Path="Database/Database.Seed/Database.Seed.csproj" />
|
||||||
</Folder>
|
</Folder>
|
||||||
<Folder Name="/Repository/" >
|
<Folder Name="/Repository/">
|
||||||
<Project Path="Repository/Repository.Core/Repository.Core.csproj"/>
|
<Project Path="Repository/Repository.Core/Repository.Core.csproj" />
|
||||||
<Project Path="Repository/Repository.Tests/Repository.Tests.csproj"/>
|
<Project Path="Repository/Repository.Tests/Repository.Tests.csproj" />
|
||||||
</Folder>
|
</Folder>
|
||||||
<Folder Name="/Service/">
|
<Folder Name="/Service/">
|
||||||
<Project Path="Service/Service.Core/Service.Core.csproj"/>
|
<Project Path="Service/Service.Core/Service.Core.csproj" />
|
||||||
</Folder>
|
</Folder>
|
||||||
</Solution>
|
</Solution>
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
// Get connection string from environment variable
|
|
||||||
|
|
||||||
using System.Reflection;
|
|
||||||
using DbUp;
|
|
||||||
|
|
||||||
var connectionString = Environment.GetEnvironmentVariable(
|
|
||||||
"DB_CONNECTION_STRING"
|
|
||||||
);
|
|
||||||
|
|
||||||
var upgrader = DeployChanges
|
|
||||||
.To.SqlDatabase(connectionString)
|
|
||||||
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
|
|
||||||
.LogToConsole()
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
var result = upgrader.PerformUpgrade();
|
|
||||||
|
|
||||||
if (!result.Successful)
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
|
||||||
Console.WriteLine(result.Error);
|
|
||||||
Console.ResetColor();
|
|
||||||
#if DEBUG
|
|
||||||
Console.ReadLine();
|
|
||||||
#endif
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
|
||||||
Console.WriteLine("Success!");
|
|
||||||
Console.ResetColor();
|
|
||||||
return 0;
|
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<RootNamespace>DataLayer</RootNamespace>
|
<RootNamespace>DataLayer</RootNamespace>
|
||||||
|
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -14,4 +15,9 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<EmbeddedResource Include="scripts/**/*.sql" />
|
<EmbeddedResource Include="scripts/**/*.sql" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="..\..\.dockerignore">
|
||||||
|
<Link>.dockerignore</Link>
|
||||||
|
</Content>
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
17
src/Core/Database/Database.Migrations/Dockerfile
Normal file
17
src/Core/Database/Database.Migrations/Dockerfile
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
WORKDIR /src
|
||||||
|
# Copy everything from the context (src/Core/Database)
|
||||||
|
COPY . .
|
||||||
|
RUN dotnet restore "./Database.Migrations/Database.Migrations.csproj"
|
||||||
|
RUN dotnet build "./Database.Migrations/Database.Migrations.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||||
|
|
||||||
|
FROM build AS publish
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
RUN dotnet publish "./Database.Migrations/Database.Migrations.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||||
|
|
||||||
|
FROM mcr.microsoft.com/dotnet/runtime:10.0 AS final
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=publish /app/publish .
|
||||||
|
ENTRYPOINT ["dotnet", "Database.Migrations.dll"]
|
||||||
82
src/Core/Database/Database.Migrations/Program.cs
Normal file
82
src/Core/Database/Database.Migrations/Program.cs
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
using System.Data;
|
||||||
|
using System.Reflection;
|
||||||
|
using DbUp;
|
||||||
|
using Microsoft.Data.SqlClient;
|
||||||
|
|
||||||
|
namespace DataLayer;
|
||||||
|
|
||||||
|
public static class Program
|
||||||
|
{
|
||||||
|
private static readonly string? connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING");
|
||||||
|
private static readonly string? masterConnectionString = Environment.GetEnvironmentVariable("MASTER_DB_CONNECTION_STRING");
|
||||||
|
|
||||||
|
private static bool DeployMigrations()
|
||||||
|
{
|
||||||
|
var upgrader = DeployChanges
|
||||||
|
.To.SqlDatabase(connectionString)
|
||||||
|
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
|
||||||
|
.LogToConsole()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var result = upgrader.PerformUpgrade();
|
||||||
|
return result.Successful;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool CreateDatabaseIfNotExists()
|
||||||
|
{
|
||||||
|
var myConn = new SqlConnection(masterConnectionString);
|
||||||
|
|
||||||
|
const string str = """
|
||||||
|
IF NOT EXISTS (SELECT 1 FROM sys.databases WHERE name = 'Biergarten')
|
||||||
|
CREATE DATABASE [Biergarten]
|
||||||
|
""";
|
||||||
|
|
||||||
|
var myCommand = new SqlCommand(str, myConn);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
myConn.Open();
|
||||||
|
myCommand.ExecuteNonQuery();
|
||||||
|
Console.WriteLine("Database creation command executed successfully.");
|
||||||
|
}
|
||||||
|
catch (System.Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error creating database: {ex}");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (myConn.State == ConnectionState.Open)
|
||||||
|
{
|
||||||
|
myConn.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int Main(string[] args)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Starting database migrations...");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
CreateDatabaseIfNotExists();
|
||||||
|
var success = DeployMigrations();
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Database migrations completed successfully.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Database migrations failed.");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("An error occurred during database migrations:");
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,9 +16,9 @@
|
|||||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.3" />
|
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.3" />
|
||||||
<PackageReference Include="dbup" Version="5.0.41" />
|
<PackageReference Include="dbup" Version="5.0.41" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Database.Core\Database.Core.csproj" />
|
<ProjectReference Include="..\Database.Migrations\Database.Migrations.csproj" />
|
||||||
<ProjectReference Include="..\..\Repository\Repository.Core\Repository.Core.csproj" />
|
<ProjectReference Include="..\..\Repository\Repository.Core\Repository.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -22,14 +22,14 @@ try
|
|||||||
await useMaster.ExecuteNonQueryAsync();
|
await useMaster.ExecuteNonQueryAsync();
|
||||||
|
|
||||||
var dbName = "Biergarten";
|
var dbName = "Biergarten";
|
||||||
var dropDb = connection.CreateCommand();
|
var dropDb = connection.CreateCommand();
|
||||||
dropDb.CommandText = $@"
|
dropDb.CommandText = $@"
|
||||||
IF DB_ID(N'{dbName}') IS NOT NULL
|
IF DB_ID(N'{dbName}') IS NOT NULL
|
||||||
BEGIN
|
BEGIN
|
||||||
ALTER DATABASE [{dbName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
|
ALTER DATABASE [{dbName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
|
||||||
DROP DATABASE [{dbName}];
|
DROP DATABASE [{dbName}];
|
||||||
END";
|
END";
|
||||||
await dropDb.ExecuteNonQueryAsync();
|
await dropDb.ExecuteNonQueryAsync();
|
||||||
|
|
||||||
var createDb = connection.CreateCommand();
|
var createDb = connection.CreateCommand();
|
||||||
createDb.CommandText = $@"CREATE DATABASE [{dbName}];";
|
createDb.CommandText = $@"CREATE DATABASE [{dbName}];";
|
||||||
|
|||||||
Reference in New Issue
Block a user