Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ services:
environment:
ACCEPT_EULA: "Y"
MSSQL_SA_PASSWORD: "${DATABASE_PASSWORD}"
MSSQL_PID: "Developer"
ports:
- "1433:1433"
user: "root"
Expand All @@ -71,17 +70,20 @@ services:
networks:
- backend

db-setup:
container_name: "db-setup"
db-migrations:
container_name: db-migrations
build:
context: ./database
dockerfile: Dockerfile
restart: "no"
environment:
DATABASE_PASSWORD: "${DATABASE_PASSWORD}"
context: .
dockerfile: src/ServiceLayer.Mesh/Dockerfile.migrations
args:
DatabaseConnectionString: "${DatabaseConnectionString}"
depends_on:
db:
condition: service_healthy
environment:
DATABASE_NAME: "${DATABASE_NAME}"
DATABASE_USER: "${DATABASE_USER}"
DATABASE_PASSWORD: "${DATABASE_PASSWORD}"
networks:
- backend

Expand Down
16 changes: 0 additions & 16 deletions database/Dockerfile

This file was deleted.

23 changes: 0 additions & 23 deletions database/create_database_statement.sql

This file was deleted.

21 changes: 0 additions & 21 deletions database/db-setup-entrypoint.sh

This file was deleted.

12 changes: 12 additions & 0 deletions scripts/database/migrate-and-seed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -e

# Drop and recreate database for fresh start
echo "Checking if database exists..."
/opt/mssql-tools/bin/sqlcmd -S db -U "$DATABASE_USER" -P "$DATABASE_PASSWORD" -Q "IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'$DATABASE_NAME') CREATE DATABASE $DATABASE_NAME"

# Run migrations
echo "Running migrations..."
/opt/mssql-tools/bin/sqlcmd -S db -d "$DATABASE_NAME" -U "$DATABASE_USER" -P "$DATABASE_PASSWORD" -i /database/migration.sql

echo "Migration completed successfully!"
41 changes: 41 additions & 0 deletions src/ServiceLayer.Mesh/Dockerfile.migrations
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Stage 1: Build & Generate EF Migration Script
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build-env

WORKDIR /src

RUN apt-get update && apt-get install -y git

COPY . .

RUN git submodule update --init --recursive

ARG DatabaseConnectionString
ENV DatabaseConnectionString=${DatabaseConnectionString}

RUN dotnet restore ./src/ServiceLayer.Mesh/ServiceLayer.Mesh.csproj && \
dotnet build ./src/ServiceLayer.Mesh/ServiceLayer.Mesh.csproj && \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To take advantage of layer caching, it's better to copy the .csproj file first and run dotnet restore. After that, you can copy the source code and build the application.

dotnet tool install dotnet-ef --tool-path /tools

ENV PATH="/tools:$PATH" \
DOTNET_CLI_HOME=/tmp \
DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true \
DOTNET_NOLOGO=true

RUN mkdir -p /database && \
dotnet ef migrations script \
-o /database/migration.sql \
--project ./src/ServiceLayer.Mesh/

# Stage 2: Runtime - Apply Migration and Seed Data
FROM mcr.microsoft.com/mssql-tools:v1 AS migration-env

COPY --from=build-env /database/migration.sql /database/migration.sql
COPY scripts/database/migrate-and-seed.sh /scripts/database/migrate-and-seed.sh

RUN addgroup --system appgroup && \
adduser --system --ingroup appgroup appuser && \
chmod +x /scripts/database/migrate-and-seed.sh

USER appuser

ENTRYPOINT ["/scripts/database/migrate-and-seed.sh"]
Loading