22// Licensed under MIT license. See License.txt in the project root for license information.
33
44using System ;
5- using System . Collections . Generic ;
6- using System . Reflection ;
75using System . Runtime . CompilerServices ;
86using System . Threading . Tasks ;
97using Microsoft . EntityFrameworkCore ;
@@ -83,10 +81,6 @@ public static DbContextOptions<T> CreatePostgreSqlUniqueMethodOptions<T>(this ob
8381 //------------------------------------------------------------
8482 //methods to provide empty PostgreSql databases and how to delete all the PostgreSql databases used for unit testing
8583
86- static Checkpoint EmptyCheckpoint = new Checkpoint
87- {
88- DbAdapter = DbAdapter . Postgres
89- } ;
9084
9185 /// <summary>
9286 /// This will ensure that there is a PostgreSql database, and that database has no rows in any tables
@@ -99,7 +93,12 @@ public static DbContextOptions<T> CreatePostgreSqlUniqueMethodOptions<T>(this ob
9993 public async static Task EnsureCreatedAndEmptyPostgreSqlAsync < T > ( this T context )
10094 where T : DbContext
10195 {
102- if ( ! context . Database . EnsureCreated ( ) )
96+ Checkpoint EmptyCheckpoint = new Checkpoint
97+ {
98+ DbAdapter = DbAdapter . Postgres
99+ } ;
100+
101+ if ( ! context . Database . EnsureCreated ( ) )
103102 {
104103 //the database arealy exists, so we just need to empty the tables
105104
@@ -137,52 +136,9 @@ public async static Task EnsureCreatedAndEmptyPostgreSqlAsync<T>(this T context)
137136 } ;
138137 }
139138
140- /// <summary>
141- /// This will delete all PostgreSql databases that start with the database name in the default connection string
142- /// WARNING: This will delete multiple databases - make sure your <see cref="AppSettings.PostgreSqlConnectionString"/> database name is unique!!!
143- /// </summary>
144- /// <returns>Number of databases deleted</returns>
145- public static int DeleteAllPostgreSqlUnitTestDatabases ( )
146- {
147- var config = AppSettings . GetConfiguration ( Assembly . GetCallingAssembly ( ) ) ;
148- var baseConnection = config . GetConnectionString ( AppSettings . PostgreSqlConnectionString ) ;
149- if ( string . IsNullOrEmpty ( baseConnection ) )
150- throw new InvalidOperationException (
151- $ "Your { AppSettings . AppSettingFilename } file isn't set up for the '{ AppSettings . PostgreSqlConnectionString } '.") ;
152-
153- var databaseNamesToDelete = baseConnection . GetAllPostgreUnitTestDatabases ( ) ;
154-
155- var builder = new NpgsqlConnectionStringBuilder ( baseConnection ) ;
156- builder . Database = "postgres" ;
157- foreach ( var databaseName in databaseNamesToDelete )
158- {
159- builder . ToString ( ) . ExecuteScalars (
160- $ "REVOKE CONNECT ON DATABASE \" { databaseName } \" FROM PUBLIC",
161- "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity " +
162- $ "WHERE datname = '{ databaseName } '",
163- $ "DROP DATABASE \" { databaseName } \" "
164- ) ;
165- }
166- return databaseNamesToDelete . Count ;
167- }
168-
169- //------------------------------------
139+ //------------------------------------------------
170140 //private methods
171141
172- private static void ExecuteScalars ( this string connectionString , params string [ ] commands )
173- {
174- using ( NpgsqlConnection conn = new NpgsqlConnection ( connectionString ) )
175- {
176- conn . Open ( ) ;
177- foreach ( var commandText in commands )
178- {
179- using ( NpgsqlCommand cmd = new NpgsqlCommand ( commandText , conn ) )
180- {
181- var result = cmd . ExecuteScalar ( ) ;
182- }
183- }
184- }
185- }
186142
187143 private static DbContextOptionsBuilder < T > CreatePostgreSqlOptionWithDatabaseName < T > ( object callingClass ,
188144 string callingMember , Action < DbContextOptionsBuilder < T > > extraOptions )
@@ -197,31 +153,6 @@ private static DbContextOptionsBuilder<T> CreatePostgreSqlOptionWithDatabaseName
197153 return builder ;
198154 }
199155
200- private static List < string > GetAllPostgreUnitTestDatabases ( this string connectionString )
201- {
202- var builder = new NpgsqlConnectionStringBuilder ( connectionString ) ;
203- var orgDbStartsWith = builder . Database ;
204- builder . Database = "postgres" ;
205- var newConnectionString = builder . ToString ( ) ;
206156
207- var result = new List < string > ( ) ;
208- using ( NpgsqlConnection conn = new NpgsqlConnection ( newConnectionString ) )
209- {
210- conn . Open ( ) ;
211- string cmdText = $ "SELECT datName FROM pg_database WHERE datname LIKE '{ orgDbStartsWith } %'";
212- using ( NpgsqlCommand cmd = new NpgsqlCommand ( cmdText , conn ) )
213- {
214- using ( var reader = cmd . ExecuteReader ( ) )
215- {
216- while ( reader . Read ( ) )
217- {
218- result . Add ( reader . GetString ( 0 ) ) ;
219- }
220- }
221- }
222- }
223-
224- return result ;
225- }
226157 }
227158}
0 commit comments