Skip to content

Commit

Permalink
TextScript.GetDependencies check only first batch
Browse files Browse the repository at this point in the history
  • Loading branch information
max-ieremenko committed May 27, 2021
1 parent e569a45 commit 6233a1e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 11 deletions.
9 changes: 7 additions & 2 deletions Sources/SqlDatabase.Test/Scripts/MsSql/MsSqlTextReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ public void IsGo(string line, bool expected)
[TestCaseSource(nameof(GetSplitByGoTestCases))]
public void SplitByGo(Stream input, string[] expected)
{
var actual = _sut.Read(input);
actual.ShouldBe(expected);
var batches = _sut.ReadBatches(input);
batches.ShouldBe(expected);

input.Position = 0;

var first = _sut.ReadFirstBatch(input);
first.ShouldBe(expected[0]);
}

private static IEnumerable<TestCaseData> GetSplitByGoTestCases()
Expand Down
38 changes: 35 additions & 3 deletions Sources/SqlDatabase.Test/Scripts/PgSql/PgSqlTextReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,44 @@ namespace SqlDatabase.Scripts.PgSql
[TestFixture]
public class PgSqlTextReaderTest
{
private PgSqlTextReader _sut;

[SetUp]
public void BeforeEachTest()
{
_sut = new PgSqlTextReader();
}

[Test]
public void ReadFirstBatch()
{
const string Expected = @"
/*
* module dependency: a 2.0
* module dependency: b 1.0
*/
;
line 2;";

var actual = _sut.ReadFirstBatch(Expected.AsFuncStream()());

actual.ShouldBe(@"/*
* module dependency: a 2.0
* module dependency: b 1.0
*/");
}

[Test]
public void Read()
public void ReadBatches()
{
const string Expected = "abc";
const string Expected = @"
line 1;
;
line 2;";

var actual = new PgSqlTextReader().Read(Expected.AsFuncStream()());
var actual = _sut.ReadBatches(Expected.AsFuncStream()());

actual.ShouldBe(new[] { Expected });
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/SqlDatabase/Scripts/ISqlTextReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace SqlDatabase.Scripts
{
internal interface ISqlTextReader
{
IEnumerable<string> Read(Stream sql);
string ReadFirstBatch(Stream sql);

IEnumerable<string> ReadBatches(Stream sql);
}
}
8 changes: 7 additions & 1 deletion Sources/SqlDatabase/Scripts/MsSql/MsSqlTextReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

Expand All @@ -9,7 +10,12 @@ internal sealed class MsSqlTextReader : ISqlTextReader
{
private readonly Regex _goRegex = new Regex("^(\\s*(go)+\\s*)+$", RegexOptions.Compiled | RegexOptions.IgnoreCase);

public IEnumerable<string> Read(Stream sql)
public string ReadFirstBatch(Stream sql)
{
return ReadBatches(sql).FirstOrDefault();
}

public IEnumerable<string> ReadBatches(Stream sql)
{
var batch = new StringBuilder();

Expand Down
43 changes: 41 additions & 2 deletions Sources/SqlDatabase/Scripts/PgSql/PgSqlTextReader.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace SqlDatabase.Scripts.PgSql
{
internal sealed class PgSqlTextReader : ISqlTextReader
{
public IEnumerable<string> Read(Stream sql)
private const int MaxFirstBatchSize = 20;
private readonly Regex _semicolonRegex = new Regex("^(\\s*;+\\s*)+$", RegexOptions.Compiled | RegexOptions.IgnoreCase);

public string ReadFirstBatch(Stream sql)
{
var script = new StringBuilder();

using (var reader = new StreamReader(sql))
{
string line;
var lineNumber = 0;
while ((line = reader.ReadLine()) != null)
{
if (script.Length == 0 && string.IsNullOrWhiteSpace(line))
{
continue;
}

lineNumber++;
if (_semicolonRegex.IsMatch(line) || lineNumber > MaxFirstBatchSize)
{
break;
}

if (script.Length > 0)
{
script.AppendLine();
}

script.Append(line);
}
}

return script.ToString();
}

public IEnumerable<string> ReadBatches(Stream sql)
{
string script;
using (var reader = new StreamReader(sql))
Expand Down
4 changes: 2 additions & 2 deletions Sources/SqlDatabase/Scripts/TextScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public IList<ScriptDependency> GetDependencies()
string batch;
using (var sql = ReadSqlContent())
{
batch = TextReader.Read(sql).FirstOrDefault();
batch = TextReader.ReadFirstBatch(sql);
}

if (string.IsNullOrWhiteSpace(batch))
Expand Down Expand Up @@ -162,7 +162,7 @@ private IEnumerable<string> ResolveBatches(IVariables variables, ILogger logger)
var batches = new List<string>();
using (var sql = ReadSqlContent())
{
foreach (var batch in TextReader.Read(sql))
foreach (var batch in TextReader.ReadBatches(sql))
{
var script = scriptParser.ApplyVariables(batch);
if (!string.IsNullOrWhiteSpace(script))
Expand Down

0 comments on commit 6233a1e

Please sign in to comment.