Skip to content

Commit

Permalink
See changelog. (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
chullybun committed Nov 8, 2022
1 parent e3b501a commit 2750c53
Show file tree
Hide file tree
Showing 27 changed files with 237 additions and 141 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Represents the **NuGet** versions.

## v1.0.18
- *Fixed:* Corrected issue where comments were removed from the SQL statement when executed against the database; i.e. they were missing from the likes of stored procedures, etc.
- *Fixed:* Schema create template updated to remove transaction wrap which was invalid.
- *Enhancement:* Existing namespace `Schema` renamed to `DbSchema`.
- *Enhancement:* Updated the outbox code generation templates to support the `Key` column.

## v1.0.17
- *Fixed:* Updated to `CoreEx` version `1.0.9` and `OnRamp` version `1.0.6`.

Expand Down
3 changes: 2 additions & 1 deletion src/DbEx/DatabaseExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/DbEx

using CoreEx.Database;
using DbEx.Schema;
using DbEx.DbSchema;
using OnRamp.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
Expand Down
44 changes: 6 additions & 38 deletions src/DbEx/DbEx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>DbEx</RootNamespace>
<Version>1.0.17</Version>
<Version>1.0.18</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>DbEx Developers</Authors>
<Company>Avanade</Company>
Expand Down Expand Up @@ -39,42 +39,10 @@
</PropertyGroup>

<ItemGroup>
<None Remove="Resources\SqlServer\DatabaseCreate.sql" />
<None Remove="Resources\SqlServer\DatabaseDrop.sql" />
<None Remove="Resources\SqlServer\JournalAuditScript.sql" />
<None Remove="Resources\SqlServer\JournalEnsureExists.sql" />
<None Remove="Resources\SqlServer\JournalGetExecuted.sql" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\Schema_sql.hbs" />
<EmbeddedResource Include="Resources\Alter_sql.hbs" />
<EmbeddedResource Include="Resources\CdcDb_sql.hbs" />
<EmbeddedResource Include="Resources\Cdc_sql.hbs" />
<EmbeddedResource Include="Resources\Create_sql.hbs" />
<EmbeddedResource Include="Resources\Default_sql.hbs" />
<EmbeddedResource Include="Resources\RefData_sql.hbs" />
<EmbeddedResource Include="Resources\SelectTableAlwaysGeneratedColumns.sql" />
<EmbeddedResource Include="Resources\SelectTableAndColumns.sql" />
<EmbeddedResource Include="Resources\SelectTableForeignKeys.sql" />
<EmbeddedResource Include="Resources\SelectTableGeneratedColumns.sql" />
<EmbeddedResource Include="Resources\SelectTableIdentityColumns.sql" />
<EmbeddedResource Include="Resources\SelectTablePrimaryKey.sql" />
<EmbeddedResource Include="Resources\SqlServer\DatabaseDrop.sql" />
<EmbeddedResource Include="Resources\SqlServer\DatabaseCreate.sql" />
<EmbeddedResource Include="Resources\SqlServer\DeleteAllAndReset.sql" />
<EmbeddedResource Include="Resources\SqlServer\JournalAuditScript.sql" />
<EmbeddedResource Include="Resources\SqlServer\JournalEnsureExists.sql" />
<EmbeddedResource Include="Resources\SqlServer\JournalGetExecuted.sql" />
<EmbeddedResource Include="Resources\SqlServer\TableInsertOrMerge_sql.hb" />
<EmbeddedResource Include="Templates\SqlServer\EventOutboxDequeue_cs.hbs" />
<EmbeddedResource Include="Templates\SqlServer\EventOutboxEnqueue_cs.hbs" />
<EmbeddedResource Include="Templates\SqlServer\SchemaEventOutbox_sql.hbs" />
<EmbeddedResource Include="Templates\SqlServer\SpEventOutboxDequeue_sql.hbs" />
<EmbeddedResource Include="Templates\SqlServer\SpEventOutboxEnqueue_sql.hbs" />
<EmbeddedResource Include="Templates\SqlServer\TableEventOutboxData_sql.hbs" />
<EmbeddedResource Include="Templates\SqlServer\TableEventOutbox_sql.hbs" />
<EmbeddedResource Include="Templates\SqlServer\UdtEventOutbox_sql.hbs" />
<EmbeddedResource Include="Resources\**\*" />
<EmbeddedResource Include="Migrations\**\*" />
<EmbeddedResource Include="Data\**\*" />
<EmbeddedResource Include="Templates\**\*" />
</ItemGroup>

<ItemGroup>
Expand All @@ -85,7 +53,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CoreEx.Database" Version="1.0.9" />
<PackageReference Include="CoreEx.Database" Version="1.0.11" />
<PackageReference Include="OnRamp" Version="1.0.6" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Diagnostics;
using System.Text;

namespace DbEx.Schema
namespace DbEx.DbSchema
{
/// <summary>
/// Represents the Database <b>Column</b> schema definition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Linq;
using System.Text.RegularExpressions;

namespace DbEx.Schema
namespace DbEx.DbSchema
{
/// <summary>
/// Represents the Database <b>Table</b> schema definition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using System;

namespace DbEx.Schema
namespace DbEx.DbSchema
{
/// <summary>
/// Provides Database Type mapper/helper functions.
Expand Down
2 changes: 1 addition & 1 deletion src/DbEx/Migration/Data/DataColumn.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/DbEx

using DbEx.Schema;
using DbEx.DbSchema;
using System;

namespace DbEx.Migration.Data
Expand Down
2 changes: 1 addition & 1 deletion src/DbEx/Migration/Data/DataParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/DbEx

using DbEx.Schema;
using DbEx.DbSchema;
using HandlebarsDotNet;
using Newtonsoft.Json.Linq;
using System;
Expand Down
2 changes: 1 addition & 1 deletion src/DbEx/Migration/Data/DataParserArgs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/DbEx

using DbEx.Schema;
using DbEx.DbSchema;
using System;
using System.Collections.Generic;

Expand Down
2 changes: 1 addition & 1 deletion src/DbEx/Migration/Data/DataRow.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/DbEx

using DbEx.Schema;
using DbEx.DbSchema;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
2 changes: 1 addition & 1 deletion src/DbEx/Migration/Data/DataTable.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/DbEx

using DbEx.Schema;
using DbEx.DbSchema;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
14 changes: 12 additions & 2 deletions src/DbEx/Migration/SqlServer/Internal/SqlServerObjectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace DbEx.Migration.SqlServer.Internal
{
Expand Down Expand Up @@ -106,7 +105,18 @@ private void Parse()
int start = -1;
string? line;
using var sr = new StringReader(_sql);
using var tr = new StringReader(SqlServerMigrator.CleanSql(sr));

var stmts = SqlServerMigrator.SplitAndCleanSql(sr);
if (stmts.Count == 0)
return;

if (stmts.Count > 1)
{
ErrorMessage = "The SQL contains more than a single statement.";
return;
}

using var tr = new StringReader(stmts[0].CleanSql);
while ((line = tr.ReadLine()) is not null)
{
// Parse out the token(s).
Expand Down
120 changes: 57 additions & 63 deletions src/DbEx/Migration/SqlServer/SqlServerMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,70 +227,32 @@ public override async Task<bool> ExecuteScriptsAsync(IEnumerable<DatabaseMigrati
private async Task ExecuteScriptAsync(DatabaseMigrationScript script, CancellationToken cancellationToken = default)
{
using var sr = script.GetStreamReader();
foreach (var sql in SplitSql(CleanSql(sr)))
foreach (var (OriginalSql, _) in SplitAndCleanSql(sr))
{
await Database.SqlStatement(sql).NonQueryAsync(cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrEmpty(OriginalSql))
await Database.SqlStatement(OriginalSql).NonQueryAsync(cancellationToken).ConfigureAwait(false);
}
}

/// <summary>
/// <see cref="CleanSql(TextReader)">Cleans</see> and splits (based on the SQL Server '<c><see href="http://technet.microsoft.com/en-us/library/ms188037.aspx">GO</see></c>' statement) the SQL command(s) from a <see cref="TextReader"/>.
/// Splits (based on the SQL Server '<c><see href="http://technet.microsoft.com/en-us/library/ms188037.aspx">GO</see></c>' statement) and cleans (removes comments) the SQL command(s) from a <see cref="TextReader"/>.
/// </summary>
/// <param name="tr">The <see cref="TextReader"/>.</param>
/// <returns>A list of executable SQL statements.</returns>
/// <remarks>The '<c>GO [count]</c>' count syntax is not supported; i.e. will not be parsed correctly and will likely result in an error when executed.</remarks>
public static List<string> CleanAndSplitSql(TextReader tr) => SplitSql(CleanSql(tr));

/// <summary>
/// Split the SQL into multiple statements based on the SQL Server '<c><see href="http://technet.microsoft.com/en-us/library/ms188037.aspx">GO</see></c>' statement.
/// </summary>
/// <param name="sql">The originating SQL.</param>
/// <returns>A list of executable SQL statements.</returns>
private static List<string> SplitSql(string sql)
/// <returns>A list of executable SQL statement pairs.</returns>
/// <remarks>The resulting list contains two strings, the first being the original SQL statement (including all comments), and the second being the cleaned SQL statement (all comments removed).
/// <para>Note: The '<c>GO [count]</c>' count syntax is not supported; i.e. will not be parsed correctly and will likely result in an error when executed.</para></remarks>
public static List<(string OriginalSql, string CleanSql)> SplitAndCleanSql(TextReader tr)
{
var list = new List<string>();
if (string.IsNullOrEmpty(sql))
return list;

var sb = new StringBuilder();
string? line;
using var tr = new StringReader(sql);

while ((line = tr.ReadLine()) is not null)
{
if (line.Trim().Equals("GO", StringComparison.InvariantCultureIgnoreCase))
{
if (sb.Length > 0 && !string.IsNullOrEmpty(sb.ToString().Trim()))
list.Add(sb.ToString());

sb.Clear();
}
else
{
if (sb.Length > 0)
sb.AppendLine();
if (tr == null)
throw new ArgumentNullException(nameof(tr));

sb.Append(line);
}
}

if (sb.Length > 0 && !string.IsNullOrEmpty(sb.ToString().Trim()))
list.Add(sb.ToString());

return list;
}

/// <summary>
/// Reads the SQL command from a <see cref="TextReader"/> and parses out all of the comments.
/// </summary>
/// <param name="tr">The <see cref="TextReader"/>.</param>
public static string CleanSql(TextReader tr)
{
string? line;
var list = new List<(string, string)>();
string? orig, line;
bool inComment = false;
StringBuilder sb = new();
StringBuilder sbo = new();
StringBuilder sbc = new();

while ((line = tr.ReadLine()) is not null)
while ((orig = line = tr.ReadLine()) is not null)
{
int ci;

Expand All @@ -299,41 +261,73 @@ public static string CleanSql(TextReader tr)
{
ci = line.IndexOf("*/");
if (ci < 0)
{
sbo.AppendLine(orig);
continue;
}

line = line[(ci + 2)..];
inComment = false;

if (string.IsNullOrEmpty(line))
{
sbo.AppendLine(orig);
continue;
}
}

ci = line.IndexOf("/*");
if (ci >= 0)
while ((ci = line.IndexOf("/*")) >= 0)
{
sbc.Append(line[0..ci]);
line = line[ci..];
var ci2 = line.IndexOf("*/");
if (ci2 >= 0)
line = line[0..ci] + line[(ci2 + 2)..];
line = line[(ci2 + 2)..];
else
{
inComment = true;
continue;
break;
}
}

if (inComment == true)
{
sbo.AppendLine(orig);
continue;
}

// Remove -- comments.
ci = line.IndexOf("--", StringComparison.InvariantCulture);
if (ci >= 0)
line = line[..ci];

// Add as valid statement.
if (sb.Length > 0)
sb.AppendLine();

sb.Append(line);
if (line.Trim().Equals("GO", StringComparison.InvariantCultureIgnoreCase))
{
AddSqlStatement(list, sbo.ToString(), sbc.ToString());
sbo.Clear();
sbc.Clear();
}
else
{
sbo.AppendLine(orig);
sbc.AppendLine(line);
}
}

return sb.ToString();
AddSqlStatement(list, sbo.ToString(), sbc.ToString());
return list;
}

/// <summary>
/// Adds the SQL statement to the list.
/// </summary>
private static void AddSqlStatement(List<(string OriginalSql, string CleanSql)> list, string orig, string clean)
{
var temp = clean.Replace(Environment.NewLine, string.Empty).Trim();
if (temp.Length == 0)
list.Add((orig, string.Empty));
else
list.Add((orig, clean));
}
}
}
6 changes: 1 addition & 5 deletions src/DbEx/Resources/Schema_sql.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@
{{! PARAM:Param1=Name }}
-- Create schema: [{{lookup Parameters 'Param1'}}]

BEGIN TRANSACTION

CREATE SCHEMA [{{lookup Parameters 'Param1'}}]
AUTHORIZATION [dbo];

COMMIT TRANSACTION
AUTHORIZATION [dbo];
1 change: 1 addition & 0 deletions src/DbEx/Templates/SqlServer/SpEventOutboxDequeue_sql.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ BEGIN
[Source],
[Timestamp],
[CorrelationId],
[Key],
[TenantId],
[PartitionKey],
[ETag],
Expand Down
Loading

0 comments on commit 2750c53

Please sign in to comment.