Skip to content

Commit

Permalink
Changed GetExecutedScripts so that it does not created the table if i…
Browse files Browse the repository at this point in the history
…t doesn't exist. This means the table is only created when the first journal record is actually written. This is the behaviour in 3.x. Fixes #316
  • Loading branch information
droyad committed Feb 26, 2018
1 parent 0f183a4 commit 1fd851e
Show file tree
Hide file tree
Showing 26 changed files with 174 additions and 176 deletions.
36 changes: 20 additions & 16 deletions src/dbup-core/Support/TableJournal.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using DbUp.Engine;
using DbUp.Engine.Output;
using DbUp.Engine.Transactions;
Expand Down Expand Up @@ -56,29 +57,32 @@ public abstract class TableJournal : IJournal

protected Func<IUpgradeLog> Log { get; private set; }

/// <summary>
/// Recalls the version number of the database.
/// </summary>
/// <returns>All executed scripts.</returns>
public string[] GetExecutedScripts()
{
EnsureTableIsLatestVersion();
Log().WriteInformation("Fetching list of already executed scripts.");

var scripts = new List<string>();
ConnectionManager().ExecuteCommandsWithManagedConnection(dbCommandFactory =>
if (journalExists || DoesTableExist())
{
using (var command = GetJournalEntriesCommand(dbCommandFactory))
Log().WriteInformation("Fetching list of already executed scripts.");

var scripts = new List<string>();
ConnectionManager().ExecuteCommandsWithManagedConnection(dbCommandFactory =>
{
using (var reader = command.ExecuteReader())
using (var command = GetJournalEntriesCommand(dbCommandFactory))
{
while (reader.Read())
scripts.Add((string)reader[0]);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
scripts.Add((string) reader[0]);
}
}
}
});
});

