Skip to content

Commit

Permalink
Merge pull request #43 from AngeloDotNet/develop
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
AngeloDotNet authored Nov 29, 2024
2 parents d83136e + eceb0a5 commit 06a89f7
Showing 1 changed file with 48 additions and 35 deletions.
83 changes: 48 additions & 35 deletions src/GSWCloudApp.Common/Helpers/ApplicationHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -7,46 +8,58 @@ namespace GSWCloudApp.Common.Helpers;

public static class ApplicationHelpers
{
public static async Task ConfigureDatabaseAsync<TDbContext>(IServiceProvider serviceProvider) where TDbContext : DbContext
public static async void ApplyMigrations<TDbContext>(this WebApplication app) where TDbContext : DbContext
{
using var scope = serviceProvider.CreateScope();
using var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<TDbContext>();

if (!dbContext.Database.IsInMemory())
{
await EnsureDatabaseAsync();
await RunMigrationsAsync();
}
else
{
dbContext.Database.EnsureCreated();
}
var dbCreator = dbContext.GetService<IRelationalDatabaseCreator>();

async Task EnsureDatabaseAsync()
if (!await dbCreator.ExistsAsync())
{
var dbCreator = dbContext.GetService<IRelationalDatabaseCreator>();
var strategy = dbContext.Database.CreateExecutionStrategy();

await strategy.ExecuteAsync(async () =>
{
if (!await dbCreator.ExistsAsync())
{
await dbCreator.CreateAsync();
}
});
await dbCreator.CreateAsync();
}

async Task RunMigrationsAsync()
{
var strategy = dbContext.Database.CreateExecutionStrategy();
dbContext.Database.Migrate();
}

await strategy.ExecuteAsync(async () =>
{
await using var transaction = await dbContext.Database.BeginTransactionAsync();
//TODO: da eliminare al prossimo refactoring
//public static async Task ConfigureDatabaseAsync<TDbContext>(IServiceProvider serviceProvider) where TDbContext : DbContext
//{
// using var scope = serviceProvider.CreateScope();
// var dbContext = scope.ServiceProvider.GetRequiredService<TDbContext>();

await dbContext.Database.MigrateAsync();
await transaction.CommitAsync();
});
}
}
}
// if (!dbContext.Database.IsInMemory())
// {
// await EnsureDatabaseAsync();
// await RunMigrationsAsync();
// }
// else
// {
// dbContext.Database.EnsureCreated();
// }

// async Task EnsureDatabaseAsync()
// {
// var dbCreator = dbContext.GetService<IRelationalDatabaseCreator>();

// if (!await dbCreator.ExistsAsync())
// {
// await dbCreator.CreateAsync();
// }
// }

// async Task RunMigrationsAsync(this WebApplication app)
// {
// //await using var transaction = await dbContext.Database.BeginTransactionAsync();

// //await dbContext.Database.MigrateAsync();
// //await transaction.CommitAsync();
// using IServiceScope scope = app.Services.CreateScope();

// var dbContext = scope.ServiceProvider.GetRequiredService<Appdb>();
// dbContext.Database.Migrate();

// }
//}
}

0 comments on commit 06a89f7

Please sign in to comment.