From 521fb894652254478a89b787bea8165c801dc3cb Mon Sep 17 00:00:00 2001 From: Aashik Nagadikeri Harish Date: Tue, 11 Nov 2025 12:46:13 -0800 Subject: [PATCH 1/3] Override function to check if table exists --- ...pportTests.VerifyBasicSupport.approved.txt | 12 ++++++++++-- ...yJournalCreationIfNameChanged.approved.txt | 12 ++++++++++-- ...s.VerifyVariableSubstitutions.approved.txt | 12 ++++++++++-- .../NoPublicApiChanges.Run.approved.cs | 1 + src/dbup-postgresql/PostgresqlTableJournal.cs | 19 +++++++++++++++++-- 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt index ac69b5d..13e54bf 100644 --- a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt +++ b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt @@ -1,12 +1,20 @@ DB Operation: Open connection Info: Beginning database upgrade Info: Checking whether journal table exists -DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'schemaversions' +DB Operation: Execute scalar command: + select case + when to_regclass('"schemaversions"') is not null then 1 + else 0 + end; DB Operation: Dispose command Info: Journal table does not exist Info: Executing Database Server script 'Script0001.sql' Info: Checking whether journal table exists -DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'schemaversions' +DB Operation: Execute scalar command: + select case + when to_regclass('"schemaversions"') is not null then 1 + else 0 + end; DB Operation: Dispose command Info: Creating the "schemaversions" table DB Operation: Execute non query command: CREATE TABLE "schemaversions" diff --git a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt index 46bee6c..1df7321 100644 --- a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt +++ b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt @@ -1,12 +1,20 @@ DB Operation: Open connection Info: Beginning database upgrade Info: Checking whether journal table exists -DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'TestSchemaVersions' and TABLE_SCHEMA = 'test' +DB Operation: Execute scalar command: + select case + when to_regclass('"test"."TestSchemaVersions"') is not null then 1 + else 0 + end; DB Operation: Dispose command Info: Journal table does not exist Info: Executing Database Server script 'Script0001.sql' Info: Checking whether journal table exists -DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'TestSchemaVersions' and TABLE_SCHEMA = 'test' +DB Operation: Execute scalar command: + select case + when to_regclass('"test"."TestSchemaVersions"') is not null then 1 + else 0 + end; DB Operation: Dispose command Info: Creating the "test"."TestSchemaVersions" table DB Operation: Execute non query command: CREATE TABLE "test"."TestSchemaVersions" diff --git a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt index 3321b7a..d195fd8 100644 --- a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt +++ b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt @@ -1,12 +1,20 @@ DB Operation: Open connection Info: Beginning database upgrade Info: Checking whether journal table exists -DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'schemaversions' +DB Operation: Execute scalar command: + select case + when to_regclass('"schemaversions"') is not null then 1 + else 0 + end; DB Operation: Dispose command Info: Journal table does not exist Info: Executing Database Server script 'Script0001.sql' Info: Checking whether journal table exists -DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'schemaversions' +DB Operation: Execute scalar command: + select case + when to_regclass('"schemaversions"') is not null then 1 + else 0 + end; DB Operation: Dispose command Info: Creating the "schemaversions" table DB Operation: Execute non query command: CREATE TABLE "schemaversions" diff --git a/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs b/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs index b3131dc..2ab976e 100644 --- a/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs +++ b/src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs @@ -63,6 +63,7 @@ public class PostgresqlTableJournal : DbUp.Support.TableJournal { public PostgresqlTableJournal(System.Func connectionManager, System.Func logger, string schema, string tableName) { } protected override string CreateSchemaTableSql(string quotedPrimaryKeyName) { } + protected override string DoesTableExistSql() { } protected override string GetInsertJournalEntrySql(string scriptName, string applied) { } protected override System.Data.IDbCommand GetInsertScriptCommand(System.Func dbCommandFactory, DbUp.Engine.SqlScript script) { } protected override string GetJournalEntriesSql() { } diff --git a/src/dbup-postgresql/PostgresqlTableJournal.cs b/src/dbup-postgresql/PostgresqlTableJournal.cs index 1fe0e0f..a351113 100644 --- a/src/dbup-postgresql/PostgresqlTableJournal.cs +++ b/src/dbup-postgresql/PostgresqlTableJournal.cs @@ -1,9 +1,10 @@ -using System; +using System; using System.Data; using DbUp.Engine; using DbUp.Engine.Output; using DbUp.Engine.Transactions; using DbUp.Support; +using Npgsql; namespace DbUp.Postgresql; @@ -74,4 +75,18 @@ scriptname character varying(255) NOT NULL, CONSTRAINT {quotedPrimaryKeyName} PRIMARY KEY (schemaversionsid) )"; } -} \ No newline at end of file + + /// + protected override string DoesTableExistSql() + { + string fqSchemaTableName = FqSchemaTableName.Replace("'", "''"); + + string sql = $@" + select case + when to_regclass('{fqSchemaTableName}') is not null then 1 + else 0 + end;"; + + return sql; + } +} From efb2ffea19f9b8de52294ca3c42b44b3e95b7be4 Mon Sep 17 00:00:00 2001 From: Aashik Nagadikeri Harish Date: Tue, 11 Nov 2025 13:00:46 -0800 Subject: [PATCH 2/3] Fixed whitespace --- ...eSupportTests.VerifyBasicSupport.approved.txt | 16 ++++++++-------- ...rifyJournalCreationIfNameChanged.approved.txt | 16 ++++++++-------- ...ests.VerifyVariableSubstitutions.approved.txt | 16 ++++++++-------- src/dbup-postgresql/PostgresqlTableJournal.cs | 9 ++++----- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt index 13e54bf..cb5f5fd 100644 --- a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt +++ b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt @@ -2,19 +2,19 @@ Info: Beginning database upgrade Info: Checking whether journal table exists DB Operation: Execute scalar command: - select case - when to_regclass('"schemaversions"') is not null then 1 - else 0 - end; +select case + when to_regclass('"schemaversions"') is not null then 1 + else 0 +end; DB Operation: Dispose command Info: Journal table does not exist Info: Executing Database Server script 'Script0001.sql' Info: Checking whether journal table exists DB Operation: Execute scalar command: - select case - when to_regclass('"schemaversions"') is not null then 1 - else 0 - end; +select case + when to_regclass('"schemaversions"') is not null then 1 + else 0 +end; DB Operation: Dispose command Info: Creating the "schemaversions" table DB Operation: Execute non query command: CREATE TABLE "schemaversions" diff --git a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt index 1df7321..3f5c4dc 100644 --- a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt +++ b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt @@ -2,19 +2,19 @@ Info: Beginning database upgrade Info: Checking whether journal table exists DB Operation: Execute scalar command: - select case - when to_regclass('"test"."TestSchemaVersions"') is not null then 1 - else 0 - end; +select case + when to_regclass('"test"."TestSchemaVersions"') is not null then 1 + else 0 +end; DB Operation: Dispose command Info: Journal table does not exist Info: Executing Database Server script 'Script0001.sql' Info: Checking whether journal table exists DB Operation: Execute scalar command: - select case - when to_regclass('"test"."TestSchemaVersions"') is not null then 1 - else 0 - end; +select case + when to_regclass('"test"."TestSchemaVersions"') is not null then 1 + else 0 +end; DB Operation: Dispose command Info: Creating the "test"."TestSchemaVersions" table DB Operation: Execute non query command: CREATE TABLE "test"."TestSchemaVersions" diff --git a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt index d195fd8..7749611 100644 --- a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt +++ b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt @@ -2,19 +2,19 @@ Info: Beginning database upgrade Info: Checking whether journal table exists DB Operation: Execute scalar command: - select case - when to_regclass('"schemaversions"') is not null then 1 - else 0 - end; +select case + when to_regclass('"schemaversions"') is not null then 1 + else 0 +end; DB Operation: Dispose command Info: Journal table does not exist Info: Executing Database Server script 'Script0001.sql' Info: Checking whether journal table exists DB Operation: Execute scalar command: - select case - when to_regclass('"schemaversions"') is not null then 1 - else 0 - end; +select case + when to_regclass('"schemaversions"') is not null then 1 + else 0 +end; DB Operation: Dispose command Info: Creating the "schemaversions" table DB Operation: Execute non query command: CREATE TABLE "schemaversions" diff --git a/src/dbup-postgresql/PostgresqlTableJournal.cs b/src/dbup-postgresql/PostgresqlTableJournal.cs index a351113..437c78b 100644 --- a/src/dbup-postgresql/PostgresqlTableJournal.cs +++ b/src/dbup-postgresql/PostgresqlTableJournal.cs @@ -4,7 +4,6 @@ using DbUp.Engine.Output; using DbUp.Engine.Transactions; using DbUp.Support; -using Npgsql; namespace DbUp.Postgresql; @@ -82,10 +81,10 @@ protected override string DoesTableExistSql() string fqSchemaTableName = FqSchemaTableName.Replace("'", "''"); string sql = $@" - select case - when to_regclass('{fqSchemaTableName}') is not null then 1 - else 0 - end;"; +select case + when to_regclass('{fqSchemaTableName}') is not null then 1 + else 0 +end;"; return sql; } From 6b5f761814a79302cdcde7a33584d6e848165027 Mon Sep 17 00:00:00 2001 From: Aashik Nagadikeri Harish Date: Tue, 11 Nov 2025 13:10:14 -0800 Subject: [PATCH 3/3] Added full function name --- .../DatabaseSupportTests.VerifyBasicSupport.approved.txt | 4 ++-- ...pportTests.VerifyJournalCreationIfNameChanged.approved.txt | 4 ++-- ...abaseSupportTests.VerifyVariableSubstitutions.approved.txt | 4 ++-- src/dbup-postgresql/PostgresqlTableJournal.cs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt index cb5f5fd..2132e1a 100644 --- a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt +++ b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt @@ -3,7 +3,7 @@ Info: Beginning database upgrade Info: Checking whether journal table exists DB Operation: Execute scalar command: select case - when to_regclass('"schemaversions"') is not null then 1 + when pg_catalog.to_regclass('"schemaversions"') is not null then 1 else 0 end; DB Operation: Dispose command @@ -12,7 +12,7 @@ Info: Executing Database Server script 'Script0001.sql' Info: Checking whether journal table exists DB Operation: Execute scalar command: select case - when to_regclass('"schemaversions"') is not null then 1 + when pg_catalog.to_regclass('"schemaversions"') is not null then 1 else 0 end; DB Operation: Dispose command diff --git a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt index 3f5c4dc..ed1a276 100644 --- a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt +++ b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt @@ -3,7 +3,7 @@ Info: Beginning database upgrade Info: Checking whether journal table exists DB Operation: Execute scalar command: select case - when to_regclass('"test"."TestSchemaVersions"') is not null then 1 + when pg_catalog.to_regclass('"test"."TestSchemaVersions"') is not null then 1 else 0 end; DB Operation: Dispose command @@ -12,7 +12,7 @@ Info: Executing Database Server script 'Script0001.sql' Info: Checking whether journal table exists DB Operation: Execute scalar command: select case - when to_regclass('"test"."TestSchemaVersions"') is not null then 1 + when pg_catalog.to_regclass('"test"."TestSchemaVersions"') is not null then 1 else 0 end; DB Operation: Dispose command diff --git a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt index 7749611..6b59624 100644 --- a/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt +++ b/src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt @@ -3,7 +3,7 @@ Info: Beginning database upgrade Info: Checking whether journal table exists DB Operation: Execute scalar command: select case - when to_regclass('"schemaversions"') is not null then 1 + when pg_catalog.to_regclass('"schemaversions"') is not null then 1 else 0 end; DB Operation: Dispose command @@ -12,7 +12,7 @@ Info: Executing Database Server script 'Script0001.sql' Info: Checking whether journal table exists DB Operation: Execute scalar command: select case - when to_regclass('"schemaversions"') is not null then 1 + when pg_catalog.to_regclass('"schemaversions"') is not null then 1 else 0 end; DB Operation: Dispose command diff --git a/src/dbup-postgresql/PostgresqlTableJournal.cs b/src/dbup-postgresql/PostgresqlTableJournal.cs index 437c78b..e76616f 100644 --- a/src/dbup-postgresql/PostgresqlTableJournal.cs +++ b/src/dbup-postgresql/PostgresqlTableJournal.cs @@ -82,7 +82,7 @@ protected override string DoesTableExistSql() string sql = $@" select case - when to_regclass('{fqSchemaTableName}') is not null then 1 + when pg_catalog.to_regclass('{fqSchemaTableName}') is not null then 1 else 0 end;";