Skip to content

Commit

Permalink
Created the schema if it didn't exist (SQL Server only). Fixed up a S…
Browse files Browse the repository at this point in the history
…qlCe test that was using the wrong journal. Fixes #346
  • Loading branch information
droyad committed Aug 3, 2018
1 parent 637ca5e commit a4f8bd1
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/dbup-core/Support/TableJournal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace DbUp.Support
/// </summary>
public abstract class TableJournal : IJournal
{
readonly ISqlObjectParser sqlObjectParser;
bool journalExists;

/// <summary>
Expand All @@ -31,7 +30,7 @@ public abstract class TableJournal : IJournal
ISqlObjectParser sqlObjectParser,
string schema, string table)
{
this.sqlObjectParser = sqlObjectParser;
this.SqlObjectParser = sqlObjectParser;
ConnectionManager = connectionManager;
Log = logger;
UnquotedSchemaTableName = table;
Expand All @@ -42,6 +41,7 @@ public abstract class TableJournal : IJournal
}

protected string SchemaTableSchema { get; private set; }
protected ISqlObjectParser SqlObjectParser { get; }

/// <summary>
/// Schema table name, no schema and unquoted
Expand Down Expand Up @@ -130,7 +130,7 @@ protected IDbCommand GetJournalEntriesCommand(Func<IDbCommand> dbCommandFactory)
protected IDbCommand GetCreateTableCommand(Func<IDbCommand> dbCommandFactory)
{
var command = dbCommandFactory();
var primaryKeyName = sqlObjectParser.QuoteIdentifier("PK_" + UnquotedSchemaTableName + "_Id");
var primaryKeyName = SqlObjectParser.QuoteIdentifier("PK_" + UnquotedSchemaTableName + "_Id");
command.CommandText = CreateSchemaTableSql(primaryKeyName);
command.CommandType = CommandType.Text;
return command;
Expand Down Expand Up @@ -161,7 +161,7 @@ protected IDbCommand GetCreateTableCommand(Func<IDbCommand> dbCommandFactory)
/// <param name="quotedIdentifier">identifier to unquote.</param>
protected string UnquoteSqlObjectName(string quotedIdentifier)
{
return sqlObjectParser.UnquoteIdentifier(quotedIdentifier);
return SqlObjectParser.UnquoteIdentifier(quotedIdentifier);
}

protected virtual void OnTableCreated(Func<IDbCommand> dbCommandFactory)
Expand Down
11 changes: 9 additions & 2 deletions src/dbup-sqlserver/SqlTableJournal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ protected override string GetJournalEntriesSql()

protected override string CreateSchemaTableSql(string quotedPrimaryKeyName)
{
return
$@"create table {FqSchemaTableName} (
var query = "";

if (!string.IsNullOrEmpty(SchemaTableSchema))
query += $@"IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{SchemaTableSchema}')
EXEC('CREATE SCHEMA {SqlObjectParser.QuoteIdentifier(SchemaTableSchema)}')
";

query += $@"create table {FqSchemaTableName} (
[Id] int identity(1,1) not null constraint {quotedPrimaryKeyName} primary key,
[ScriptName] nvarchar(255) not null,
[Applied] datetime not null
)";
return query;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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 count(*) FROM information_schema.tables WHERE table_schema = 'test'AND table_name = 'TestSchemaVersions')
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 count(*) FROM information_schema.tables WHERE table_schema = 'test'AND table_name = 'TestSchemaVersions')
DB Operation: Dispose command
Info: Creating the [test].[TestSchemaVersions] table
DB Operation: Execute non query command: create table [test].[TestSchemaVersions] (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ 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: Dispose command
Info: Creating the [test].[TestSchemaVersions] table
DB Operation: Execute non query command: create table [test].[TestSchemaVersions] (
DB Operation: Execute non query command: IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'test')
EXEC('CREATE SCHEMA [test]')
create table [test].[TestSchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_TestSchemaVersions_Id] primary key,
[ScriptName] nvarchar(255) not null,
[Applied] datetime not null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Info: Checking whether journal table exists..
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'SchemaVersions' and TABLE_SCHEMA = 'dbo'
DB Operation: Dispose command
Info: Creating the [dbo].[SchemaVersions] table
DB Operation: Execute non query command: create table [dbo].[SchemaVersions] (
DB Operation: Execute non query command: IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'dbo')
EXEC('CREATE SCHEMA [dbo]')
create table [dbo].[SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
[ScriptName] nvarchar(255) not null,
[Applied] datetime not null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES wh
DB Operation: Dispose command
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'SchemaVersions' and TABLE_SCHEMA = 'dbo'
DB Operation: Dispose command
DB Operation: Execute non query command: create table [dbo].[SchemaVersions] (
DB Operation: Execute non query command: IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'dbo')
EXEC('CREATE SCHEMA [dbo]')
create table [dbo].[SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
[ScriptName] nvarchar(255) not null,
[Applied] datetime not null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Info: Checking whether journal table exists..
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'SchemaVersions' and TABLE_SCHEMA = 'dbo'
DB Operation: Dispose command
Info: Creating the [dbo].[SchemaVersions] table
DB Operation: Execute non query command: create table [dbo].[SchemaVersions] (
DB Operation: Execute non query command: IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'dbo')
EXEC('CREATE SCHEMA [dbo]')
create table [dbo].[SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
[ScriptName] nvarchar(255) not null,
[Applied] datetime not null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES wh
DB Operation: Dispose command
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'SchemaVersions' and TABLE_SCHEMA = 'dbo'
DB Operation: Dispose command
DB Operation: Execute non query command: create table [dbo].[SchemaVersions] (
DB Operation: Execute non query command: IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'dbo')
EXEC('CREATE SCHEMA [dbo]')
create table [dbo].[SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
[ScriptName] nvarchar(255) not null,
[Applied] datetime not null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ DB Operation: Dispose transaction
DB Operation: Begin transaction
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'SchemaVersions' and TABLE_SCHEMA = 'dbo'
DB Operation: Dispose command
DB Operation: Execute non query command: create table [dbo].[SchemaVersions] (
DB Operation: Execute non query command: IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'dbo')
EXEC('CREATE SCHEMA [dbo]')
create table [dbo].[SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
[ScriptName] nvarchar(255) not null,
[Applied] datetime not null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Info: Checking whether journal table exists..
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'SchemaVersions' and TABLE_SCHEMA = 'dbo'
DB Operation: Dispose command
Info: Creating the [dbo].[SchemaVersions] table
DB Operation: Execute non query command: create table [dbo].[SchemaVersions] (
DB Operation: Execute non query command: IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'dbo')
EXEC('CREATE SCHEMA [dbo]')
create table [dbo].[SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
[ScriptName] nvarchar(255) not null,
[Applied] datetime not null
Expand Down
1 change: 1 addition & 0 deletions src/dbup-tests/ApprovalFiles/dbup-core.approved.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ public abstract class TableJournal : DbUp.Engine.IJournal
protected string FqSchemaTableName { get; }
protected System.Func<DbUp.Engine.Output.IUpgradeLog> Log { get; }
protected string SchemaTableSchema { get; }
protected DbUp.Engine.ISqlObjectParser SqlObjectParser { get; }
protected string UnquotedSchemaTableName { get; }
protected abstract string CreateSchemaTableSql(string quotedPrimaryKeyName);
protected bool DoesTableExist(System.Func<System.Data.IDbCommand> dbCommandFactory) { }
Expand Down
3 changes: 2 additions & 1 deletion src/dbup-tests/DatabaseSupportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using DbUp.Firebird;
using DbUp.MySql;
using DbUp.Postgresql;
using DbUp.SqlCe;
#endif

namespace DbUp.Tests
Expand Down Expand Up @@ -101,7 +102,7 @@ ExampleTable DatabaseExampleTable
#if !NETCORE
new ExampleAction("Firebird", Deploy(to => to.FirebirdDatabase(string.Empty), (builder, schema, tableName) => { builder.Configure(c => c.Journal = new FirebirdTableJournal(()=>c.ConnectionManager, ()=>c.Log, tableName)); return builder; })),
new ExampleAction("PostgreSQL", Deploy(to => to.PostgresqlDatabase(string.Empty), (builder, schema, tableName) => { builder.Configure(c => c.Journal = new PostgresqlTableJournal(()=>c.ConnectionManager, ()=>c.Log, schema, tableName)); return builder; })),
new ExampleAction("SqlCe", Deploy(to => to.SqlCeDatabase(string.Empty), (builder, schema, tableName) => { builder.Configure(c => c.Journal = new SqlTableJournal(()=>c.ConnectionManager, ()=>c.Log, schema, tableName)); return builder; })),
new ExampleAction("SqlCe", Deploy(to => to.SqlCeDatabase(string.Empty), (builder, schema, tableName) => { builder.Configure(c => c.Journal = new SqlCeTableJournal(()=>c.ConnectionManager, ()=>c.Log, schema, tableName)); return builder; })),
new ExampleAction("MySql", Deploy(to => to.MySqlDatabase(string.Empty), (builder, schema, tableName) => { builder.Configure(c => c.Journal = new MySqlTableJournal(()=>c.ConnectionManager, ()=>c.Log, schema, tableName)); return builder; }))
#endif
};
Expand Down

0 comments on commit a4f8bd1

Please sign in to comment.