Skip to content

Commit

Permalink
Added check to ensure the database is created.
Browse files Browse the repository at this point in the history
  • Loading branch information
stijuh committed Jun 10, 2023
1 parent 4d80a35 commit 53e898f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion GainsTracker.CoreAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public static void Main(string[] args)

WebApplication app = builder.Build();

app.ResetAndUpdateDatabase(args.Length > 0 && args[0].ToBool());
var resetDatabase = args.Length > 0 && args[0].ToBool();

app.ResetAndUpdateDatabase(resetDatabase);

if (!resetDatabase)
app.EnsureDatabaseIsCreated();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment() || app.Environment.EnvironmentName == "Docker")
Expand Down
9 changes: 9 additions & 0 deletions GainsTracker.CoreAPI/ProgramExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ public static void ResetAndUpdateDatabase(this WebApplication app, bool execute
db.Database.EnsureCreated();
db.Database.Migrate();
}

public static void EnsureDatabaseIsCreated(this WebApplication app)
{
using IServiceScope scope = app.Services.CreateScope();
AppDbContext db = scope.ServiceProvider.GetRequiredService<AppDbContext>();

Console.WriteLine("checking if database is filled..");
db.Database.EnsureCreated();
}

/// <summary>
/// Configure global exception handling.
Expand Down

0 comments on commit 53e898f

Please sign in to comment.