Skip to content

Commit b8cfcde

Browse files
committed
Fix for 15256 - bad generated SQL for DefaultValueSql with line breaks.
1 parent 112b606 commit b8cfcde

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ protected virtual void DefaultValue(
12391239
{
12401240
builder
12411241
.Append(" DEFAULT (")
1242-
.Append(defaultValueSql)
1242+
.Append(EscapeLineBreaks(defaultValueSql))
12431243
.Append(")");
12441244
}
12451245
else if (defaultValue != null)
@@ -1676,5 +1676,21 @@ protected virtual bool TryGetVersion([CanBeNull] IModel model, out string versio
16761676

16771677
return true;
16781678
}
1679+
1680+
private static readonly char[] LineBreakChars = new char[] { '\r', '\n' };
1681+
private static string EscapeLineBreaks(string value)
1682+
{
1683+
if (value == null
1684+
|| value.IndexOfAny(LineBreakChars) == -1)
1685+
{
1686+
return value;
1687+
}
1688+
1689+
return ("CONCAT('" + value
1690+
.Replace("\r", "', CHAR(13), '")
1691+
.Replace("\n", "', CHAR(10), '") + "')")
1692+
.Replace("'', ", string.Empty)
1693+
.Replace(", ''", string.Empty);
1694+
}
16791695
}
16801696
}

test/EFCore.Relational.Tests/Migrations/MigrationSqlGeneratorTestBase.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,26 @@ public virtual void SqlOperation()
138138
=> Generate(
139139
new SqlOperation { Sql = "-- I <3 DDL" });
140140

141+
public virtual void DefaultValueSql_with_line_breaks()
142+
{
143+
Generate(
144+
new CreateTableOperation
145+
{
146+
Name = "TestLineBreaks",
147+
Schema = "dbo",
148+
Columns =
149+
{
150+
new AddColumnOperation
151+
{
152+
Name = "TestDefaultValueSql",
153+
Table = "Test",
154+
ClrType = typeof(string),
155+
DefaultValueSql = "\r\nVarious Line\rBreaks\n"
156+
}
157+
}
158+
});
159+
}
160+
141161
protected TestHelpers TestHelpers { get; }
142162

143163
protected MigrationSqlGeneratorTestBase(TestHelpers testHelpers)

test/EFCore.SqlServer.Tests/Migrations/SqlServerMigrationSqlGeneratorTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,17 @@ public virtual void SqlOperation_ignores_non_go()
545545
");
546546
}
547547

548+
[ConditionalFact]
549+
public override void DefaultValueSql_with_line_breaks()
550+
{
551+
base.DefaultValueSql_with_line_breaks();
552+
553+
AssertSql(@"CREATE TABLE [dbo].[TestLineBreaks] (
554+
[TestDefaultValueSql] nvarchar(max) NOT NULL DEFAULT (CONCAT(CHAR(13), CHAR(10), 'Various Line', CHAR(13), 'Breaks', CHAR(10)))
555+
);
556+
");
557+
}
558+
548559
public SqlServerMigrationSqlGeneratorTest()
549560
: base(SqlServerTestHelpers.Instance)
550561
{

test/EFCore.Sqlite.Tests/Migrations/SqliteMigrationSqlGeneratorTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ public virtual void DefaultValue_formats_literal_correctly()
7171
");
7272
}
7373

74+
[ConditionalFact]
75+
public override void DefaultValueSql_with_line_breaks()
76+
{
77+
base.DefaultValueSql_with_line_breaks();
78+
79+
AssertSql(
80+
@"CREATE TABLE ""TestLineBreaks"" (
81+
""TestDefaultValueSql"" TEXT NOT NULL DEFAULT (CONCAT(CHAR(13), CHAR(10), 'Various Line', CHAR(13), 'Breaks', CHAR(10)))
82+
);
83+
");
84+
}
85+
7486
[ConditionalTheory]
7587
[InlineData(true, null)]
7688
[InlineData(false, "PK_Id")]

0 commit comments

Comments
 (0)