Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove regex from SqlServerMigrationsSqlGenerator #30069

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 30 additions & 33 deletions src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore.SqlServer.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Update.Internal;
Expand Down Expand Up @@ -1398,49 +1397,47 @@ protected override void Generate(
/// <param name="builder">The command builder to use to build the commands.</param>
protected override void Generate(SqlOperation operation, IModel? model, MigrationCommandListBuilder builder)
{
var batches = Regex.Split(
Regex.Replace(
operation.Sql,
@"\\\r?\n",
string.Empty,
default,
TimeSpan.FromMilliseconds(1000.0)),
@"^\s*(GO[ \t]+[0-9]+|GO)(?:\s+|$)",
RegexOptions.IgnoreCase | RegexOptions.Multiline,
TimeSpan.FromMilliseconds(1000.0));
for (var i = 0; i < batches.Length; i++)
{
if (batches[i].StartsWith("GO", StringComparison.OrdinalIgnoreCase)
|| string.IsNullOrWhiteSpace(batches[i]))
var preBatched = operation.Sql
.Replace("\\\n", "")
.Replace("\\\r\n", "")
Comment on lines +1401 to +1402
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.Replace("\\\n", "")
.Replace("\\\r\n", "")
.Replace("\\\r\n", "")
.Replace("\\\n", "")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);

var batchBuilder = new StringBuilder();
foreach (var line in preBatched)
{
if (string.IsNullOrWhiteSpace(line))
{
continue;
}

var count = 1;
if (i != batches.Length - 1
&& batches[i + 1].StartsWith("GO", StringComparison.OrdinalIgnoreCase))
var trimmed = line.TrimStart();
if (trimmed.StartsWith("GO", StringComparison.OrdinalIgnoreCase))
{
var match = Regex.Match(
batches[i + 1], "([0-9]+)",
default,
TimeSpan.FromMilliseconds(1000.0));
if (match.Success)
var batch = batchBuilder.ToString();
batchBuilder.Clear();

var count = trimmed.Length >= 4
&& int.TryParse(trimmed.Substring(3), out var specifiedCount)
? specifiedCount
: 1;

for (var j = 0; j < count; j++)
{
count = int.Parse(match.Value);
AppendBatch(batch);
}
}

for (var j = 0; j < count; j++)
else
{
builder.Append(batches[i]);
batchBuilder.AppendLine(line);
}
}

if (i == batches.Length - 1)
{
builder.AppendLine();
}
AppendBatch(batchBuilder.ToString());

EndStatement(builder, operation.SuppressTransaction);
}
void AppendBatch(string batch)
{
builder.Append(batch);
EndStatement(builder, operation.SuppressTransaction);
}
}

Expand Down