Skip to content

Commit

Permalink
Added: only do MigrateTenantDatabase if pending
Browse files Browse the repository at this point in the history
  • Loading branch information
dillancagnetta committed Jan 17, 2025
1 parent f838c3e commit 91547a8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,23 @@ private async Task MigrateTenantDatabase(ITenant tenant, ITenantProvider provide
{
try
{
Console.WriteLine($"*** Beginning migration: [{tenant.Name}]");

using var context = DbContextFactory.Create(tenant.ConnectionString, provider);
await context.Database.MigrateAsync(ct);
await using var dbContext = DbContextFactory.Create(tenant.ConnectionString, provider);
if ((await dbContext.Database.GetPendingMigrationsAsync(ct)).Any())
{
Console.WriteLine($"*** Beginning migration for: [{tenant.Name}]");

await dbContext.Database.MigrateAsync(ct);

// Fixes issues with PostgreSQL not reloading types after migration e.g. hstore extension
// https://github.com/npgsql/efcore.pg/issues/292#issuecomment-388608426
await context.Database.OpenConnectionAsync(ct);
await ((NpgsqlConnection)context.Database.GetDbConnection()).ReloadTypesAsync(ct);
// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// Fixes issues with PostgreSQL not reloading types after migration e.g. hstore extension
// https://github.com/npgsql/efcore.pg/issues/292#issuecomment-388608426
await dbContext.Database.OpenConnectionAsync(ct);
await ((NpgsqlConnection)dbContext.Database.GetDbConnection()).ReloadTypesAsync(ct);
await dbContext.Database.CloseConnectionAsync();
// ------------------------------------------------------------------------------

Console.WriteLine($"*** Completed migration: [{tenant.Name}]");
Console.WriteLine($"*** Completed migration for: [{tenant.Name}]");
}
}
catch(Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public partial class InitialDb : Migration
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("Npgsql:PostgresExtension:hstore", ",,");
/*migrationBuilder.AlterDatabase()
.Annotation("Npgsql:PostgresExtension:hstore", ",,");*/

migrationBuilder.CreateTable(
name: "ChurchAttendanceType",
Expand Down

0 comments on commit 91547a8

Please sign in to comment.