Escape DEFAULT constraint names #345

Merged
merged 2 commits into from Dec 14, 2012
View
2 src/FluentMigrator.Runner/Generators/SqlServer/SqlServer2000Generator.cs
@@ -138,7 +138,7 @@ public override string Generate(AlterDefaultConstraintExpression expression)
Quoter.QuoteTableName(expression.TableName),
Quoter.QuoteColumnName(expression.ColumnName),
Quoter.QuoteValue(expression.DefaultValue),
- SqlServerColumn.GetDefaultConstraintName(expression.TableName, expression.ColumnName)));
+ Quoter.QuoteConstraintName(SqlServerColumn.GetDefaultConstraintName(expression.TableName, expression.ColumnName))));
return builder.ToString();
}
View
2 src/FluentMigrator.Runner/Generators/SqlServer/SqlServer2005Generator.cs
@@ -273,7 +273,7 @@ public override string Generate(AlterDefaultConstraintExpression expression)
Quoter.QuoteColumnName(expression.ColumnName),
Quoter.QuoteValue(expression.DefaultValue),
Quoter.QuoteSchemaName(expression.SchemaName),
- SqlServerColumn.GetDefaultConstraintName(expression.TableName, expression.ColumnName)));
+ Quoter.QuoteConstraintName(SqlServerColumn.GetDefaultConstraintName(expression.TableName, expression.ColumnName))));
return builder.ToString();
}
View
2 src/FluentMigrator.Runner/Generators/SqlServer/SqlServerColumn.cs
@@ -19,7 +19,7 @@ protected override string FormatDefaultValue(ColumnDefinition column)
var defaultValue = base.FormatDefaultValue(column);
if (column.ModificationType == ColumnModificationType.Create && !string.IsNullOrEmpty(defaultValue))
- return "CONSTRAINT " + GetDefaultConstraintName(column.TableName, column.Name) + " " + defaultValue;
+ return "CONSTRAINT " + Quoter.QuoteConstraintName(GetDefaultConstraintName(column.TableName, column.Name)) + " " + defaultValue;
return string.Empty;
}
View
2 src/FluentMigrator.Tests/Unit/Generators/SqlServer2000/SqlServer2000AlterTableTests.cs
@@ -111,7 +111,7 @@ public void CanAlterDefaultConstraint()
"SET @sql = N'ALTER TABLE [TestTable1] DROP CONSTRAINT ' + @default;\r\n" +
"EXEC sp_executesql @sql;\r\n\r\n" +
"-- create alter table command to create new default constraint as string and run it\r\n" +
- "ALTER TABLE [TestTable1] WITH NOCHECK ADD CONSTRAINT DF_TestTable1_TestColumn1 DEFAULT(1) FOR [TestColumn1];";
+ "ALTER TABLE [TestTable1] WITH NOCHECK ADD CONSTRAINT [DF_TestTable1_TestColumn1] DEFAULT(1) FOR [TestColumn1];";
sql.ShouldBe(expected);
}
View
4 src/FluentMigrator.Tests/Unit/Generators/SqlServer2000/SqlServer2000CreateTableTests.cs
@@ -117,7 +117,7 @@ public override void CanCreateTableWithDefaultValue()
var expression = GeneratorTestHelper.GetCreateTableWithDefaultValue();
var sql = _generator.Generate(expression);
sql.ShouldBe(
- "CREATE TABLE [TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT DF_TestTable1_TestColumn1 DEFAULT 'Default', [TestColumn2] INT NOT NULL CONSTRAINT DF_TestTable1_TestColumn2 DEFAULT 0)");
+ "CREATE TABLE [TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT [DF_TestTable1_TestColumn1] DEFAULT 'Default', [TestColumn2] INT NOT NULL CONSTRAINT [DF_TestTable1_TestColumn2] DEFAULT 0)");
}
[Test]
@@ -128,7 +128,7 @@ public override void CanCreateTableWithDefaultValueExplicitlySetToNull()
expression.Columns[0].TableName = expression.TableName;
var sql = _generator.Generate(expression);
sql.ShouldBe(
- "CREATE TABLE [TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT DF_TestTable1_TestColumn1 DEFAULT NULL, [TestColumn2] INT NOT NULL)");
+ "CREATE TABLE [TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT [DF_TestTable1_TestColumn1] DEFAULT NULL, [TestColumn2] INT NOT NULL)");
}
[Test]
View
4 src/FluentMigrator.Tests/Unit/Generators/SqlServer2005/SqlServer2005AlterTableTests.cs
@@ -83,7 +83,7 @@ public void CanAlterDefaultConstraintWithDefaultSchema()
"SET @sql = N'ALTER TABLE [dbo].[TestTable1] DROP CONSTRAINT ' + @default;\r\n" +
"EXEC sp_executesql @sql;\r\n\r\n" +
"-- create alter table command to create new default constraint as string and run it\r\n" +
- "ALTER TABLE [dbo].[TestTable1] WITH NOCHECK ADD CONSTRAINT DF_TestTable1_TestColumn1 DEFAULT(1) FOR [TestColumn1];";
+ "ALTER TABLE [dbo].[TestTable1] WITH NOCHECK ADD CONSTRAINT [DF_TestTable1_TestColumn1] DEFAULT(1) FOR [TestColumn1];";
sql.ShouldBe(expected);
}
@@ -111,7 +111,7 @@ public void CanAlterDefaultConstraintWithCustomSchema()
"SET @sql = N'ALTER TABLE [TestSchema].[TestTable1] DROP CONSTRAINT ' + @default;\r\n" +
"EXEC sp_executesql @sql;\r\n\r\n" +
"-- create alter table command to create new default constraint as string and run it\r\n" +
- "ALTER TABLE [TestSchema].[TestTable1] WITH NOCHECK ADD CONSTRAINT DF_TestTable1_TestColumn1 DEFAULT(1) FOR [TestColumn1];";
+ "ALTER TABLE [TestSchema].[TestTable1] WITH NOCHECK ADD CONSTRAINT [DF_TestTable1_TestColumn1] DEFAULT(1) FOR [TestColumn1];";
sql.ShouldBe(expected);
}
View
8 src/FluentMigrator.Tests/Unit/Generators/SqlServer2005/SqlServer2005CreateTableTests.cs
@@ -139,7 +139,7 @@ public void CanCreateTableWithDefaultValueWithDefaultSchema()
var expression = GeneratorTestHelper.GetCreateTableWithDefaultValue();
var sql = generator.Generate(expression);
sql.ShouldBe(
- "CREATE TABLE [dbo].[TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT DF_TestTable1_TestColumn1 DEFAULT 'Default', [TestColumn2] INT NOT NULL CONSTRAINT DF_TestTable1_TestColumn2 DEFAULT 0)");
+ "CREATE TABLE [dbo].[TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT [DF_TestTable1_TestColumn1] DEFAULT 'Default', [TestColumn2] INT NOT NULL CONSTRAINT [DF_TestTable1_TestColumn2] DEFAULT 0)");
}
@@ -150,7 +150,7 @@ public void CanCreateTableWithDefaultValueWithCustomSchema()
expression.SchemaName = "TestSchema";
var sql = generator.Generate(expression);
sql.ShouldBe(
- "CREATE TABLE [TestSchema].[TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT DF_TestTable1_TestColumn1 DEFAULT 'Default', [TestColumn2] INT NOT NULL CONSTRAINT DF_TestTable1_TestColumn2 DEFAULT 0)");
+ "CREATE TABLE [TestSchema].[TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT [DF_TestTable1_TestColumn1] DEFAULT 'Default', [TestColumn2] INT NOT NULL CONSTRAINT [DF_TestTable1_TestColumn2] DEFAULT 0)");
}
@@ -162,7 +162,7 @@ public void CanCreateTableWithDefaultValueExplicitlySetToNullWithDefaultSchema()
expression.Columns[0].TableName = expression.TableName;
var sql = generator.Generate(expression);
sql.ShouldBe(
- "CREATE TABLE [dbo].[TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT DF_TestTable1_TestColumn1 DEFAULT NULL, [TestColumn2] INT NOT NULL)");
+ "CREATE TABLE [dbo].[TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT [DF_TestTable1_TestColumn1] DEFAULT NULL, [TestColumn2] INT NOT NULL)");
}
@@ -175,7 +175,7 @@ public void CanCreateTableWithDefaultValueExplicitlySetToNullWithCustomSchema()
expression.Columns[0].TableName = expression.TableName;
var sql = generator.Generate(expression);
sql.ShouldBe(
- "CREATE TABLE [TestSchema].[TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT DF_TestTable1_TestColumn1 DEFAULT NULL, [TestColumn2] INT NOT NULL)");
+ "CREATE TABLE [TestSchema].[TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT [DF_TestTable1_TestColumn1] DEFAULT NULL, [TestColumn2] INT NOT NULL)");
}
View
4 src/FluentMigrator.Tests/Unit/Generators/SqlServerCe/SqlServerCeCreateTableTests.cs
@@ -87,7 +87,7 @@ public void CanCreateTableWithDefaultValue()
var expression = GeneratorTestHelper.GetCreateTableWithDefaultValue();
var sql = generator.Generate(expression);
sql.ShouldBe(
- "CREATE TABLE [TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT DF_TestTable1_TestColumn1 DEFAULT 'Default', [TestColumn2] INT NOT NULL CONSTRAINT DF_TestTable1_TestColumn2 DEFAULT 0)");
+ "CREATE TABLE [TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT [DF_TestTable1_TestColumn1] DEFAULT 'Default', [TestColumn2] INT NOT NULL CONSTRAINT [DF_TestTable1_TestColumn2] DEFAULT 0)");
}
[Test]
@@ -98,7 +98,7 @@ public void CanCreateTableWithDefaultValueExplicitlySetToNull()
expression.Columns[0].TableName = expression.TableName;
var sql = generator.Generate(expression);
sql.ShouldBe(
- "CREATE TABLE [TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT DF_TestTable1_TestColumn1 DEFAULT NULL, [TestColumn2] INT NOT NULL)");
+ "CREATE TABLE [TestTable1] ([TestColumn1] NVARCHAR(255) NOT NULL CONSTRAINT [DF_TestTable1_TestColumn1] DEFAULT NULL, [TestColumn2] INT NOT NULL)");
}