Skip to content

Commit

Permalink
Fix modification of SQL Server column type
Browse files Browse the repository at this point in the history
- Fix modification of SQL Server column type
- Few refactor around using QuotedName
- Add unit test change_column_type_sql
  • Loading branch information
mysticmind committed Oct 9, 2024
1 parent 16c70c4 commit 13880d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/Weasel.SqlServer.Tests/Tables/TableColumnTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,17 @@ public void add_column_sql()
column.AddColumnSql(table)
.ShouldBe("alter table dbo.people add name1 varchar(100) NOT NULL;");
}

[Fact]
public void change_column_type_sql()
{
var table = new Table("people");
const string columnName = "order";
table.AddColumn<string>(columnName).NotNull();

var updatedColumn = new TableColumn(columnName, "varchar(200)") { AllowNulls = true };

table.ColumnFor("order")!.AlterColumnTypeSql(table, updatedColumn)
.ShouldBe($"alter table dbo.people alter column [{columnName}] varchar(200) NULL;");
}
}
6 changes: 3 additions & 3 deletions src/Weasel.SqlServer/Tables/TableColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public string Declaration()

protected bool Equals(TableColumn other)
{
return string.Equals(Name, other.Name) &&
return string.Equals(QuotedName, other.QuotedName) &&
string.Equals(SqlServerProvider.Instance.ConvertSynonyms(RawType()),
SqlServerProvider.Instance.ConvertSynonyms(other.RawType()));
}
Expand Down Expand Up @@ -116,12 +116,12 @@ public override string ToString()

public virtual string AlterColumnTypeSql(Table table, TableColumn changeActual)
{
return $"alter table {table.Identifier} modify column {Name.PadRight(Name.Length)} type {Type};";
return $"alter table {table.Identifier} alter column {changeActual.ToDeclaration()};";
}

public string DropColumnSql(Table table)
{
return $"alter table {table.Identifier} drop column {Name};";
return $"alter table {table.Identifier} drop column {QuotedName};";
}


Expand Down

0 comments on commit 13880d6

Please sign in to comment.