Skip to content

Commit

Permalink
Merge pull request #89 from danbrad/FixOrderCKConstsByName
Browse files Browse the repository at this point in the history
Constraint ordering not consistent in script file
  • Loading branch information
sethreno committed Apr 21, 2016
2 parents ee6eb18 + 46a6e55 commit c41b81e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion model/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(")");
Expand Down
20 changes: 18 additions & 2 deletions test/DatabaseTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,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 categoryType = new Table("dbo", "CategoryType");
Expand Down Expand Up @@ -505,8 +506,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"));
}
Expand Down

0 comments on commit c41b81e

Please sign in to comment.