-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SchemaUtils to DropSchema with retries
- Loading branch information
1 parent
7164285
commit 33e363a
Showing
3 changed files
with
54 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Npgsql; | ||
using Weasel.Postgresql; | ||
|
||
namespace CoreTests.Util; | ||
|
||
public static class SchemaUtils | ||
{ | ||
// TODO: This should probably go to Weasel | ||
public static async Task DropSchema(string connectionString, string schemaName) | ||
{ | ||
var reconnectionCount = 0; | ||
const int maxReconnectionCount = 3; | ||
|
||
var success = false; | ||
|
||
do | ||
{ | ||
success = await dropSchema(connectionString, schemaName); | ||
|
||
if (success || ++reconnectionCount == maxReconnectionCount) | ||
return; | ||
|
||
await Task.Delay(reconnectionCount * 50).ConfigureAwait(false); | ||
} while (!success && reconnectionCount < maxReconnectionCount); | ||
|
||
throw new InvalidOperationException($"Unable to drop schema: ${schemaName}"); | ||
} | ||
|
||
private static async Task<bool> dropSchema(string connectionString, string schemaName) | ||
{ | ||
try | ||
{ | ||
await using var dbConn = new NpgsqlConnection(connectionString); | ||
await dbConn.OpenAsync(); | ||
await dbConn.DropSchema(schemaName); | ||
|
||
return true; | ||
} | ||
catch (PostgresException pgException) | ||
{ | ||
if (pgException.SqlState == PostgresErrorCodes.AdminShutdown) | ||
return false; | ||
|
||
throw; | ||
} | ||
} | ||
} |