Skip to content

Commit

Permalink
DropDatabaseOnRemove added
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaddix committed Feb 2, 2023
1 parent 67c460b commit 5cb5185
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/MccSoft.IntegreSql.EF/MccSoft.IntegreSql.EF.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>0.8.15</Version>
<Version>0.8.16</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>MCC Soft</Authors>
<Description>OpenIddict extension to support Auth code flow fo built-in ASP.Net identity providers</Description>
Expand Down
34 changes: 30 additions & 4 deletions src/MccSoft.IntegreSql.EF/NpgsqlDatabaseInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ public class NpgsqlDatabaseInitializer : BaseDatabaseInitializer
/// </summary>
public static bool UseMd5Hash = true;

/// <summary>
/// Drop database at the end of each test (when true) or not.
///
/// Old databases will be reused by IntegreSQL automatically.
/// Previously we were removing databases by default, which actually interfere with IntegreSQL.
/// It was 'hanging' when tried to reuse the removed databases.
/// However, it was reported that if not dropped, Postgres starts to consume a lot of RAM.
/// So one might be willing to drop anyway
/// (though, for the latter case I'd recommend reducing the `INTEGRESQL_TEST_MAX_POOL_SIZE` in docker).
/// </summary>
public bool DropDatabaseOnRemove { get; set; }

private record ConnectionStringInfo(string Hash, int Id);

/// <summary>
Expand Down Expand Up @@ -157,10 +169,24 @@ await _integreSqlClient.ReturnTestDatabase(

public override async Task RemoveDatabase(string connectionString)
{
// Do nothing.
// Old databases will be reused by IntegreSQL automatically.
// Previously we were removing databases here which actually interfere with IntegreSQL.
// It was 'hanging' when tried to reuse the removed databases.
if (DropDatabaseOnRemove)
{
await using var connection = new NpgsqlConnection(connectionString);
string database = connection.Database;
connection.Open();
connection.ChangeDatabase("postgres");
var command = new NpgsqlCommand($"DROP DATABASE \"{database}\"", connection);
command.ExecuteNonQuery();
}
else
{
// Do nothing by default.
// Old databases will be reused by IntegreSQL automatically.
// Previously we were removing databases here which actually interfere with IntegreSQL.
// It was 'hanging' when tried to reuse the removed databases.
// However, it was reported that if not dropped, Postgres starts to consume a lot of RAM.
// So one might be willing to drop anyway.
}
}

public override void UseProvider(DbContextOptionsBuilder options, string connectionString)
Expand Down

0 comments on commit 5cb5185

Please sign in to comment.