From 5cb5185146454ed02b3f1ed73130dd383903e033 Mon Sep 17 00:00:00 2001 From: Artur Drobinskiy Date: Thu, 2 Feb 2023 22:27:20 +0700 Subject: [PATCH] `DropDatabaseOnRemove` added --- .../MccSoft.IntegreSql.EF.csproj | 2 +- .../NpgsqlDatabaseInitializer.cs | 34 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/MccSoft.IntegreSql.EF/MccSoft.IntegreSql.EF.csproj b/src/MccSoft.IntegreSql.EF/MccSoft.IntegreSql.EF.csproj index 6a67ee1..d2a3650 100644 --- a/src/MccSoft.IntegreSql.EF/MccSoft.IntegreSql.EF.csproj +++ b/src/MccSoft.IntegreSql.EF/MccSoft.IntegreSql.EF.csproj @@ -1,7 +1,7 @@  - 0.8.15 + 0.8.16 true MCC Soft OpenIddict extension to support Auth code flow fo built-in ASP.Net identity providers diff --git a/src/MccSoft.IntegreSql.EF/NpgsqlDatabaseInitializer.cs b/src/MccSoft.IntegreSql.EF/NpgsqlDatabaseInitializer.cs index 8f81b02..a1318f8 100644 --- a/src/MccSoft.IntegreSql.EF/NpgsqlDatabaseInitializer.cs +++ b/src/MccSoft.IntegreSql.EF/NpgsqlDatabaseInitializer.cs @@ -31,6 +31,18 @@ public class NpgsqlDatabaseInitializer : BaseDatabaseInitializer /// public static bool UseMd5Hash = true; + /// + /// 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). + /// + public bool DropDatabaseOnRemove { get; set; } + private record ConnectionStringInfo(string Hash, int Id); /// @@ -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)