Use env variable for migration bundle instead of dockerfile entrypoint

This commit is contained in:
Zoe Roux 2024-04-15 21:24:19 +02:00
parent 7140d308fd
commit e39337f8f1
No known key found for this signature in database
3 changed files with 9 additions and 5 deletions

View File

@ -24,4 +24,4 @@ RUN dotnet ef migrations bundle --no-build --self-contained -r linux-${TARGETARC
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0
COPY --from=builder /app/migrate /app/migrate
ENTRYPOINT /app/migrate --connection "USER ID=${POSTGRES_USER};PASSWORD=${POSTGRES_PASSWORD};SERVER=${POSTGRES_SERVER};PORT=${POSTGRES_PORT};DATABASE=${POSTGRES_DB};"
ENTRYPOINT ["/app/migrate"]

View File

@ -40,6 +40,7 @@ public class PostgresContext(DbContextOptions options, IHttpContextAccessor acce
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseProjectables();
optionsBuilder.UseSnakeCaseNamingConvention();
base.OnConfiguring(optionsBuilder);
}
@ -128,7 +129,11 @@ public class PostgresContextBuilder : IDesignTimeDbContextFactory<PostgresContex
{
public PostgresContext CreateDbContext(string[] args)
{
NpgsqlDataSource dataSource = PostgresModule.CreateDataSource(new ConfigurationManager());
IConfigurationRoot config = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
NpgsqlDataSource dataSource = PostgresModule.CreateDataSource(config);
DbContextOptionsBuilder builder = new();
builder.UseNpgsql(dataSource);

View File

@ -37,7 +37,7 @@ public static class PostgresModule
{
["USER ID"] = configuration.GetValue("POSTGRES_USER", "KyooUser"),
["PASSWORD"] = configuration.GetValue("POSTGRES_PASSWORD", "KyooPassword"),
["SERVER"] = configuration.GetValue("POSTGRES_SERVER", "db"),
["SERVER"] = configuration.GetValue("POSTGRES_SERVER", "postgres"),
["PORT"] = configuration.GetValue("POSTGRES_PORT", "5432"),
["DATABASE"] = configuration.GetValue("POSTGRES_DB", "kyooDB"),
["POOLING"] = "true",
@ -55,11 +55,10 @@ public static class PostgresModule
public static void ConfigurePostgres(this WebApplicationBuilder builder)
{
NpgsqlDataSource dataSource = CreateDataSource(builder.Configuration);
builder.Services.AddDbContext<DatabaseContext, PostgresContext>(
x =>
{
x.UseNpgsql(dataSource).UseProjectables();
x.UseNpgsql(dataSource);
if (builder.Environment.IsDevelopment())
x.EnableDetailedErrors().EnableSensitiveDataLogging();
},