@@ -327,67 +327,65 @@ PostgresqlConnectionOptions connectionOptions
327327
328328 logger . LogDebug ( "Master ConnectionString => {0}" , logMasterConnectionStringBuilder . ConnectionString ) ;
329329
330- using ( var connection = new NpgsqlConnection ( masterConnectionStringBuilder . ConnectionString ) )
331- {
332- connection . ApplyConnectionOptions ( connectionOptions ) ;
333- connection . Open ( ) ;
330+ var factory = new DataSourceConnectionFactory ( masterConnectionStringBuilder . ConnectionString , connectionOptions ) ;
331+ using var connection = factory . CreateConnection ( ) ;
332+ connection . Open ( ) ;
334333
335- var sqlCommandText =
336- $@ "SELECT case WHEN oid IS NOT NULL THEN 1 ELSE 0 end FROM pg_database WHERE datname = '{ databaseName } ' limit 1;";
334+ var sqlCommandText =
335+ $@ "SELECT case WHEN oid IS NOT NULL THEN 1 ELSE 0 end FROM pg_database WHERE datname = '{ databaseName } ' limit 1;";
337336
338- // check to see if the database already exists..
339- using ( var command = new NpgsqlCommand ( sqlCommandText , connection )
340- {
341- CommandType = CommandType . Text
342- } )
343- {
344- var results = Convert . ToInt32 ( command . ExecuteScalar ( ) ) ;
345-
346- // if the database does not exist, we're done here...
347- if ( results == 0 )
348- {
349- logger . LogInformation ( @"Database {0} does not exist. Skipping delete operation." , databaseName ) ;
350- return ;
351- }
352- }
337+ // check to see if the database already exists..
338+ using ( var command = new NpgsqlCommand ( sqlCommandText , connection )
339+ {
340+ CommandType = CommandType . Text
341+ } )
342+ {
343+ var results = Convert . ToInt32 ( command . ExecuteScalar ( ) ) ;
353344
354- // prevent new connections to the database
355- sqlCommandText = $ "alter database \" { databaseName } \" with ALLOW_CONNECTIONS false;";
356- using ( var command = new NpgsqlCommand ( sqlCommandText , connection )
345+ // if the database does not exist, we're done here...
346+ if ( results == 0 )
357347 {
358- CommandType = CommandType . Text
359- } )
360- {
361- command . ExecuteNonQuery ( ) ;
348+ logger . LogInformation ( @"Database {0} does not exist. Skipping delete operation." , databaseName ) ;
349+ return ;
362350 }
351+ }
363352
364- logger . LogInformation ( @"Stopped connections for database {0}." , databaseName ) ;
353+ // prevent new connections to the database
354+ sqlCommandText = $ "alter database \" { databaseName } \" with ALLOW_CONNECTIONS false;";
355+ using ( var command = new NpgsqlCommand ( sqlCommandText , connection )
356+ {
357+ CommandType = CommandType . Text
358+ } )
359+ {
360+ command . ExecuteNonQuery ( ) ;
361+ }
365362
366- // terminate all existing connections to the database
367- sqlCommandText = $ "select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where pg_stat_activity.datname = \' { databaseName } \' ;";
368- using ( var command = new NpgsqlCommand ( sqlCommandText , connection )
369- {
370- CommandType = CommandType . Text
371- } )
372- {
373- command . ExecuteNonQuery ( ) ;
374- }
363+ logger . LogInformation ( @"Stopped connections for database {0}." , databaseName ) ;
375364
376- logger . LogInformation ( @"Closed existing connections for database {0}." , databaseName ) ;
365+ // terminate all existing connections to the database
366+ sqlCommandText = $ "select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where pg_stat_activity.datname = \' { databaseName } \' ;";
367+ using ( var command = new NpgsqlCommand ( sqlCommandText , connection )
368+ {
369+ CommandType = CommandType . Text
370+ } )
371+ {
372+ command . ExecuteNonQuery ( ) ;
373+ }
377374
378- sqlCommandText = $ "drop database \" { databaseName } \" ;" ;
375+ logger . LogInformation ( @"Closed existing connections for database {0}." , databaseName ) ;
379376
380- // drop the database
381- using ( var command = new NpgsqlCommand ( sqlCommandText , connection )
382- {
383- CommandType = CommandType . Text
384- } )
385- {
386- command . ExecuteNonQuery ( ) ;
387- }
377+ sqlCommandText = $ "drop database \" { databaseName } \" ;";
388378
389- logger . LogInformation ( @"Dropped database {0}." , databaseName ) ;
379+ // drop the database
380+ using ( var command = new NpgsqlCommand ( sqlCommandText , connection )
381+ {
382+ CommandType = CommandType . Text
383+ } )
384+ {
385+ command . ExecuteNonQuery ( ) ;
390386 }
387+
388+ logger . LogInformation ( @"Dropped database {0}." , databaseName ) ;
391389 }
392390
393391 /// <summary>
0 commit comments