Skip to content

Commit

Permalink
Merge pull request #35 from intive/feature/PATRO23-102-Run-migrations…
Browse files Browse the repository at this point in the history
…-on-startup

feature/PATRO23-102-Add-migrations-run-on-startup
  • Loading branch information
pzajaczkowski authored Apr 3, 2023
2 parents 86ca5d9 + 1e2a488 commit b149f22
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/api/app/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

builder.Services.AddEndpointsApiExplorer();

builder.Services.AddExampleModule(builder.Configuration);
builder.Services.AddHttpLogging(logging =>
{
logging.LoggingFields = HttpLoggingFields.All;
Expand All @@ -27,6 +26,7 @@
});

builder.Services.AddSharedModule();
builder.Services.AddExampleModule(builder.Configuration);

builder.Services.AddMediatR(cfg =>
{
Expand Down
2 changes: 2 additions & 0 deletions src/modules/example/api/ExampleModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Intive.Patronage2023.Modules.Example.Domain;
using Intive.Patronage2023.Modules.Example.Infrastructure.Data;
using Intive.Patronage2023.Modules.Example.Infrastructure.Domain;
using Intive.Patronage2023.Shared.Abstractions;

using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -37,6 +38,7 @@ public static IServiceCollection AddExampleModule(this IServiceCollection servic
/// <returns>Updated IApplicationBuilder.</returns>
public static IApplicationBuilder UseExampleModule(this IApplicationBuilder app)
{
app.InitDatabase<ExampleDbContext>();
return app;
}
}
32 changes: 32 additions & 0 deletions src/shared/abstractions/ApplicationBuilderExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Builder;

using Microsoft.EntityFrameworkCore;

using Microsoft.Extensions.DependencyInjection;

namespace Intive.Patronage2023.Shared.Abstractions;

/// <summary>
/// Class contains methods that extend ApplicationBuilder.
/// </summary>
public static class ApplicationBuilderExtension
{
/// <summary>
/// Enables migrations on startup for provided dbcontext class.
/// </summary>
/// <typeparam name="T">DbContext class to make migrations for.</typeparam>
/// <param name="app">IApplicationBuilder.</param>
/// <returns>Updated IApplicationBuilder.</returns>
public static IApplicationBuilder InitDatabase<T>(this IApplicationBuilder app)
where T : DbContext
{
using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>()!.CreateScope())
{
var dbContext = scope.ServiceProvider
.GetRequiredService<T>();
dbContext.Database.Migrate();
}

return app;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

<ItemGroup>
<PackageReference Include="MediatR" Version="12.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.3" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit b149f22

Please sign in to comment.