Skip to content

Commit

Permalink
Working on refining replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbra committed Jun 3, 2023
1 parent 1f45209 commit f41cd41
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,10 @@ public void slash_with_comment_after()
[Test]
public void slash_with_semicolon_directly_after()
{
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 + @"
" + 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,20 @@ SELECT 1
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(";");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using FluentAssertions;
using grate.Configuration;
using grate.Migration;
using grate.unittests.TestInfrastructure;
using NUnit.Framework;
using static grate.Configuration.KnownFolderKeys;

namespace grate.unittests.Oracle.Running_MigrationScripts;

[TestFixture]
[Category("Oracle")]
// ReSharper disable once InconsistentNaming
public class With_batch_separator: Generic.Running_MigrationScripts.MigrationsScriptsBase
{
protected override IGrateTestContext Context => GrateTestContext.Oracle;

[Test()]
public async Task Separates_multiple_statements()
{
var db = TestConfig.RandomDatabase();

var parent = TestConfig.CreateRandomTempDirectory();
var knownFolders = FoldersConfiguration.Default(null);
GrateMigrator? migrator;


var path = new DirectoryInfo(Path.Combine(parent.ToString(), knownFolders[Up]!.Path));
const string filename = "multiple_statements.sql";

const string fileContent = @"
create table table_one (
col number
);
/
create table table_two (
col number
)
";

WriteSql(path, filename, fileContent);

await using (migrator = Context.GetMigrator(db, parent, knownFolders))
{
await migrator.Migrate();
}

string[] scripts;
string sql = $"SELECT text_of_script FROM {Context.Syntax.TableWithSchema("grate", "ScriptsRun")}";

await using (var conn = Context.CreateDbConnection(db))
{
scripts = (await conn.QueryAsync<string>(sql)).ToArray();
}

scripts.Should().HaveCount(2);
}

}
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
5 changes: 3 additions & 2 deletions grate/Infrastructure/OracleSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ 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|$)";
return strings + "|" + dashComments + "|" + starComments + "|" + batchSeparator + "|" + sqlPlusExecuteCommand;
}
}

Expand Down

0 comments on commit f41cd41

Please sign in to comment.