From 8f66a5cf39881ee427e7a99ebed6116ea2fe1b55 Mon Sep 17 00:00:00 2001 From: Dan Bradley Date: Wed, 20 Apr 2016 15:11:24 +0100 Subject: [PATCH 1/2] 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 2/2] 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(")");