Skip to content

Commit

Permalink
calling the drop database in a separate command. Closes GH-3366
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Aug 15, 2024
1 parent 7767908 commit 77d4053
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/CoreTests/create_database_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ await Should.ThrowAsync<PostgresException>(async () =>
// Specify a db to which to connect in case database needs to be created.
// If not specified, defaults to 'postgres' on the connection for a tenant.
c.MaintenanceDatabase(cstring);

c.ForTenant()
.CheckAgainstPgDatabase()

.DropExisting()
.WithOwner("postgres")
.WithEncoding("UTF-8")
.ConnectionLimit(-1)
Expand Down
36 changes: 22 additions & 14 deletions src/Marten/Schema/DatabaseGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,18 @@ private async Task CreateDbAsync(
CancellationToken ct = default
)
{
var cmdText = string.Empty;

if (dropExisting)
{
if (config.KillConnections)
{
cmdText =
$"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '{catalog}' AND pid <> pg_backend_pid();";
}

cmdText += $"DROP DATABASE IF EXISTS \"{catalog}\";";
await dropCurrentDatabaseAsync(catalog, config, maintenanceDb, ct).ConfigureAwait(false);
}

var connection = new NpgsqlConnection(maintenanceDb);
await using var _ = connection.ConfigureAwait(false);
await using var cmd = connection.CreateCommand(cmdText);
await using var __ = cmd.ConfigureAwait(false);
cmd.CommandText += $"CREATE DATABASE \"{catalog}\" WITH" + config;
await using var connection = new NpgsqlConnection(maintenanceDb);
await connection.OpenAsync(ct).ConfigureAwait(false);
try
{
await cmd.ExecuteNonQueryAsync(ct).ConfigureAwait(false);
await connection.CreateCommand($"CREATE DATABASE \"{catalog}\" WITH" + config)
.ExecuteNonQueryAsync(ct).ConfigureAwait(false);
config.OnDbCreated?.Invoke(connection);
}
finally
Expand All @@ -158,6 +148,24 @@ private async Task CreateDbAsync(
}
}

private static async Task dropCurrentDatabaseAsync(string catalog, TenantDatabaseCreationExpressions config,
string maintenanceDb, CancellationToken ct)
{
var cmdText = string.Empty;
if (config.KillConnections)
{
cmdText =
$"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '{catalog}' AND pid <> pg_backend_pid();";
}

cmdText += $"DROP DATABASE IF EXISTS \"{catalog}\";";

await using var conn = new NpgsqlConnection(maintenanceDb);
await conn.OpenAsync(ct).ConfigureAwait(false);
await conn.CreateCommand(cmdText).ExecuteNonQueryAsync(ct).ConfigureAwait(false);
await conn.CloseAsync().ConfigureAwait(false);
}

private sealed class TenantDatabaseCreationExpressions: ITenantDatabaseCreationExpressions
{
private readonly StringBuilder _createOptions = new();
Expand Down

0 comments on commit 77d4053

Please sign in to comment.