public bool RunCompare(DatabaseSchema schema1, DatabaseSchema schema2) { var comparer = new CompareSchemas(schema1, schema2); string txt; try { txt = comparer.Execute(); } catch (Exception exception) { Message = @"An error occurred while creating the script.\n" + exception.Message; return false; } if (string.IsNullOrEmpty(txt)) { Message = "No differences found"; } else { using (var scriptForm = new ScriptForm(txt)) { scriptForm.ShowDialog(); } } return true; }
public void WhenSecondSchemaIsEmptyThenEverythingIsDropped() { //arrange var schema1 = TestHelper.GetNorthwindReader().ReadAll(); var schema2 = new DatabaseSchema(null, null); //act var comparison = new CompareSchemas(schema1, schema2); var script = comparison.Execute(); //assert Assert.AreNotEqual(string.Empty, script); if (schema1.Tables.Any()) { Assert.IsTrue(script.Contains("DROP TABLE")); } if (schema1.Views.Any()) { Assert.IsTrue(script.Contains("DROP VIEW")); } if (schema1.StoredProcedures.Any()) { Assert.IsTrue(script.Contains("DROP PROCEDURE")); } }
public void GivenAlteredColumn() { //arrange DatabaseSchema schema1 = CreateSchema(); var productsTable = CreateProductsTable(); schema1.Tables.Add(productsTable); var nameColumn = new DatabaseColumn { Name = "Name", DbDataType = "NVARCHAR", Length = 10, Nullable = false }; productsTable.Columns.Add(nameColumn); DatabaseSchema schema2 = CreateSchema(); var productsTable2 = CreateProductsTable(); var nameColumn2 = new DatabaseColumn { Name = "Name", DbDataType = "NVARCHAR", Length = 20, Nullable = true }; productsTable2.Columns.Add(nameColumn2); schema2.Tables.Add(productsTable2); //act var comparison = new CompareSchemas(schema1, schema2); var result = comparison.ExecuteResult(); //assert var alterColumn = result.FirstOrDefault(x => x.ResultType == ResultType.Change && x.SchemaObjectType == SchemaObjectType.Column && x.TableName == "Products" && x.Name == "Name"); Assert.IsNotNull(alterColumn); }
public void GivenIdenticalSchemas() { //arrange DatabaseSchema schema1 = CreateSchema(); DatabaseSchema schema2 = CreateSchema(); //act var comparison = new CompareSchemas(schema1, schema2); var result = comparison.ExecuteResult(); //assert Assert.AreEqual(0, result.Count); }
public void GivenIdenticalSchemas() { //arrange DatabaseSchema schema1 = CreateSchema(); DatabaseSchema schema2 = CreateSchema(); //act var comparison = new CompareSchemas(schema1, schema2); var script = comparison.Execute(); //assert Assert.AreEqual(string.Empty, script); }
public void GivenDroppedTable() { //arrange DatabaseSchema schema1 = CreateSchema(); schema1.Tables.Add(CreateProductsTable()); DatabaseSchema schema2 = CreateSchema(); //act var comparison = new CompareSchemas(schema1, schema2); var script = comparison.Execute(); //assert Assert.IsTrue(script.Contains("DROP TABLE [Products]")); }
public void GivenDroppedTable() { //arrange DatabaseSchema schema1 = CreateSchema(); schema1.Tables.Add(CreateProductsTable()); DatabaseSchema schema2 = CreateSchema(); //act var comparison = new CompareSchemas(schema1, schema2); var result = comparison.ExecuteResult(); //assert var dropProducts = result.FirstOrDefault(x => x.ResultType == ResultType.Delete && x.SchemaObjectType == SchemaObjectType.Table && x.Name == "Products"); Assert.IsNotNull(dropProducts); }
public void GivenAddedColumn() { //arrange DatabaseSchema schema1 = CreateSchema(); var productsTable = CreateProductsTable(); schema1.Tables.Add(productsTable); DatabaseSchema schema2 = CreateSchema(); var productsTable2 = CreateProductsTable(); schema2.Tables.Add(productsTable2); var nameColumn = new DatabaseColumn { Name = "Name", DbDataType = "NVARCHAR", Length = 10, Nullable = false }; productsTable2.Columns.Add(nameColumn); //act var comparison = new CompareSchemas(schema1, schema2); var script = comparison.Execute(); //assert Assert.IsTrue(script.Contains("ADD [Name]")); }
public void Given2DatabasesThenDifferencesFound() { //arrange CreateDatabases(); //act var dsr1 = new DatabaseReader("Data Source=" + Db1Sqlite + ";Version=3;", SqlType.SQLite); var schema1 = dsr1.ReadAll(); var dsr2 = new DatabaseReader("Data Source=" + Db2Sqlite + ";Version=3;", SqlType.SQLite); var schema2 = dsr2.ReadAll(); var comparison = new CompareSchemas(schema1, schema2); var result = comparison.Execute(); //assert //script should look like this... //-- ADDED TABLE Products COLUMN Cost //ALTER TABLE [Products] ADD [Cost] NUMERIC DEFAULT 0 NOT NULL; //-- Products from [Name] TEXT NOT NULL to [Name] TEXT NULL //-- TODO: change manually (no ALTER COLUMN) Assert.IsTrue(result.Contains("ALTER TABLE [Products] ADD [Cost]")); Assert.IsTrue(result.Contains("-- TODO: change manually (no ALTER COLUMN)")); }
public void GivenDroppedUniqueConstraint() { //arrange DatabaseSchema schema1 = CreateSchema(); DatabaseConstraint constraint = GetUniqueConstraint(); schema1.Tables[0].AddConstraint(constraint); DatabaseSchema schema2 = CreateSchema(); //act var comparison = new CompareSchemas(schema1, schema2); var result = comparison.ExecuteResult(); //assert var dropConstraint = result.FirstOrDefault(x => x.ResultType == ResultType.Delete && x.SchemaObjectType == SchemaObjectType.Constraint && x.TableName == "Orders" && x.Name == "UK_NAME"); Assert.IsNotNull(dropConstraint); }
public void GivenUniqueConstraintWithChangedColumn() { //arrange DatabaseSchema schema1 = CreateSchema(); DatabaseConstraint constraint = GetUniqueConstraint(); schema1.Tables[0].AddConstraint(constraint); DatabaseSchema schema2 = CreateSchema(); DatabaseConstraint constraint2 = GetUniqueConstraint(); constraint2.Columns[0] = "Desc"; schema2.Tables[0].AddConstraint(constraint2); //act var comparison = new CompareSchemas(schema1, schema2); var result = comparison.ExecuteResult(); //assert var changeConstraint = result.FirstOrDefault(x => x.ResultType == ResultType.Change && x.SchemaObjectType == SchemaObjectType.Constraint && x.TableName == "Orders" && x.Name == "UK_NAME"); Assert.IsNotNull(changeConstraint); }
public void GivenSameUniqueConstraint() { //arrange DatabaseSchema schema1 = CreateSchema(); DatabaseConstraint constraint = GetUniqueConstraint(); schema1.Tables[0].AddConstraint(constraint); DatabaseSchema schema2 = CreateSchema(); DatabaseConstraint constraint2 = GetUniqueConstraint(); schema2.Tables[0].AddConstraint(constraint2); //act var comparison = new CompareSchemas(schema1, schema2); var result = comparison.ExecuteResult(); //assert Assert.AreEqual(0, result.Count); }
public void GivenDroppedUniqueConstraint() { //arrange DatabaseSchema schema1 = CreateSchema(); DatabaseConstraint constraint = GetUniqueConstraint(); schema1.Tables[0].AddConstraint(constraint); DatabaseSchema schema2 = CreateSchema(); //act var comparison = new CompareSchemas(schema1, schema2); var script = comparison.Execute(); //assert Assert.IsTrue(script.Contains("DROP CONSTRAINT [UK_NAME]"), script); }
public void GivenUniqueConstraintWithChangedColumn() { //arrange DatabaseSchema schema1 = CreateSchema(); DatabaseConstraint constraint = GetUniqueConstraint(); schema1.Tables[0].AddConstraint(constraint); DatabaseSchema schema2 = CreateSchema(); DatabaseConstraint constraint2 = GetUniqueConstraint(); constraint2.Columns[0] = "Desc"; schema2.Tables[0].AddConstraint(constraint2); //act var comparison = new CompareSchemas(schema1, schema2); var script = comparison.Execute(); //assert Assert.IsTrue(script.Contains("DROP CONSTRAINT [UK_NAME]"), script); Assert.IsTrue(script.Contains("ADD CONSTRAINT [UK_NAME] UNIQUE"), script); }