Permalink
Browse files

Added the ability to differentiate between a new column and a column …

…change so that different sql can be generated if necessary. This is needed for default constraints in SQL Server.
  • Loading branch information...
1 parent cb897f2 commit 14c467a3f2e9769cfe223b5a71dddcd3bf30e78c @Andorbal Andorbal committed Jan 1, 2012
View
@@ -24,3 +24,4 @@ src/*.vs10x
src/FluentMigrator.Tests/FluentMigrator.VisualState.xml
examplecommand.txt
fluentmigrator.vssettings
+*.dotsettings
@@ -17,7 +17,7 @@ protected override string FormatDefaultValue(ColumnDefinition column)
var defaultValue = base.FormatDefaultValue(column);
- if (!string.IsNullOrEmpty(defaultValue))
+ if (column.ModificationType == ColumnModificationType.Create && !string.IsNullOrEmpty(defaultValue))
return string.Format("CONSTRAINT DF_{0}_{1} ", column.TableName, column.Name) + defaultValue;
return string.Empty;
@@ -17,8 +17,8 @@
//
#endregion
-using System.Data;
using System.Collections.Generic;
+using System.Data;
using System.Linq;
using FluentMigrator.Builders.Execute;
using FluentMigrator.Model;
@@ -157,7 +157,8 @@ protected virtual IList<FluentMigrator.Model.TableDefinition> ReadTables()
PrimaryKeyName = dr.IsNull("PrimaryKeyName") ? "" : dr["PrimaryKeyName"].ToString(),
Size = int.Parse(dr["Length"].ToString()),
TableName = dr["Table"].ToString(),
- Type = GetDbType(int.Parse(dr["TypeID"].ToString())) //TODO: set this property
+ Type = GetDbType(int.Parse(dr["TypeID"].ToString())), //TODO: set this property
+ ModificationType = ColumnModificationType.Create
});
}
}
@@ -231,7 +231,9 @@ public void CallingWithDefaultValueSetsDefaultValue()
{
const int value = 42;
+ var expressions = new List<IMigrationExpression>();
var contextMock = new Mock<IMigrationContext>();
+ contextMock.Setup(x => x.Expressions).Returns(expressions);
var columnMock = new Mock<ColumnDefinition>();
@@ -245,6 +247,46 @@ public void CallingWithDefaultValueSetsDefaultValue()
}
[Test]
+ public void CallingWithDefaultValueOnNewColumnDoesNotAddDefaultConstraintExpression() {
+ const int value = 42;
+
+ var expressions = new List<IMigrationExpression>();
+ var contextMock = new Mock<IMigrationContext>();
+ contextMock.Setup(x => x.Expressions).Returns(expressions);
+
+ var columnMock = new Mock<ColumnDefinition>();
+ columnMock.Setup(x => x.ModificationType).Returns(ColumnModificationType.Create);
+
+ var expressionMock = new Mock<AlterTableExpression>();
+
+ var builder = new AlterTableExpressionBuilder(expressionMock.Object, contextMock.Object);
+ builder.CurrentColumn = columnMock.Object;
+ builder.WithDefaultValue(42);
+
+ Assert.That(expressions.Count(), Is.EqualTo(0));
+ }
+
+ [Test]
+ public void CallingWithDefaultValueOnAlterColumnAddsDefaultConstraintExpression() {
+ const int value = 42;
+
+ var expressions = new List<IMigrationExpression>();
+ var contextMock = new Mock<IMigrationContext>();
+ contextMock.Setup(x => x.Expressions).Returns(expressions);
+
+ var columnMock = new Mock<ColumnDefinition>();
+ columnMock.Setup(x => x.ModificationType).Returns(ColumnModificationType.Alter);
+
+ var expressionMock = new Mock<AlterTableExpression>();
+
+ var builder = new AlterTableExpressionBuilder(expressionMock.Object, contextMock.Object);
+ builder.CurrentColumn = columnMock.Object;
+ builder.WithDefaultValue(42);
+
+ Assert.That(expressions.Count(), Is.EqualTo(1));
+ }
+
+ [Test]
public void CallingForeignKeySetsIsForeignKeyToTrue()
{
VerifyColumnProperty(c => c.IsForeignKey = true, b => b.ForeignKey());
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
+using System.Data;
using FluentMigrator.Expressions;
using FluentMigrator.Model;
-using System.Data;
namespace FluentMigrator.Tests.Unit.Generators
{
@@ -256,6 +256,7 @@ public static AlterColumnExpression GetAlterColumnExpression()
expression.Column.Type = DbType.String;
expression.Column.Size = 20;
expression.Column.IsNullable = false;
+ expression.Column.ModificationType = ColumnModificationType.Alter;
return expression;
}
@@ -78,6 +78,17 @@ public override void CanAlterColumn()
}
[Test]
+ public void CanAlterColumnWithDefaultValue() {
+ //TODO: This will fail if there are any keys attached
+ var expression = GeneratorTestHelper.GetAlterColumnExpression();
+ expression.Column.DefaultValue = "Foo";
+
+ var sql = _generator.Generate(expression);
+
+ sql.ShouldBe("ALTER TABLE [TestTable1] ALTER COLUMN [TestColumn1] NVARCHAR(20) NOT NULL");
+ }
+
+ [Test]
public override void CanCreateForeignKey()
{
var expression = GeneratorTestHelper.GetCreateForeignKeyExpression();
@@ -16,11 +16,11 @@
//
#endregion
+using System;
using System.Collections.Generic;
using FluentMigrator.Expressions;
using FluentMigrator.Infrastructure;
using FluentMigrator.Model;
-using System;
namespace FluentMigrator.Builders.Alter.Table
{
@@ -58,7 +58,7 @@ public void ToSchema(string schemaName)
public IAlterTableColumnAsTypeSyntax AddColumn(string name)
{
- var column = new ColumnDefinition { Name = name };
+ var column = new ColumnDefinition { Name = name, ModificationType = ColumnModificationType.Create };
var createColumn = new CreateColumnExpression
{
Column = column,
@@ -74,7 +74,7 @@ public IAlterTableColumnAsTypeSyntax AddColumn(string name)
public IAlterTableColumnAsTypeSyntax AlterColumn(string name)
{
- var column = new ColumnDefinition { Name = name };
+ var column = new ColumnDefinition { Name = name, ModificationType = ColumnModificationType.Alter };
var alterColumn = new AlterColumnExpression
{
Column = column,
@@ -90,6 +90,19 @@ public IAlterTableColumnAsTypeSyntax AlterColumn(string name)
public IAlterTableColumnOptionOrAddColumnOrAlterColumnSyntax WithDefaultValue(object value)
{
+ if (CurrentColumn.ModificationType == ColumnModificationType.Alter) {
+ // TODO: This is code duplication from the AlterColumnExpressionBuilder
+ // we need to do a drop constraint and then add constraint to change the defualt value
+ var dc = new AlterDefaultConstraintExpression {
+ TableName = Expression.TableName,
+ SchemaName = Expression.SchemaName,
+ ColumnName = CurrentColumn.Name,
+ DefaultValue = value
+ };
+
+ _context.Expressions.Add(dc);
+ }
+
CurrentColumn.DefaultValue = value;
return this;
}
@@ -16,12 +16,11 @@
//
#endregion
+using System;
using System.Collections.Generic;
-using System.Data;
using FluentMigrator.Expressions;
using FluentMigrator.Infrastructure;
using FluentMigrator.Model;
-using System;
namespace FluentMigrator.Builders.Create.Table
{
@@ -47,7 +46,7 @@ public ICreateTableWithColumnSyntax InSchema(string schemaName)
public ICreateTableColumnAsTypeSyntax WithColumn(string name)
{
- var column = new ColumnDefinition { Name = name, TableName = Expression.TableName };
+ var column = new ColumnDefinition { Name = name, TableName = Expression.TableName, ModificationType = ColumnModificationType.Create };
Expression.Columns.Add(column);
CurrentColumn = column;
return this;
@@ -31,7 +31,7 @@ public class AlterColumnExpression : MigrationExpressionBase
public AlterColumnExpression()
{
- Column = new ColumnDefinition();
+ Column = new ColumnDefinition() { ModificationType = ColumnModificationType.Alter };
}
public override void CollectValidationErrors(ICollection<string> errors)
@@ -31,7 +31,7 @@ public class CreateColumnExpression : MigrationExpressionBase
public CreateColumnExpression()
{
- Column = new ColumnDefinition();
+ Column = new ColumnDefinition() { ModificationType = ColumnModificationType.Alter};
}
public override void ApplyConventions(IMigrationConventions conventions)
@@ -44,6 +44,7 @@ public ColumnDefinition()
public virtual bool IsNullable { get; set; }
public virtual bool IsUnique { get; set; }
public virtual string TableName { get; set; }
+ public virtual ColumnModificationType ModificationType { get; set; }
public void ApplyConventions(IMigrationConventions conventions)
{
@@ -69,4 +70,9 @@ public class UndefinedDefaultValue
{
}
}
+
+ public enum ColumnModificationType {
+ Create,
+ Alter
+ }
}

0 comments on commit 14c467a

Please sign in to comment.