Skip to content

Commit f41cd41

Browse files
committed
Working on refining replacement
1 parent 1f45209 commit f41cd41

File tree

5 files changed

+88
-23
lines changed

5 files changed

+88
-23
lines changed

grate.unittests/Basic/Infrastructure/Oracle/Statement_Splitting/BatchSplitterReplacer_.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@ public void slash_with_comment_after()
264264
[Test]
265265
public void slash_with_semicolon_directly_after()
266266
{
267-
string sql_to_match = "jalla /;";
268-
string expected_scrubbed = "jalla " + Batch_terminator_replacement_string + ";";
267+
string sql_to_match = @"jalla;
268+
/";
269+
string expected_scrubbed = "jalla" + Batch_terminator_replacement_string + @"
270+
" + Batch_terminator_replacement_string;
269271
TestContext.WriteLine(sql_to_match);
270272
string sql_statement_scrubbed = Replacer.Replace(sql_to_match);
271273
Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed);

grate.unittests/Basic/Infrastructure/Oracle/Statement_Splitting/StatementSplitter_.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using FluentAssertions;
1+
using System.Linq;
2+
using FluentAssertions;
23
using grate.Infrastructure;
34
using grate.Migration;
45
using Microsoft.Extensions.Logging.Abstractions;
@@ -15,7 +16,7 @@ public class StatementSplitter_
1516
private static readonly StatementSplitter Splitter = new(Database.StatementSeparatorRegex);
1617

1718
[Test]
18-
public void Splits_and_removes_GO_statements()
19+
public void Splits_and_removes_slashes_and_semicolon()
1920
{
2021
var original = @"
2122
SELECT * FROM v$version WHERE banner LIKE 'Oracle%';
@@ -27,6 +28,20 @@ SELECT 1
2728
var batches = Splitter.Split(original);
2829

2930
batches.Should().HaveCount(2);
31+
batches.First().Should().NotEndWith(";");
32+
}
33+
34+
[Test]
35+
public void Splits_and_removes_semicolon()
36+
{
37+
var original = @"
38+
SELECT * FROM v$version WHERE banner LIKE 'Oracle%';
39+
SELECT 1
40+
";
41+
var batches = Splitter.Split(original).ToArray();
42+
43+
batches.Should().HaveCount(2);
44+
batches.First().Should().NotEndWith(";");
3045
}
3146

3247
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.IO;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Dapper;
5+
using FluentAssertions;
6+
using grate.Configuration;
7+
using grate.Migration;
8+
using grate.unittests.TestInfrastructure;
9+
using NUnit.Framework;
10+
using static grate.Configuration.KnownFolderKeys;
11+
12+
namespace grate.unittests.Oracle.Running_MigrationScripts;
13+
14+
[TestFixture]
15+
[Category("Oracle")]
16+
// ReSharper disable once InconsistentNaming
17+
public class With_batch_separator: Generic.Running_MigrationScripts.MigrationsScriptsBase
18+
{
19+
protected override IGrateTestContext Context => GrateTestContext.Oracle;
20+
21+
[Test()]
22+
public async Task Separates_multiple_statements()
23+
{
24+
var db = TestConfig.RandomDatabase();
25+
26+
var parent = TestConfig.CreateRandomTempDirectory();
27+
var knownFolders = FoldersConfiguration.Default(null);
28+
GrateMigrator? migrator;
29+
30+
31+
var path = new DirectoryInfo(Path.Combine(parent.ToString(), knownFolders[Up]!.Path));
32+
const string filename = "multiple_statements.sql";
33+
34+
const string fileContent = @"
35+
create table table_one (
36+
col number
37+
);
38+
/
39+
40+
create table table_two (
41+
col number
42+
)
43+
";
44+
45+
WriteSql(path, filename, fileContent);
46+
47+
await using (migrator = Context.GetMigrator(db, parent, knownFolders))
48+
{
49+
await migrator.Migrate();
50+
}
51+
52+
string[] scripts;
53+
string sql = $"SELECT text_of_script FROM {Context.Syntax.TableWithSchema("grate", "ScriptsRun")}";
54+
55+
await using (var conn = Context.CreateDbConnection(db))
56+
{
57+
scripts = (await conn.QueryAsync<string>(sql)).ToArray();
58+
}
59+
60+
scripts.Should().HaveCount(2);
61+
}
62+
63+
}

grate.unittests/TestInfrastructure/OracleSplitterContext.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ public static class FullSplitter
3030
3131
/* / */
3232
33-
BOB7
34-
35-
/*
36-
37-
/
38-
39-
*/
40-
4133
BOB8
4234
4335
--
@@ -121,14 +113,6 @@ INSERT [dbo].[Foo] ([Bar]) VALUES (N'/ speed racer, / speed racer, / speed racer
121113
122114
/* / */
123115
124-
BOB7
125-
126-
/*
127-
128-
/
129-
130-
*/
131-
132116
BOB8
133117
134118
--
@@ -164,7 +148,7 @@ yeppsasd decimal(20, 6) NULL,
164148
uhuhhh datetime NULL,
165149
slsald varchar(15) NULL,
166150
uhasdf varchar(15) NULL,
167-
daf_asdfasdf DECIMAL(20,6) NULL;
151+
daf_asdfasdf DECIMAL(20,6) NULL
168152
" + StatementSplitter.BatchTerminatorReplacementString + @"
169153
170154
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Daily job',

grate/Infrastructure/OracleSyntax.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ public string StatementSeparatorRegex
1111
const string strings = @"(?<KEEP1>'[^']*')";
1212
const string dashComments = @"(?<KEEP1>--.*$)";
1313
const string starComments = @"(?<KEEP1>/\*[\S\s]*?\*/)";
14-
const string separator = @"(?<KEEP1>^|\s)(?<BATCHSPLITTER>/)(?<KEEP2>\s|;|$)";
15-
return strings + "|" + dashComments + "|" + starComments + "|" + separator;
14+
const string batchSeparator = @"(?<KEEP1>.*)(?<BATCHSPLITTER>;)(?<KEEP2>\s|$)";
15+
const string sqlPlusExecuteCommand = @"(?<KEEP1>^|\s)(?<BATCHSPLITTER>/)(?<KEEP2>\s|$)";
16+
return strings + "|" + dashComments + "|" + starComments + "|" + batchSeparator + "|" + sqlPlusExecuteCommand;
1617
}
1718
}
1819

0 commit comments

Comments
 (0)