Skip to content

Commit

Permalink
Added collation to columns
Browse files Browse the repository at this point in the history
  • Loading branch information
marcio-santos-zocdoc committed Aug 31, 2016
1 parent 15d8a97 commit 851b9ac
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 12 deletions.
25 changes: 18 additions & 7 deletions model/Models/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,22 @@ public string IdentityText {
}
}

public string RowGuidColText {
get { return IsRowGuidCol ? "ROWGUIDCOL" : string.Empty; }
}

public ColumnDiff Compare(Column c) {
public string RowGuidColText {
get { return IsRowGuidCol ? "ROWGUIDCOL" : string.Empty; }
}

public string Collation { get; set; }

public string CollationText {
get {
if (string.IsNullOrEmpty(Collation)) {
return string.Empty;
}
return "COLLATE " + Collation;
}
}

public ColumnDiff Compare(Column c) {
return new ColumnDiff(this, c);
}

Expand Down Expand Up @@ -102,8 +113,8 @@ private string ScriptBase(bool includeDefaultConstraint) {
var lengthString = Length.ToString();
if (lengthString == "-1") lengthString = "max";

return string.Format("[{0}] [{1}]({2}) {3} {4}", Name, Type, lengthString, IsNullableText,
includeDefaultConstraint ? DefaultText : string.Empty);
return string.Format("[{0}] [{1}]({2}) {3} {4} {5}", Name, Type, lengthString, CollationText, IsNullableText,
includeDefaultConstraint ? DefaultText : string.Empty );
case "decimal":
case "numeric":

Expand Down
7 changes: 5 additions & 2 deletions model/Models/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ private void LoadColumns(SqlCommand cm) {
t.TABLE_SCHEMA,
c.TABLE_NAME,
c.COLUMN_NAME,
c.COLLATION_NAME,
c.DATA_TYPE,
c.ORDINAL_POSITION,
c.IS_NULLABLE,
Expand All @@ -769,6 +770,7 @@ inner join INFORMATION_SCHEMA.TABLES t
s.name as TABLE_SCHEMA,
tt.name as TABLE_NAME,
c.name as COLUMN_NAME,
c.collation_name as COLLATION_NAME,
t.name as DATA_TYPE,
c.column_id as ORDINAL_POSITION,
CASE WHEN c.is_nullable = 1 THEN 'YES' ELSE 'NO' END as IS_NULLABLE,
Expand Down Expand Up @@ -805,8 +807,9 @@ private static void LoadColumnsBase(IDataReader dr, List<Table> tables) {
Type = (string) dr["DATA_TYPE"],
IsNullable = (string) dr["IS_NULLABLE"] == "YES",
Position = (int) dr["ORDINAL_POSITION"],
IsRowGuidCol = (string) dr["IS_ROW_GUID_COL"] == "YES"
};
IsRowGuidCol = (string) dr["IS_ROW_GUID_COL"] == "YES",
Collation = dr["COLLATION_NAME"] == DBNull.Value ? string.Empty : dr["COLLATION_NAME"].ToString()
};

switch (c.Type) {
case "binary":
Expand Down
84 changes: 81 additions & 3 deletions test/DatabaseTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,46 @@ public void TestViewIndexes() {
Assert.That(result, Is.StringContaining("CREATE UNIQUE CLUSTERED INDEX [MyIndex] ON [dbo].[MyView] ([Id], [Name])"));
}

[Test]
[Test]
public void ScriptCreate_ShouldAddDefaultCollation()
{
var scriptToExecute = @"CREATE TABLE MyTable (Name nvarchar(250))";

var db = CreateTestDbWithScript(scriptToExecute);
var result = db.ScriptCreate();
TestHelper.DropDb("TEST");

Assert.That(result, Is.StringContaining("[Name] [nvarchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL "));
}

[Test]
public void ScriptCreate_ShouldKeepCollation()
{
var scriptToExecute = @"CREATE TABLE MyTable (Name nvarchar(250) COLLATE SQL_Latin1_General_CP1_CS_AS)";

var db = CreateTestDbWithScript(scriptToExecute);
var result = db.ScriptCreate();
TestHelper.DropDb("TEST");

Assert.That(result, Is.StringContaining("[Name] [nvarchar](250) COLLATE SQL_Latin1_General_CP1_CS_AS NULL "));
}

private static Database CreateTestDbWithScript(string scriptToExecute)
{
TestHelper.DropDb("TEST");
TestHelper.ExecSql("CREATE DATABASE TEST", "");

TestHelper.ExecSql(scriptToExecute, "TEST");

var db = new Database("TEST")
{
Connection = TestHelper.GetConnString("TEST")
};
db.Load();
return db;
}

[Test]
[Ignore("test won't work without license key for sqldbdiff")]
public void TestDiffScript() {
TestHelper.DropDb("TEST_SOURCE");
Expand Down Expand Up @@ -241,7 +280,7 @@ public void TestScript() {
public void TestScriptTableType() {
var setupSQL1 = @"
CREATE TYPE [dbo].[MyTableType] AS TABLE(
[ID] [nvarchar](250) NULL,
[ID] [nvarchar](250) COLLATE SQL_Latin1_General_CP1_CS_AS NULL,
[Value] [numeric](5, 1) NULL,
[LongNVarchar] [nvarchar](max) NULL
)
Expand Down Expand Up @@ -271,8 +310,47 @@ [LongNVarchar] [nvarchar](max) NULL

}

[Test]
public void ScriptToDir_ShouldAddCollation_ToVarCharTypeColumns()
{
var setupSQL1 = @"
CREATE TYPE [dbo].[MyTableType] AS TABLE(
[ID] [nvarchar](250) COLLATE SQL_Latin1_General_CP1_CS_AS NULL,
[Value] [numeric](5, 1) NULL,
[LongNVarchar] [nvarchar](max) NULL
)
";

[Test]
var db = new Database("TestScriptTableType");

db.Connection = ConfigHelper.TestDB.Replace("database=TESTDB", "database=" + db.Name);

db.ExecCreate(true);

DBHelper.ExecSql(db.Connection, setupSQL1);

db.Dir = db.Name;
db.Load();

db.ScriptToDir();

var scriptedType = File.ReadAllText(db.Name + "\\table_types\\TYPE_MyTableType.sql");
var expectedScriptedType =
@"CREATE TYPE [dbo].[MyTableType] AS TABLE (
[ID] [nvarchar](250) COLLATE SQL_Latin1_General_CP1_CS_AS NULL ,
[Value] [numeric](5,1) NULL ,
[LongNVarchar] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
)
GO
";

Assert.AreEqual(expectedScriptedType, scriptedType);
}


[Test]
public void TestScriptTableTypePrimaryKey() {
var setupSQL1 = @"
CREATE TYPE [dbo].[MyTableType] AS TABLE(
Expand Down

0 comments on commit 851b9ac

Please sign in to comment.