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

Refine Oracle statement batch splitting #329

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,12 @@ public void slash_with_comment_after()
}

[Test]
public void slash_with_semicolon_directly_after()
public void slash_with_semicolon_directly_after_yields_just_one_splitter()
{
string sql_to_match = "jalla /;";
string expected_scrubbed = "jalla " + Batch_terminator_replacement_string + ";";
string sql_to_match = @"jalla;
/";
string expected_scrubbed = "jalla" + @"
" + Batch_terminator_replacement_string;
TestContext.WriteLine(sql_to_match);
string sql_statement_scrubbed = Replacer.Replace(sql_to_match);
Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using System.Linq;
using FluentAssertions;
using grate.Infrastructure;
using grate.Migration;
using Microsoft.Extensions.Logging.Abstractions;
Expand All @@ -15,7 +16,7 @@ public class StatementSplitter_
private static readonly StatementSplitter Splitter = new(Database.StatementSeparatorRegex);

[Test]
public void Splits_and_removes_GO_statements()
public void Splits_and_removes_slashes_and_semicolon()
{
var original = @"
SELECT * FROM v$version WHERE banner LIKE 'Oracle%';
Expand All @@ -27,6 +28,39 @@ SELECT 1
var batches = Splitter.Split(original);

batches.Should().HaveCount(2);
batches.First().Should().NotEndWith(";");
}

[Test]
public void Splits_and_removes_slashes_and_semicolon_2()
{
var original = @"
create table table_one (
col number
);
/

create table table_two (
col number
)
";
var batches = Splitter.Split(original);

batches.Should().HaveCount(2);
batches.First().Should().NotEndWith(";");
}

[Test]
public void Splits_and_removes_semicolon()
{
var original = @"
SELECT * FROM v$version WHERE banner LIKE 'Oracle%';
SELECT 1
";
var batches = Splitter.Split(original).ToArray();

batches.Should().HaveCount(2);
batches.First().Should().NotEndWith(";");
}

}
13 changes: 7 additions & 6 deletions grate.unittests/Generic/GenericDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ public virtual async Task Is_created_with_custom_script_if_custom_create_databas

var customScript = Context.Syntax.CreateDatabase(scriptedDatabase, password);
TestConfig.WriteContent(Wrap(config.SqlFilesDirectory, config.Folders?.CreateDatabase?.Path), "createDatabase.sql", customScript);
try
{
//try
//{
await using var migrator = GetMigrator(config);
await migrator.Migrate();
}
catch (DbException)
{
//}
//catch (DbException e)
//{
//var s = e.Message;
//Do nothing because database name is wrong due to custom script
}
//}

File.Delete(Path.Join(Wrap(config.SqlFilesDirectory, config.Folders?.CreateDatabase?.Path).ToString(), "createDatabase.sql"));

Expand Down
18 changes: 1 addition & 17 deletions grate.unittests/TestInfrastructure/OracleSplitterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ public static class FullSplitter

/* / */

BOB7

/*

/

*/

BOB8

--
Expand Down Expand Up @@ -121,14 +113,6 @@ INSERT [dbo].[Foo] ([Bar]) VALUES (N'/ speed racer, / speed racer, / speed racer

/* / */

BOB7

/*

/

*/

BOB8

--
Expand Down Expand Up @@ -164,7 +148,7 @@ yeppsasd decimal(20, 6) NULL,
uhuhhh datetime NULL,
slsald varchar(15) NULL,
uhasdf varchar(15) NULL,
daf_asdfasdf DECIMAL(20,6) NULL;
daf_asdfasdf DECIMAL(20,6) NULL
" + StatementSplitter.BatchTerminatorReplacementString + @"

EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Daily job',
Expand Down
15 changes: 13 additions & 2 deletions grate/Infrastructure/BatchSplitterReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,23 @@ public BatchSplitterReplacer(string pattern, string replacement)
_regex = new Regex(pattern, IgnoreCase | Multiline);
}

public string Replace(string text) => _regex.Replace(text, ReplaceBatchSeparator);
public string Replace(string text)
{
var replace = _regex.Replace(text, ReplaceBatchSeparator);

// Combine multiple consecutive replacement tokens with one (needed for Oracle, if ; and / are on separate lines)
replace = Regex.Replace(
replace,
Regex.Escape(Replacement) + "(\\s*)" + Regex.Escape(Replacement),
"$1" + Replacement);

return replace;
}

private string ReplaceBatchSeparator(Match match)
{
var groups = match.Groups;
var replacement = groups["BATCHSPLITTER"].Success ? Replacement : string.Empty;
return groups["KEEP1"].Value + replacement + groups["KEEP2"].Value;
}
}
}
6 changes: 4 additions & 2 deletions grate/Infrastructure/OracleSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public string StatementSeparatorRegex
const string strings = @"(?<KEEP1>'[^']*')";
const string dashComments = @"(?<KEEP1>--.*$)";
const string starComments = @"(?<KEEP1>/\*[\S\s]*?\*/)";
const string separator = @"(?<KEEP1>^|\s)(?<BATCHSPLITTER>/)(?<KEEP2>\s|;|$)";
return strings + "|" + dashComments + "|" + starComments + "|" + separator;
const string batchSeparator = @"(?<KEEP1>.*)(?<BATCHSPLITTER>;)(?<KEEP2>\s|$)";
//const string sqlPlusExecuteCommand = @"(?<KEEP1>^|\s)(?<BATCHSPLITTER>\/|;)(?<KEEP2>\s*|$|\n)";
const string sqlPlusExecuteCommand = @"(?<KEEP1>^|\s)(?<BATCHSPLITTER>\/)(?<KEEP2>\s|$)";
return strings + "|" + dashComments + "|" + starComments + "|" + batchSeparator + "|" + sqlPlusExecuteCommand;
}
}

Expand Down