Skip to content

Commit

Permalink
Handle another error code when concurrently creating the migrations h…
Browse files Browse the repository at this point in the history
…istory table
  • Loading branch information
roji committed Sep 22, 2024
1 parent a894bdc commit 79608d6
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/EFCore.PG/Migrations/Internal/NpgsqlHistoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,34 +103,34 @@ protected override bool InterpretExistsResult(object? value)

bool IHistoryRepository.CreateIfNotExists()
{
// In PG, doing CREATE TABLE IF NOT EXISTS concurrently can result in a unique constraint violation
// (duplicate key value violates unique constraint "pg_type_typname_nsp_index"). We catch this and report that the table wasn't
// created.
// In PG, doing CREATE TABLE IF NOT EXISTS isn't concurrency-safe, and can result a "duplicate table" error or in a unique
// constraint violation (duplicate key value violates unique constraint "pg_type_typname_nsp_index").
// We catch this and report that the table wasn't created.
try
{
return Dependencies.MigrationCommandExecutor.ExecuteNonQuery(
GetCreateIfNotExistsCommands(), Dependencies.Connection, new MigrationExecutionState(), commitTransaction: true)
!= 0;
}
catch (PostgresException e) when (e.SqlState == "23505")
catch (PostgresException e) when (e.SqlState is "23505" or "42P07")
{
return false;
}
}

async Task<bool> IHistoryRepository.CreateIfNotExistsAsync(CancellationToken cancellationToken)
{
// In PG, doing CREATE TABLE IF NOT EXISTS concurrently can result in a unique constraint violation
// (duplicate key value violates unique constraint "pg_type_typname_nsp_index"). We catch this and report that the table wasn't
// created.
// In PG, doing CREATE TABLE IF NOT EXISTS isn't concurrency-safe, and can result a "duplicate table" error or in a unique
// constraint violation (duplicate key value violates unique constraint "pg_type_typname_nsp_index").
// We catch this and report that the table wasn't created.
try
{
return (await Dependencies.MigrationCommandExecutor.ExecuteNonQueryAsync(
GetCreateIfNotExistsCommands(), Dependencies.Connection, new MigrationExecutionState(), commitTransaction: true,
cancellationToken: cancellationToken).ConfigureAwait(false))
!= 0;
}
catch (PostgresException e) when (e.SqlState == "23505")
catch (PostgresException e) when (e.SqlState is "23505" or "42P07")
{
return false;
}
Expand Down

0 comments on commit 79608d6

Please sign in to comment.