private void CheckResultsOfMigration1() { // assert Versioning table was created DataTable versioningTable = GetTable(_options.VersioningTable); Assert.IsNotNull(versioningTable, string.Format(CultureInfo.CurrentCulture, "The '{0}' table was not created.", _options.VersioningTableName)); Assert.AreEqual(3, versioningTable.Columns.Count); Assert.AreEqual(BootstrapMigration.TimestampColumnName, versioningTable.Columns[0].ColumnName); Assert.AreEqual(BootstrapMigration.ModuleColumnName, versioningTable.Columns[1].ColumnName); Assert.AreEqual(BootstrapMigration.TagColumnName, versioningTable.Columns[2].ColumnName); // assert Customer table was created Migration1 migration1 = new Migration1(); DataTable customerTable = GetTable(migration1.Tables[0].FullName); Assert.IsNotNull(customerTable, string.Format(CultureInfo.CurrentCulture, "The '{0}' table was not created.", migration1.Tables[0].FullName)); Assert.AreEqual(1, customerTable.Columns.Count); Assert.AreEqual(migration1.Tables[0].Columns[0], customerTable.Columns[0].ColumnName); // assert Versioning table has necessary entries Assert.AreEqual(1, versioningTable.Rows.Count, "The versioning table should have one entry."); Assert.AreEqual(Timestamps[0], versioningTable.Rows[0][0], "The timestamp of Migration1 is wrong."); Assert.AreEqual(MigrationExportAttribute.DefaultModuleName, versioningTable.Rows[0][1], "The module of Migration1 is wrong."); Assert.AreEqual(DBNull.Value, versioningTable.Rows[0][2], "The tag of Migration1 is wrong."); }
public virtual void TestMigrationWithinTransactionScopeRollback() { _options.VersioningTableName = "My Versioning Table"; // test overriding the default versioning table name if (ProviderSupportsSchemas) { _options.VersioningTableSchema = CustomVersioningTableSchema; // test installing versioning table in a different schema } Migrator migrator = CreateMigrator(); // verify if the migrations batch is populated correctly IMigrationBatch batch = migrator.FetchMigrationsTo(typeof(Migration1).Assembly, Timestamps[0]); Assert.AreEqual(1, batch.Steps.Count); CollectionAssert.AreEqual(new[] { Timestamps[0] }, batch.Steps[0].Migrations.Select(m => m.Timestamp).ToArray()); Assert.AreEqual(MigrationExportAttribute.DefaultModuleName, batch.Steps[0].ModuleName); Assert.IsNull(batch.Steps[0].Migrations.Single().Tag); Assert.AreEqual(MigrationDirection.Up, batch.Steps[0].Direction); using (var transaction = new TransactionScope()) { // use MigrateTo to execute the actual migrations to test that method, too migrator.MigrateTo(typeof(Migration1).Assembly, Timestamps[0]); transaction.Dispose(); } // assert Versioning table was not created DataTable versioningTable = GetTable(_options.VersioningTable); Assert.IsNull(versioningTable, string.Format(CultureInfo.CurrentCulture, "The '{0}' table was created.", _options.VersioningTableName)); // assert Customer table was not created var migration1 = new Migration1(); DataTable customerTable = GetTable(migration1.Tables[0].FullName); Assert.IsNull(customerTable, string.Format(CultureInfo.CurrentCulture, "The '{0}' table was created.", migration1.Tables[0].FullName)); }
private void CheckResultsOfMigration1() { // assert Versioning table was created DataTable versioningTable = GetTable(_options.VersioningTableName); Assert.IsNotNull(versioningTable, string.Format(CultureInfo.CurrentCulture, "The '{0}' table was not created.", _options.VersioningTableName)); Assert.AreEqual(3, versioningTable.Columns.Count); Assert.AreEqual(BootstrapMigration.TimestampColumnName, versioningTable.Columns[0].ColumnName); Assert.AreEqual(BootstrapMigration.ModuleColumnName, versioningTable.Columns[1].ColumnName); Assert.AreEqual(BootstrapMigration.TagColumnName, versioningTable.Columns[2].ColumnName); // assert Customer table was created Migration1 migration1 = new Migration1(); DataTable customerTable = GetTable(migration1.Tables[0].Name); Assert.IsNotNull(customerTable, string.Format(CultureInfo.CurrentCulture, "The '{0}' table was not created.", migration1.Tables[0].Name)); Assert.AreEqual(1, customerTable.Columns.Count); Assert.AreEqual(migration1.Tables[0].Columns[0], customerTable.Columns[0].ColumnName); // assert Versioning table has necessary entries Assert.AreEqual(1, versioningTable.Rows.Count, "The versioning table is missing entries."); Assert.AreEqual(Timestamps[0], versioningTable.Rows[0][0], "The timestamp of Migration1 is wrong."); Assert.AreEqual(MigrationExportAttribute.DefaultModuleName, versioningTable.Rows[0][1], "The module of Migration1 is wrong."); Assert.AreEqual(DBNull.Value, versioningTable.Rows[0][2], "The tag of Migration1 is wrong."); }
public void TestScriptingAllMigrations() { DirectoryInfo targetDirectory = PrepareScriptingDirectory(); _options.VersioningTableName = "My Versioning Table"; // test overriding the default versioning table name _options.OnlyScriptSqlTo(targetDirectory); Migrator migrator = CreateMigrator(); migrator.MigrateAll(typeof(Migration1).Assembly); // assert that all script files were generated List<FileInfo> scriptFiles = targetDirectory.GetFiles(string.Format(CultureInfo.InvariantCulture, "Migration.*.*.sql")) .OrderBy(f => int.Parse(Regex.Match(f.Name, @"Migration\..*\.(\d+)\.sql").Groups[1].Value, CultureInfo.InvariantCulture)) .ToList(); Assert.AreEqual(Migrations.Count, scriptFiles.Count); Assert.AreEqual("Migration." + MigrationExportAttribute.DefaultModuleName + ".1.sql", scriptFiles[0].Name); Assert.AreEqual("Migration." + Migration2.Module + ".2.sql", scriptFiles[1].Name); // assert Versioning table was *not* created as we are scripting only DataTable versioningTable = GetTable(_options.VersioningTableName); Assert.IsNull(versioningTable, string.Format(CultureInfo.CurrentCulture, "The '{0}' table was created altough ScriptingMode was ScriptOnly.", _options.VersioningTableName)); // assert Customer table was *not* created as we are scripting only var migration1 = new Migration1(); DataTable customerTable = GetTable(migration1.Tables[0].Name); Assert.IsNull(customerTable, string.Format(CultureInfo.CurrentCulture, "The '{0}' table was created altough ScriptingMode was ScriptOnly.", migration1.Tables[0].Name)); // execute generated script files against database and recheck results IProviderMetadata providerMetadata; _options.SupportedProviders.GetProvider(ProviderName, out providerMetadata); var info = new ConnectionInfo(ConnectionString, providerMetadata.InvariantName, providerMetadata.SupportsTransactions, providerMetadata.EnableAnsiQuotesCommand); using (IDbConnection connection = migrator.ConnectionFactory.OpenConnection(info)) { foreach (FileInfo scriptFile in scriptFiles) { Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Reading script '{0}':", scriptFile.FullName)); string[] scriptLines = File.ReadAllLines(scriptFile.FullName); foreach (string line in scriptLines) { Trace.WriteLine(line); } // group all lines between empty lines into one command (some database platforms require DDL operations to // be executed in separated commands) Trace.WriteLine(Environment.NewLine + string.Format(CultureInfo.CurrentCulture, "Executing script '{0}':", scriptFile.FullName)); string commandText = string.Empty; foreach (string line in scriptLines) { if (line.Trim().Length != 0) { commandText += line; } else { ExecuteCommand(commandText, connection); commandText = string.Empty; } } Assert.IsEmpty(commandText, "The script should end with an empty line."); } } VerifyResultsOfAllMigrations(); // delete script files targetDirectory.Delete(true); }
public void TestScriptingAllMigrations() { DirectoryInfo targetDirectory = PrepareScriptingDirectory(); _options.VersioningTableName = "My Versioning Table"; // test overriding the default versioning table name _options.OnlyScriptSqlTo(targetDirectory); Migrator migrator = CreateMigrator(); migrator.MigrateAll(typeof(Migration1).Assembly); // assert that all script files were generated List <FileInfo> scriptFiles = targetDirectory.GetFiles(string.Format(CultureInfo.InvariantCulture, "Migration.*.*.sql")) .OrderBy(f => int.Parse(Regex.Match(f.Name, @"Migration\..*\.(\d+)\.sql").Groups[1].Value, CultureInfo.InvariantCulture)) .ToList(); Assert.AreEqual(Migrations.Count, scriptFiles.Count); Assert.AreEqual("Migration." + MigrationExportAttribute.DefaultModuleName + ".1.sql", scriptFiles[0].Name); Assert.AreEqual("Migration." + Migration2.Module + ".2.sql", scriptFiles[1].Name); // assert Versioning table was *not* created as we are scripting only DataTable versioningTable = GetTable(_options.VersioningTable); Assert.IsNull(versioningTable, string.Format(CultureInfo.CurrentCulture, "The '{0}' table was created altough ScriptingMode was ScriptOnly.", _options.VersioningTableName)); // assert Customer table was *not* created as we are scripting only var migration1 = new Migration1(); DataTable customerTable = GetTable(migration1.Tables[0].FullName); Assert.IsNull(customerTable, string.Format(CultureInfo.CurrentCulture, "The '{0}' table was created altough ScriptingMode was ScriptOnly.", migration1.Tables[0].FullName)); // execute generated script files against database and recheck results IProviderMetadata metadata = IntegrationTestContext.ProviderMetadata; var info = new ConnectionInfo(ConnectionString, metadata.InvariantName, metadata.SupportsTransactions, metadata.EnableAnsiQuotesCommand); using (IDbConnection connection = migrator.Configuration.ConnectionFactory.OpenConnection(info)) { foreach (FileInfo scriptFile in scriptFiles) { Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Reading script '{0}':", scriptFile.FullName)); string[] scriptLines = File.ReadAllLines(scriptFile.FullName); foreach (string line in scriptLines) { Trace.WriteLine(line); } // group all lines between empty lines into one command (some database platforms require DDL operations to // be executed in separated commands) Trace.WriteLine(Environment.NewLine + string.Format(CultureInfo.CurrentCulture, "Executing script '{0}':", scriptFile.FullName)); string commandText = string.Empty; foreach (string line in scriptLines) { if (line.Trim().Length != 0) { commandText += line; } else { ExecuteCommand(commandText, connection); commandText = string.Empty; } } Assert.IsEmpty(commandText, "The script should end with an empty line."); } } VerifyResultsOfAllMigrations(); // delete script files targetDirectory.Delete(true); }