Skip to content

Commit 5aa672e

Browse files
aashikgowdaAashik Nagadikeri Harish
andauthored
Use Postgres function to_regclass to check for existence of table (#40)
* Override function to check if table exists * Fixed whitespace * Added full function name --------- Co-authored-by: Aashik Nagadikeri Harish <aashik@StriVR-HQ.local>
1 parent 144c6b7 commit 5aa672e

5 files changed

Lines changed: 47 additions & 8 deletions

src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyBasicSupport.approved.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
DB Operation: Open connection
22
Info: Beginning database upgrade
33
Info: Checking whether journal table exists
4-
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'schemaversions'
4+
DB Operation: Execute scalar command:
5+
select case
6+
when pg_catalog.to_regclass('"schemaversions"') is not null then 1
7+
else 0
8+
end;
59
DB Operation: Dispose command
610
Info: Journal table does not exist
711
Info: Executing Database Server script 'Script0001.sql'
812
Info: Checking whether journal table exists
9-
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'schemaversions'
13+
DB Operation: Execute scalar command:
14+
select case
15+
when pg_catalog.to_regclass('"schemaversions"') is not null then 1
16+
else 0
17+
end;
1018
DB Operation: Dispose command
1119
Info: Creating the "schemaversions" table
1220
DB Operation: Execute non query command: CREATE TABLE "schemaversions"

src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyJournalCreationIfNameChanged.approved.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
DB Operation: Open connection
22
Info: Beginning database upgrade
33
Info: Checking whether journal table exists
4-
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'TestSchemaVersions' and TABLE_SCHEMA = 'test'
4+
DB Operation: Execute scalar command:
5+
select case
6+
when pg_catalog.to_regclass('"test"."TestSchemaVersions"') is not null then 1
7+
else 0
8+
end;
59
DB Operation: Dispose command
610
Info: Journal table does not exist
711
Info: Executing Database Server script 'Script0001.sql'
812
Info: Checking whether journal table exists
9-
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'TestSchemaVersions' and TABLE_SCHEMA = 'test'
13+
DB Operation: Execute scalar command:
14+
select case
15+
when pg_catalog.to_regclass('"test"."TestSchemaVersions"') is not null then 1
16+
else 0
17+
end;
1018
DB Operation: Dispose command
1119
Info: Creating the "test"."TestSchemaVersions" table
1220
DB Operation: Execute non query command: CREATE TABLE "test"."TestSchemaVersions"

src/Tests/ApprovalFiles/DatabaseSupportTests.VerifyVariableSubstitutions.approved.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
DB Operation: Open connection
22
Info: Beginning database upgrade
33
Info: Checking whether journal table exists
4-
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'schemaversions'
4+
DB Operation: Execute scalar command:
5+
select case
6+
when pg_catalog.to_regclass('"schemaversions"') is not null then 1
7+
else 0
8+
end;
59
DB Operation: Dispose command
610
Info: Journal table does not exist
711
Info: Executing Database Server script 'Script0001.sql'
812
Info: Checking whether journal table exists
9-
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'schemaversions'
13+
DB Operation: Execute scalar command:
14+
select case
15+
when pg_catalog.to_regclass('"schemaversions"') is not null then 1
16+
else 0
17+
end;
1018
DB Operation: Dispose command
1119
Info: Creating the "schemaversions" table
1220
DB Operation: Execute non query command: CREATE TABLE "schemaversions"

src/Tests/ApprovalFiles/NoPublicApiChanges.Run.approved.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public class PostgresqlTableJournal : DbUp.Support.TableJournal
6464
{
6565
public PostgresqlTableJournal(System.Func<DbUp.Engine.Transactions.IConnectionManager> connectionManager, System.Func<DbUp.Engine.Output.IUpgradeLog> logger, string schema, string tableName) { }
6666
protected override string CreateSchemaTableSql(string quotedPrimaryKeyName) { }
67+
protected override string DoesTableExistSql() { }
6768
protected override string GetInsertJournalEntrySql(string scriptName, string applied) { }
6869
protected override System.Data.IDbCommand GetInsertScriptCommand(System.Func<System.Data.IDbCommand> dbCommandFactory, DbUp.Engine.SqlScript script) { }
6970
protected override string GetJournalEntriesSql() { }

src/dbup-postgresql/PostgresqlTableJournal.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Data;
33
using DbUp.Engine;
44
using DbUp.Engine.Output;
@@ -74,4 +74,18 @@ scriptname character varying(255) NOT NULL,
7474
CONSTRAINT {quotedPrimaryKeyName} PRIMARY KEY (schemaversionsid)
7575
)";
7676
}
77-
}
77+
78+
/// <inheritdoc/>
79+
protected override string DoesTableExistSql()
80+
{
81+
string fqSchemaTableName = FqSchemaTableName.Replace("'", "''");
82+
83+
string sql = $@"
84+
select case
85+
when pg_catalog.to_regclass('{fqSchemaTableName}') is not null then 1
86+
else 0
87+
end;";
88+
89+
return sql;
90+
}
91+
}

0 commit comments

Comments
 (0)