mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 02:39:03 +00:00
Change dir name for migrations, update docker config to seed db
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,
|
||||||
|
|||||||
@@ -6,11 +6,11 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
ACCEPT_EULA: "Y"
|
ACCEPT_EULA: "Y"
|
||||||
SA_PASSWORD: "${SA_PASSWORD}"
|
SA_PASSWORD: "${SA_PASSWORD}"
|
||||||
MSSQL_PID: "Developer"
|
MSSQL_PID: "Express"
|
||||||
ports:
|
ports:
|
||||||
- "1433:1433"
|
- "1433:1433"
|
||||||
volumes:
|
volumes:
|
||||||
- sqlserverdata:/var/opt/mssql
|
- sqlserverdata-dev:/var/opt/mssql
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P '${SA_PASSWORD}' -C -Q 'SELECT 1' || exit 1"]
|
test: ["CMD-SHELL", "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P '${SA_PASSWORD}' -C -Q 'SELECT 1' || exit 1"]
|
||||||
interval: 10s
|
interval: 10s
|
||||||
@@ -19,7 +19,25 @@ services:
|
|||||||
start_period: 30s
|
start_period: 30s
|
||||||
networks:
|
networks:
|
||||||
- devnet
|
- 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:
|
api.core:
|
||||||
image: api.core
|
image: api.core
|
||||||
container_name: dev-env-api-core
|
container_name: dev-env-api-core
|
||||||
@@ -44,12 +62,12 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- devnet
|
- devnet
|
||||||
volumes:
|
volumes:
|
||||||
- nuget-cache:/root/.nuget/packages
|
- nuget-cache-dev:/root/.nuget/packages
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
sqlserverdata:
|
sqlserverdata-dev:
|
||||||
driver: local
|
driver: local
|
||||||
nuget-cache:
|
nuget-cache-dev:
|
||||||
driver: local
|
driver: local
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
ACCEPT_EULA: "Y"
|
ACCEPT_EULA: "Y"
|
||||||
SA_PASSWORD: "${SA_PASSWORD}"
|
SA_PASSWORD: "${SA_PASSWORD}"
|
||||||
MSSQL_PID: "Developer"
|
MSSQL_PID: "Express"
|
||||||
volumes:
|
volumes:
|
||||||
- sqlserverdata:/var/opt/mssql
|
- sqlserverdata-prod:/var/opt/mssql
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P '${SA_PASSWORD}' -C -Q 'SELECT 1' || exit 1"]
|
test: ["CMD-SHELL", "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P '${SA_PASSWORD}' -C -Q 'SELECT 1' || exit 1"]
|
||||||
interval: 10s
|
interval: 10s
|
||||||
@@ -18,6 +18,26 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- prodnet
|
- 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:
|
api.core:
|
||||||
image: api.core
|
image: api.core
|
||||||
container_name: prod-env-api-core
|
container_name: prod-env-api-core
|
||||||
@@ -37,17 +57,18 @@ services:
|
|||||||
ASPNETCORE_ENVIRONMENT: "Production"
|
ASPNETCORE_ENVIRONMENT: "Production"
|
||||||
ASPNETCORE_URLS: "http://0.0.0.0:8080"
|
ASPNETCORE_URLS: "http://0.0.0.0:8080"
|
||||||
DOTNET_RUNNING_IN_CONTAINER: "true"
|
DOTNET_RUNNING_IN_CONTAINER: "true"
|
||||||
|
MASTER_DB_CONNECTION_STRING: "${MASTER_DB_CONNECTION_STRING}"
|
||||||
DB_CONNECTION_STRING: "${DB_CONNECTION_STRING}"
|
DB_CONNECTION_STRING: "${DB_CONNECTION_STRING}"
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
networks:
|
networks:
|
||||||
- prodnet
|
- prodnet
|
||||||
volumes:
|
volumes:
|
||||||
- nuget-cache:/root/.nuget/packages
|
- nuget-cache-prod:/root/.nuget/packages
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
sqlserverdata:
|
sqlserverdata-prod:
|
||||||
driver: local
|
driver: local
|
||||||
nuget-cache:
|
nuget-cache-prod:
|
||||||
driver: local
|
driver: local
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<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/">
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,7 +18,7 @@
|
|||||||
</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