Skip to content

Commit

Permalink
Merge pull request #162 from omnibs/exceptionfix
Browse files Browse the repository at this point in the history
Remove reliance on exceptions to check if table exists (fixes #142)
  • Loading branch information
JakeGinnivan committed Feb 2, 2016
2 parents 845751e + 709c939 commit 2060d60
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Open connection
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'SchemaVersions' COLLATE NOCASE
Dispose command
Execute non query command: script1contents
Dispose command
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'SchemaVersions' COLLATE NOCASE
Dispose command
Execute non query command: CREATE TABLE [SchemaVersions] (
SchemaVersionID INTEGER CONSTRAINT 'PK_SchemaVersions_SchemaVersionID' PRIMARY KEY AUTOINCREMENT NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Open connection
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: script1contents
Dispose command
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: create table [SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Open connection
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: script1contents
Dispose command
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: create table [SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Open connection
Execute scalar command: select count(*) from [TestSchemaVersions]
Execute scalar command: SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'TestSchemaVersions' COLLATE NOCASE
Dispose command
Execute non query command: script1contents
Dispose command
Execute scalar command: select count(*) from [TestSchemaVersions]
Execute scalar command: SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'TestSchemaVersions' COLLATE NOCASE
Dispose command
Execute non query command: CREATE TABLE [TestSchemaVersions] (
SchemaVersionID INTEGER CONSTRAINT 'PK_TestSchemaVersions_SchemaVersionID' PRIMARY KEY AUTOINCREMENT NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Open connection
Execute scalar command: select count(*) from [test].[TestSchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'TestSchemaVersions' and TABLE_SCHEMA = 'test'
Dispose command
Execute non query command: script1contents
Dispose command
Execute scalar command: select count(*) from [test].[TestSchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'TestSchemaVersions' and TABLE_SCHEMA = 'test'
Dispose command
Execute non query command: create table [test].[TestSchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_TestSchemaVersions_Id] primary key,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Open connection
Execute scalar command: select count(*) from [test].[TestSchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'TestSchemaVersions' and TABLE_SCHEMA = 'test'
Dispose command
Execute non query command: script1contents
Dispose command
Execute scalar command: select count(*) from [test].[TestSchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'TestSchemaVersions' and TABLE_SCHEMA = 'test'
Dispose command
Execute non query command: create table [test].[TestSchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_TestSchemaVersions_Id] primary key,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Open connection
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'SchemaVersions' COLLATE NOCASE
Dispose command
Execute non query command: print SubstitutedValue
Dispose command
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'SchemaVersions' COLLATE NOCASE
Dispose command
Execute non query command: CREATE TABLE [SchemaVersions] (
SchemaVersionID INTEGER CONSTRAINT 'PK_SchemaVersions_SchemaVersionID' PRIMARY KEY AUTOINCREMENT NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Open connection
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: print SubstitutedValue
Dispose command
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: create table [SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Open connection
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: print SubstitutedValue
Dispose command
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: create table [SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
Expand Down
2 changes: 2 additions & 0 deletions src/DbUp.Tests/DbUp.approved.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ public sealed class SQLiteTableJournal : DbUp.Support.SqlServer.SqlTableJournal
public SQLiteTableJournal(System.Func<DbUp.Engine.Transactions.IConnectionManager> connectionManager, System.Func<DbUp.Engine.Output.IUpgradeLog> logger, string table) { }
protected override string CreatePrimaryKeyName(string table) { }
protected override string CreateTableSql(string schema, string table) { }
protected override bool VerifyTableExistsCommand(System.Data.IDbCommand command, string tableName, string schemaName) { }
}
}
namespace DbUp.Support.SqlServer
Expand Down Expand Up @@ -394,6 +395,7 @@ public class SqlTableJournal : DbUp.Engine.IJournal
public string[] GetExecutedScripts() { }
protected virtual string GetExecutedScriptsSql(string schema, string table) { }
public void StoreExecutedScript(DbUp.Engine.SqlScript script) { }
protected virtual bool VerifyTableExistsCommand(System.Data.IDbCommand command, string tableName, string schemaName) { }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Open connection
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: print 'script1'
Dispose command
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: create table [SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
Expand All @@ -19,7 +19,7 @@ Execute non query command: insert into [SchemaVersions] (ScriptName, Applied) va
Dispose command
Execute non query command: print 'script2'
Dispose command
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: create table [SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Open connection
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: error
Dispose command
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Open connection
Begin transaction
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: print 'script1'
Dispose command
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: create table [SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
Expand All @@ -20,7 +20,7 @@ Execute non query command: insert into [SchemaVersions] (ScriptName, Applied) va
Dispose command
Execute non query command: print 'script2'
Dispose command
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: create table [SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Open connection
Begin transaction
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Execute non query command: error
Dispose command
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Open connection
Begin transaction
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Commit transaction
Dispose transaction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Open connection
Begin transaction
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Commit transaction
Dispose transaction
Expand All @@ -10,7 +10,7 @@ Dispose command
Commit transaction
Dispose transaction
Begin transaction
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Commit transaction
Dispose transaction
Expand Down Expand Up @@ -38,7 +38,7 @@ Dispose command
Commit transaction
Dispose transaction
Begin transaction
Execute scalar command: select count(*) from [SchemaVersions]
Execute scalar command: select 1 from information_schema.tables where TABLE_NAME = 'SchemaVersions'
Dispose command
Commit transaction
Dispose transaction
Expand Down
20 changes: 16 additions & 4 deletions src/DbUp/Support/SqlServer/SqlTableJournal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ private bool DoesTableExist()
{
using (var command = dbCommandFactory())
{
command.CommandText = string.Format("select count(*) from {0}", CreateTableName(schema, table));
command.CommandType = CommandType.Text;
command.ExecuteScalar();
return true;
return VerifyTableExistsCommand(command, table, schema);
}
}
catch (SqlException)
Expand All @@ -187,5 +184,20 @@ private bool DoesTableExist()
}
});
}

/// <summary>Verify, using database-specific queries, if the table exists in the database.</summary>
/// <param name="command">The <c>IDbCommand</c> to be used for the query</param>
/// <param name="tableName">The name of the table</param>
/// <param name="schemaName">The schema for the table</param>
/// <returns>True if table exists, false otherwise</returns>
protected virtual bool VerifyTableExistsCommand(IDbCommand command, string tableName, string schemaName)
{
command.CommandText = string.IsNullOrEmpty(schema)
? string.Format("select 1 from information_schema.tables where TABLE_NAME = '{0}'", tableName)
: string.Format("select 1 from information_schema.tables where TABLE_NAME = '{0}' and TABLE_SCHEMA = '{1}'", tableName, schemaName);
command.CommandType = CommandType.Text;
var result = command.ExecuteScalar() as int?;
return result == 1;
}
}
}
14 changes: 14 additions & 0 deletions src/DbUp/Support/Sqlite/SqliteTableJournal.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Data;
using DbUp.Engine;
using DbUp.Engine.Output;
using DbUp.Engine.Transactions;
Expand Down Expand Up @@ -42,5 +43,18 @@ protected override string CreatePrimaryKeyName(string table)
{
return "'PK_" + table + "_SchemaVersionID'";
}

/// <summary>Verify, using database-specific queries, if the table exists in the database.</summary>
/// <param name="command">The <c>IDbCommand</c> to be used for the query</param>
/// <param name="tableName">The name of the table</param>
/// <param name="schemaName">The schema for the table</param>
/// <returns>True if table exists, false otherwise</returns>
protected override bool VerifyTableExistsCommand(IDbCommand command, string tableName, string schemaName)
{
command.CommandText = string.Format("SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = '{0}' COLLATE NOCASE", tableName);
command.CommandType = CommandType.Text;
var result = (long)command.ExecuteScalar();
return result == 1;
}
}
}

0 comments on commit 2060d60

Please sign in to comment.