From 164cdee8af2a3ecb541c7b2f1d61b2fa9233a88b Mon Sep 17 00:00:00 2001 From: Dan Bradley Date: Wed, 20 Apr 2016 14:36:26 +0100 Subject: [PATCH 1/6] Test to show that foreign keys are not ordered by name. --- test/DatabaseTester.cs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/DatabaseTester.cs b/test/DatabaseTester.cs index 6165d99c..5a183339 100644 --- a/test/DatabaseTester.cs +++ b/test/DatabaseTester.cs @@ -400,6 +400,7 @@ public void TestScriptToDir() { loc.Columns.Add(new Column("id", "int", false, null) {Position = 1}); loc.Columns.Add(new Column("policyId", "int", false, null) {Position = 2}); loc.Columns.Add(new Column("storage", "bit", false, null) {Position = 3}); + loc.Columns.Add(new Column("category", "int", false, null) { Position = 4 }); loc.AddConstraint(new Constraint("PK_Location", "PRIMARY KEY", "id") { Clustered = true, Unique = true }); loc.Columns.Items[0].Identity = new Identity(1, 1); @@ -408,6 +409,12 @@ public void TestScriptToDir() { formType.Columns.Add(new Column("desc", "varchar", 10, false, null) {Position = 2}); formType.AddConstraint(new Constraint("PK_FormType", "PRIMARY KEY", "code") { Clustered = true, Unique = true }); + + var categoryType = new Table("dbo", "CategoryType"); + categoryType.Columns.Add(new Column("id", "int", false, null) { Position = 1 }); + categoryType.Columns.Add(new Column("Category", "varchar", 10, false, null) { Position = 2 }); + categoryType.AddConstraint(new Constraint("PK_CategoryType", "PRIMARY KEY", "id") { Clustered = true, Unique = true }); + var fk_policy_formType = new ForeignKey("FK_Policy_FormType"); fk_policy_formType.Table = policy; fk_policy_formType.Columns.Add("form"); @@ -424,6 +431,14 @@ public void TestScriptToDir() { fk_location_policy.OnUpdate = "NO ACTION"; fk_location_policy.OnDelete = "CASCADE"; + var fk_location_category = new ForeignKey("FK_Location_category"); + fk_location_category.Table = loc; + fk_location_category.Columns.Add("category"); + fk_location_category.RefTable = categoryType; + fk_location_category.RefColumns.Add("id"); + fk_location_category.OnUpdate = "NO ACTION"; + fk_location_category.OnDelete = "CASCADE"; + var tt_codedesc = new Table("dbo", "CodeDesc"); tt_codedesc.IsType = true; tt_codedesc.Columns.Add(new Column("code", "tinyint", false, null) { Position = 1 }); @@ -433,10 +448,12 @@ public void TestScriptToDir() { var db = new Database("ScriptToDirTest"); db.Tables.Add(policy); db.Tables.Add(formType); + db.Tables.Add(categoryType); db.Tables.Add(loc); db.TableTypes.Add(tt_codedesc); db.ForeignKeys.Add(fk_policy_formType); db.ForeignKeys.Add(fk_location_policy); + db.ForeignKeys.Add(fk_location_category); db.FindProp("COMPATIBILITY_LEVEL").Value = "110"; db.FindProp("COLLATE").Value = "SQL_Latin1_General_CP1_CI_AS"; db.FindProp("AUTO_CLOSE").Value = "OFF"; @@ -497,6 +514,28 @@ public void TestScriptToDir() { Assert.IsTrue(File.Exists(expected), "File does not exist" + expected); } + + // Test that the foreign keys are ordered in the file + foreach (var t in db.Tables) + { + var fksFile = db.Name + "\\foreign_keys\\" + t.Name + ".sql"; + + if (File.Exists(fksFile)) + { + string script = File.ReadAllText(fksFile); + int fkindex = -1; + + foreach (var fkobject in db.ForeignKeys.Where(x => x.Table == t).OrderBy(x => x.Name)) + { + var thisindex = script.IndexOf(fkobject.ScriptCreate()); + Assert.Greater(thisindex, fkindex, "Foreign keys are not ordered."); + + fkindex = thisindex; + } + } + + } + var copy = new Database("ScriptToDirTestCopy"); copy.Dir = db.Dir; copy.Connection = ConfigHelper.TestDB.Replace("database=TESTDB", "database=" + copy.Name); From 56f0f25f1c18942ddbdf2b4312a44f23291bb2d6 Mon Sep 17 00:00:00 2001 From: Dan Bradley Date: Wed, 20 Apr 2016 14:37:16 +0100 Subject: [PATCH 2/6] Fix to order foreign keys by name to ensure consistency when comparing to a previous script. --- model/Database.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/Database.cs b/model/Database.cs index a991bc39..097000d8 100644 --- a/model/Database.cs +++ b/model/Database.cs @@ -1036,7 +1036,7 @@ public void ScriptToDir(string tableHint = null, Action log WriteSchemaScript(log); WriteScriptDir("tables", Tables.ToArray(), log); WriteScriptDir("table_types", TableTypes.ToArray(), log); - WriteScriptDir("foreign_keys", ForeignKeys.ToArray(), log); + WriteScriptDir("foreign_keys", ForeignKeys.OrderBy(x => x.Name).ToArray(), log); foreach (var routineType in Routines.GroupBy(x => x.RoutineType)) { var dir = routineType.Key.ToString().ToLower() + "s"; WriteScriptDir(dir, routineType.ToArray(), log); From 8f66a5cf39881ee427e7a99ebed6116ea2fe1b55 Mon Sep 17 00:00:00 2001 From: Dan Bradley Date: Wed, 20 Apr 2016 15:11:24 +0100 Subject: [PATCH 3/6] Test to show that constraints are not ordered by name. --- test/DatabaseTester.cs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/test/DatabaseTester.cs b/test/DatabaseTester.cs index 6165d99c..689efd48 100644 --- a/test/DatabaseTester.cs +++ b/test/DatabaseTester.cs @@ -407,6 +407,7 @@ public void TestScriptToDir() { formType.Columns.Add(new Column("code", "tinyint", false, null) {Position = 1}); formType.Columns.Add(new Column("desc", "varchar", 10, false, null) {Position = 2}); formType.AddConstraint(new Constraint("PK_FormType", "PRIMARY KEY", "code") { Clustered = true, Unique = true }); + formType.AddConstraint(Constraint.CreateCheckedConstraint("CK_FormType", false, "([code]<(5))")); var fk_policy_formType = new ForeignKey("FK_Policy_FormType"); fk_policy_formType.Table = policy; @@ -488,8 +489,23 @@ public void TestScriptToDir() { Assert.IsTrue(File.Exists(db.Name + "\\data\\" + t.Name + ".tsv")); } foreach (var t in db.Tables) { - Assert.IsTrue(File.Exists(db.Name + "\\tables\\" + t.Name + ".sql")); - } + var tblFile = db.Name + "\\tables\\" + t.Name + ".sql"; + Assert.IsTrue(File.Exists(tblFile)); + + // Test that the constraints are ordered in the file + string script = File.ReadAllText(tblFile); + int cindex = -1; + + foreach (var ckobject in t.Constraints.OrderBy(x => x.Name)) + { + var thisindex = script.IndexOf(ckobject.ScriptCreate()); + Assert.Greater(thisindex, cindex, "Constraints are not ordered."); + + cindex = thisindex; + } + + + } foreach (var t in db.TableTypes) { Assert.IsTrue(File.Exists(db.Name + "\\table_types\\TYPE_" + t.Name + ".sql")); } From 46a6e550d7918da1d2786a5d530b144b9d9b7efd Mon Sep 17 00:00:00 2001 From: Dan Bradley Date: Wed, 20 Apr 2016 15:29:23 +0100 Subject: [PATCH 4/6] Fix to order constraints by name to ensure consistency when comparing to a previous script. --- model/Table.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/Table.cs b/model/Table.cs index 190cd933..d6b632be 100644 --- a/model/Table.cs +++ b/model/Table.cs @@ -138,7 +138,7 @@ public string ScriptCreate() { IsType ? "AS TABLE " : string.Empty); text.Append(Columns.Script()); if (_Constraints.Count > 0) text.AppendLine(); - foreach (var c in _Constraints.Where(c => c.Type != "INDEX")) { + foreach (var c in _Constraints.OrderBy(x => x.Name).Where(c => c.Type != "INDEX")) { text.AppendLine(" ," + c.ScriptCreate()); } text.AppendLine(")"); From 86556610144f3e1ea227c9ae63ef89ee4f63de16 Mon Sep 17 00:00:00 2001 From: Dan Bradley Date: Thu, 21 Apr 2016 09:49:11 +0100 Subject: [PATCH 5/6] Test to show a nasty little bug with foreign keys with same name where CASCADE is picked up by non-cascading FKs if another FK with same name in different schema is CASCADE --- test/DatabaseTester.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/DatabaseTester.cs b/test/DatabaseTester.cs index 6165d99c..d3aa9eb5 100644 --- a/test/DatabaseTester.cs +++ b/test/DatabaseTester.cs @@ -286,7 +286,7 @@ CONSTRAINT [PK_1a] PRIMARY KEY (a) CREATE TABLE [dbo].[t1b] ( a INT NOT NULL, - CONSTRAINT [FKName] FOREIGN KEY ([a]) REFERENCES [dbo].[t1a] ([a]) + CONSTRAINT [FKName] FOREIGN KEY ([a]) REFERENCES [dbo].[t1a] ([a]) ON UPDATE CASCADE ) CREATE TABLE [s2].[t2a] @@ -298,7 +298,7 @@ CONSTRAINT [PK_2a] PRIMARY KEY (a) CREATE TABLE [s2].[t2b] ( a INT NOT NULL, - CONSTRAINT [FKName] FOREIGN KEY ([a]) REFERENCES [s2].[t2a] ([a]) + CONSTRAINT [FKName] FOREIGN KEY ([a]) REFERENCES [s2].[t2a] ([a]) ON DELETE CASCADE ) "; @@ -321,6 +321,11 @@ CONSTRAINT [FKName] FOREIGN KEY ([a]) REFERENCES [s2].[t2a] ([a]) Assert.AreEqual(db.ForeignKeys[0].Name, db.ForeignKeys[1].Name); Assert.AreNotEqual(db.ForeignKeys[0].Table.Owner, db.ForeignKeys[1].Table.Owner); + Assert.AreEqual("CASCADE", db.FindForeignKey("FKName", "dbo").OnUpdate); + Assert.AreEqual("NO ACTION", db.FindForeignKey("FKName", "s2").OnUpdate); + + Assert.AreEqual("NO ACTION", db.FindForeignKey("FKName", "dbo").OnDelete); + Assert.AreEqual("CASCADE", db.FindForeignKey("FKName", "s2").OnDelete); } public void TestScriptViewInsteadOfTrigger() { From 524690100e7dfd67f0ce12784d72563a450e5dcd Mon Sep 17 00:00:00 2001 From: Dan Bradley Date: Thu, 21 Apr 2016 09:52:51 +0100 Subject: [PATCH 6/6] Fix for foreign keys with same name to ensure that onupdate and ondelete are correct --- model/Database.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/Database.cs b/model/Database.cs index a991bc39..5d76df79 100644 --- a/model/Database.cs +++ b/model/Database.cs @@ -399,7 +399,7 @@ from INFORMATION_SCHEMA.TABLE_CONSTRAINTS DELETE_RULE, fk.is_disabled from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc - inner join sys.foreign_keys fk on rc.CONSTRAINT_NAME = fk.name"; + inner join sys.foreign_keys fk on rc.CONSTRAINT_NAME = fk.name and rc.CONSTRAINT_SCHEMA = OBJECT_SCHEMA_NAME(fk.parent_object_id)"; using (var dr = cm.ExecuteReader()) { while (dr.Read()) { var fk = FindForeignKey((string) dr["CONSTRAINT_NAME"], (string)dr["TABLE_SCHEMA"]);