some stuff

This commit is contained in:
john 2025-05-18 17:18:34 +02:00
parent a98a61e040
commit d061cac489
3 changed files with 78 additions and 17 deletions

View file

@ -20,24 +20,54 @@ USER appuser
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release ARG BUILD_CONFIGURATION=Release
WORKDIR /src WORKDIR /src
# Copy csproj files for all projects
COPY ["Femto.Api/Femto.Api.csproj", "Femto.Api/"] COPY ["Femto.Api/Femto.Api.csproj", "Femto.Api/"]
COPY ["Femto.Modules.Blog/Femto.Modules.Blog.csproj", "Femto.Modules.Blog/"] COPY ["Femto.Modules.Blog/Femto.Modules.Blog.csproj", "Femto.Modules.Blog/"]
COPY ["Femto.Common/Femto.Common.csproj", "Femto.Common/"] COPY ["Femto.Common/Femto.Common.csproj", "Femto.Common/"]
COPY ["Femto.Modules.Auth.Contracts/Femto.Modules.Auth.Contracts.csproj", "Femto.Modules.Auth.Contracts/"] COPY ["Femto.Modules.Auth.Contracts/Femto.Modules.Auth.Contracts.csproj", "Femto.Modules.Auth.Contracts/"]
COPY ["Femto.Modules.Auth/Femto.Modules.Auth.csproj", "Femto.Modules.Auth/"] COPY ["Femto.Modules.Auth/Femto.Modules.Auth.csproj", "Femto.Modules.Auth/"]
COPY ["Femto.Modules.Media/Femto.Modules.Media.csproj", "Femto.Modules.Media/"] COPY ["Femto.Modules.Media/Femto.Modules.Media.csproj", "Femto.Modules.Media/"]
COPY ["Femto.Database/Femto.Database.csproj", "Femto.Database/"]
# Restore all dependencies
RUN dotnet restore "Femto.Api/Femto.Api.csproj" RUN dotnet restore "Femto.Api/Femto.Api.csproj"
# Copy everything
COPY . . COPY . .
# Build the API
WORKDIR "/src/Femto.Api" WORKDIR "/src/Femto.Api"
RUN dotnet build "Femto.Api.csproj" -c $BUILD_CONFIGURATION -o /app/build RUN dotnet build "Femto.Api.csproj" -c $BUILD_CONFIGURATION -o /app/build
# Build and publish both API and Database CLI
FROM build AS publish FROM build AS publish
ARG BUILD_CONFIGURATION=Release ARG BUILD_CONFIGURATION=Release
# Publish API
RUN dotnet publish "Femto.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false RUN dotnet publish "Femto.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# Publish Database CLI
WORKDIR "/src/Femto.Database"
RUN dotnet publish "Femto.Database.csproj" -c $BUILD_CONFIGURATION -o /app/femto-db /p:UseAppHost=false
# Final runtime image
FROM base AS final FROM base AS final
WORKDIR /app WORKDIR /app
COPY --from=publish /app/publish .
# Entrypoint # Copy published API and DB CLI
COPY --from=publish /app/publish .
COPY --from=publish /app/femto-db /app/femto-db
# Add a wrapper script to launch the DB CLI
RUN mkdir -p /app/scripts && \
echo '#!/bin/sh\nexec dotnet /app/femto-db/Femto.Database.dll "$@"' > /app/scripts/femto-db && \
chmod +x /app/scripts/femto-db
# (Optional) Add script dir to PATH for easier access
ENV PATH="/app/scripts:${PATH}"
# Entrypoint for the API
ENTRYPOINT ["dotnet", "Femto.Api.dll"] ENTRYPOINT ["dotnet", "Femto.Api.dll"]

View file

@ -14,7 +14,10 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Migrations\" /> <Folder Include="Migrations\" />
<None Update="Migrations\**\*.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -6,7 +6,7 @@ using Npgsql;
var nameArg = new Argument<string>("name", "the name of the migration"); var nameArg = new Argument<string>("name", "the name of the migration");
var migrationsDirectoryOption = new Option<string>( var migrationsDirectoryOption = new Option<string>(
["--migrations-directory"], ["--migrations-directory"],
() => "./Migrations", () => System.Environment.GetEnvironmentVariable("MigrationsDirectory") ?? "./Migrations",
"the directory where the migrations are stored" "the directory where the migrations are stored"
); );
@ -15,6 +15,7 @@ var newCommand = new Command("new", "creates a new migrations")
nameArg, nameArg,
migrationsDirectoryOption, migrationsDirectoryOption,
}; };
newCommand.SetHandler(MakeNewMigration, nameArg, migrationsDirectoryOption); newCommand.SetHandler(MakeNewMigration, nameArg, migrationsDirectoryOption);
var connectionStringArg = new Argument<string>( var connectionStringArg = new Argument<string>(
@ -22,30 +23,53 @@ var connectionStringArg = new Argument<string>(
"the connection string to the database" "the connection string to the database"
); );
var connectionStringOption = new Option<string?>(
["-c", "--connection-string"],
() => System.Environment.GetEnvironmentVariable("ConnectionStrings__Database") ?? null,
"the connection string to the database"
);
var upCommand = new Command("up", "update the database to the most current migration") var upCommand = new Command("up", "update the database to the most current migration")
{ {
migrationsDirectoryOption, migrationsDirectoryOption,
connectionStringArg, connectionStringArg,
}; };
upCommand.SetHandler(MigrateUp, migrationsDirectoryOption, connectionStringArg);
upCommand.SetHandler(
async (directory, connectionString) =>
{
if (connectionString is null)
{
throw new ArgumentException("Connection string is required.");
}
await MigrateUp(directory, connectionString);
},
migrationsDirectoryOption,
connectionStringOption
);
var seedCommand = new Command("seed", "seed the database with test data") { connectionStringArg }; var seedCommand = new Command("seed", "seed the database with test data") { connectionStringArg };
seedCommand.SetHandler(Seed, connectionStringArg); seedCommand.SetHandler(Seed, connectionStringArg);
// Add these near the top with other command definitions // Add these near the top with other command definitions
var yesOption = new Option<bool>( var yesOption = new Option<bool>(["-y", "--yes"], "Skip confirmation prompt");
["-y", "--yes"],
"Skip confirmation prompt"
);
var resetCommand = new Command("reset", "drops the existing database, runs migrations, and seeds test data") var resetCommand = new Command(
"reset",
"drops the existing database, runs migrations, and seeds test data"
)
{ {
connectionStringArg, connectionStringArg,
migrationsDirectoryOption, migrationsDirectoryOption,
yesOption yesOption,
}; };
resetCommand.SetHandler(CreateTestDatabase, connectionStringArg, migrationsDirectoryOption, yesOption); resetCommand.SetHandler(
CreateTestDatabase,
connectionStringArg,
migrationsDirectoryOption,
yesOption
);
var rootCommand = new RootCommand("migrator") { newCommand, upCommand, seedCommand, resetCommand }; var rootCommand = new RootCommand("migrator") { newCommand, upCommand, seedCommand, resetCommand };
@ -85,7 +109,11 @@ static async Task Seed(string connectionString)
await TestDataSeeder.Seed(dataSource); await TestDataSeeder.Seed(dataSource);
} }
static async Task CreateTestDatabase(string connectionString, string migrationsDirectory, bool skipConfirmation) static async Task CreateTestDatabase(
string connectionString,
string migrationsDirectory,
bool skipConfirmation
)
{ {
var builder = new NpgsqlConnectionStringBuilder(connectionString); var builder = new NpgsqlConnectionStringBuilder(connectionString);
if (!skipConfirmation) if (!skipConfirmation)