return scripts.ToArray();
return scripts.ToArray();
}
else
{
Log().WriteInformation("Journal table does not exist");
return new string[0];
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Info: Beginning database upgrade
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: select 1 from RDB$RELATIONS where RDB$SYSTEM_FLAG = 0 and RDB$RELATION_NAME = 'schemaversions'
DB Operation: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: select 1 from RDB$RELATIONS where RDB$SYSTEM_FLAG = 0 and RDB$RELATION_NAME = 'schemaversions'
DB Operation: Dispose command
Info: Creating the "schemaversions" table
DB Operation: Execute non query command: CREATE TABLE "schemaversions"
(
Expand All @@ -21,12 +28,6 @@ DB Operation: Execute non query command: CREATE TRIGGER BI_schemaversionsID FOR
END;
DB Operation: Dispose command
Info: The BI_schemaversionsID trigger has been created
Info: Fetching list of already executed scripts.
DB Operation: Execute reader command: select ScriptName from "schemaversions" order by ScriptName
DB Operation: Dispose command
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ 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: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'schemaversions'
DB Operation: Dispose command
Info: Creating the `schemaversions` table
DB Operation: Execute non query command: CREATE TABLE `schemaversions`
(
Expand All @@ -13,12 +20,6 @@ DB Operation: Execute non query command: CREATE TABLE `schemaversions`
);
DB Operation: Dispose command
Info: The `schemaversions` table has been created
Info: Fetching list of already executed scripts.
DB Operation: Execute reader command: select scriptname from `schemaversions` order by scriptname
DB Operation: Dispose command
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ 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: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'schemaversions'
DB Operation: Dispose command
Info: Creating the "schemaversions" table
DB Operation: Execute non query command: CREATE TABLE "schemaversions"
(
Expand All @@ -13,12 +20,6 @@ DB Operation: Execute non query command: CREATE TABLE "schemaversions"
)
DB Operation: Dispose command
Info: The "schemaversions" table has been created
Info: Fetching list of already executed scripts.
DB Operation: Execute reader command: select ScriptName from "schemaversions" order by ScriptName
DB Operation: Dispose command
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Info: Beginning database upgrade
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: SELECT count(name) FROM sqlite_master WHERE type = 'table' AND name = 'SchemaVersions'
DB Operation: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: SELECT count(name) FROM sqlite_master WHERE type = 'table' AND name = 'SchemaVersions'
DB Operation: Dispose command
Info: Creating the [SchemaVersions] table
DB Operation: Execute non query command: CREATE TABLE [SchemaVersions] (
SchemaVersionID INTEGER CONSTRAINT [PK_SchemaVersions_Id] PRIMARY KEY AUTOINCREMENT NOT NULL,
Expand All @@ -11,12 +18,6 @@ DB Operation: Execute non query command: CREATE TABLE [SchemaVersions] (
)
DB Operation: Dispose command
Info: The [SchemaVersions] table has been created
Info: Fetching list of already executed scripts.
DB Operation: Execute reader command: select [ScriptName] from [SchemaVersions] order by [ScriptName]
DB Operation: Dispose command
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Info: Beginning database upgrade
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: SELECT count(*) FROM information_schema.tables WHERE table_name='SchemaVersions'
DB Operation: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: SELECT count(*) FROM information_schema.tables WHERE table_name='SchemaVersions'
DB Operation: Dispose command
Info: Creating the [SchemaVersions] table
DB Operation: Execute non query command: create table [SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
Expand All @@ -11,12 +18,6 @@ DB Operation: Execute non query command: create table [SchemaVersions] (
)
DB Operation: Dispose command
Info: The [SchemaVersions] table has been created
Info: Fetching list of already executed scripts.
DB Operation: Execute reader command: select [ScriptName] from [SchemaVersions] order by [ScriptName]
DB Operation: Dispose command
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ 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: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'SchemaVersions'
DB Operation: Dispose command
Info: Creating the [SchemaVersions] table
DB Operation: Execute non query command: create table [SchemaVersions] (
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
Expand All @@ -11,12 +18,6 @@ DB Operation: Execute non query command: create table [SchemaVersions] (
)
DB Operation: Dispose command
Info: The [SchemaVersions] table has been created
Info: Fetching list of already executed scripts.
DB Operation: Execute reader command: select [ScriptName] from [SchemaVersions] order by [ScriptName]
DB Operation: Dispose command
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Info: Beginning database upgrade
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: select 1 from RDB$RELATIONS where RDB$SYSTEM_FLAG = 0 and RDB$RELATION_NAME = 'TestSchemaVersions'
DB Operation: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: select 1 from RDB$RELATIONS where RDB$SYSTEM_FLAG = 0 and RDB$RELATION_NAME = 'TestSchemaVersions'
DB Operation: Dispose command
Info: Creating the "TestSchemaVersions" table
DB Operation: Execute non query command: CREATE TABLE "TestSchemaVersions"
(
Expand All @@ -21,12 +28,6 @@ DB Operation: Execute non query command: CREATE TRIGGER BI_TestSchemaVersionsID
END;
DB Operation: Dispose command
Info: The BI_TestSchemaVersionsID trigger has been created
Info: Fetching list of already executed scripts.
DB Operation: Execute reader command: select ScriptName from "TestSchemaVersions" order by ScriptName
DB Operation: Dispose command
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ 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: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
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`
(
Expand All @@ -13,12 +20,6 @@ DB Operation: Execute non query command: CREATE TABLE `test`.`TestSchemaVersions
);
DB Operation: Dispose command
Info: The `test`.`TestSchemaVersions` table has been created
Info: Fetching list of already executed scripts.
DB Operation: Execute reader command: select scriptname from `test`.`TestSchemaVersions` order by scriptname
DB Operation: Dispose command
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ 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: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
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"
(
Expand All @@ -13,12 +20,6 @@ DB Operation: Execute non query command: CREATE TABLE "test"."TestSchemaVersions
)
DB Operation: Dispose command
Info: The "test"."TestSchemaVersions" table has been created
Info: Fetching list of already executed scripts.
DB Operation: Execute reader command: select ScriptName from "test"."TestSchemaVersions" order by ScriptName
DB Operation: Dispose command
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Info: Beginning database upgrade
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: SELECT count(name) FROM sqlite_master WHERE type = 'table' AND name = 'TestSchemaVersions'
DB Operation: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: SELECT count(name) FROM sqlite_master WHERE type = 'table' AND name = 'TestSchemaVersions'
DB Operation: Dispose command
Info: Creating the [TestSchemaVersions] table
DB Operation: Execute non query command: CREATE TABLE [TestSchemaVersions] (
SchemaVersionID INTEGER CONSTRAINT [PK_TestSchemaVersions_Id] PRIMARY KEY AUTOINCREMENT NOT NULL,
Expand All @@ -11,12 +18,6 @@ DB Operation: Execute non query command: CREATE TABLE [TestSchemaVersions] (
)
DB Operation: Dispose command
Info: The [TestSchemaVersions] table has been created
Info: Fetching list of already executed scripts.
DB Operation: Execute reader command: select [ScriptName] from [TestSchemaVersions] order by [ScriptName]
DB Operation: Dispose command
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ 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: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
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] (
[Id] int identity(1,1) not null constraint [PK_TestSchemaVersions_Id] primary key,
Expand All @@ -11,12 +18,6 @@ DB Operation: Execute non query command: create table [test].[TestSchemaVersions
)
DB Operation: Dispose command
Info: The [test].[TestSchemaVersions] table has been created
Info: Fetching list of already executed scripts.
DB Operation: Execute reader command: select [ScriptName] from [test].[TestSchemaVersions] order by [ScriptName]
DB Operation: Dispose command
Info: Executing Database Server script 'Script0001.sql'
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Expand Down

0 comments on commit 1fd851e

Please sign in to comment.