diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ffb93c..b17d939 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/src/DbEx/DatabaseExtensions.cs b/src/DbEx/DatabaseExtensions.cs index 36f18d6..2aae332 100644 --- a/src/DbEx/DatabaseExtensions.cs +++ b/src/DbEx/DatabaseExtensions.cs @@ -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; diff --git a/src/DbEx/DbEx.csproj b/src/DbEx/DbEx.csproj index e89aa5d..5bb8cf9 100644 --- a/src/DbEx/DbEx.csproj +++ b/src/DbEx/DbEx.csproj @@ -3,7 +3,7 @@ netstandard2.1 DbEx - 1.0.17 + 1.0.18 true DbEx Developers Avanade @@ -39,42 +39,10 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + @@ -85,7 +53,7 @@ - + diff --git a/src/DbEx/Schema/DbColumnSchema.cs b/src/DbEx/DbSchema/DbColumnSchema.cs similarity index 99% rename from src/DbEx/Schema/DbColumnSchema.cs rename to src/DbEx/DbSchema/DbColumnSchema.cs index 42a7a48..d53db75 100644 --- a/src/DbEx/Schema/DbColumnSchema.cs +++ b/src/DbEx/DbSchema/DbColumnSchema.cs @@ -4,7 +4,7 @@ using System.Diagnostics; using System.Text; -namespace DbEx.Schema +namespace DbEx.DbSchema { /// /// Represents the Database Column schema definition. diff --git a/src/DbEx/Schema/DbTableSchema.cs b/src/DbEx/DbSchema/DbTableSchema.cs similarity index 99% rename from src/DbEx/Schema/DbTableSchema.cs rename to src/DbEx/DbSchema/DbTableSchema.cs index 6bf5b41..59453a8 100644 --- a/src/DbEx/Schema/DbTableSchema.cs +++ b/src/DbEx/DbSchema/DbTableSchema.cs @@ -8,7 +8,7 @@ using System.Linq; using System.Text.RegularExpressions; -namespace DbEx.Schema +namespace DbEx.DbSchema { /// /// Represents the Database Table schema definition. diff --git a/src/DbEx/Schema/DbTypeMapper.cs b/src/DbEx/DbSchema/DbTypeMapper.cs similarity index 99% rename from src/DbEx/Schema/DbTypeMapper.cs rename to src/DbEx/DbSchema/DbTypeMapper.cs index ab4429a..5444d3c 100644 --- a/src/DbEx/Schema/DbTypeMapper.cs +++ b/src/DbEx/DbSchema/DbTypeMapper.cs @@ -2,7 +2,7 @@ using System; -namespace DbEx.Schema +namespace DbEx.DbSchema { /// /// Provides Database Type mapper/helper functions. diff --git a/src/DbEx/Migration/Data/DataColumn.cs b/src/DbEx/Migration/Data/DataColumn.cs index c3f7cc2..2db360a 100644 --- a/src/DbEx/Migration/Data/DataColumn.cs +++ b/src/DbEx/Migration/Data/DataColumn.cs @@ -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 diff --git a/src/DbEx/Migration/Data/DataParser.cs b/src/DbEx/Migration/Data/DataParser.cs index b937001..c73259c 100644 --- a/src/DbEx/Migration/Data/DataParser.cs +++ b/src/DbEx/Migration/Data/DataParser.cs @@ -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; diff --git a/src/DbEx/Migration/Data/DataParserArgs.cs b/src/DbEx/Migration/Data/DataParserArgs.cs index 61d9aa9..42b0bc8 100644 --- a/src/DbEx/Migration/Data/DataParserArgs.cs +++ b/src/DbEx/Migration/Data/DataParserArgs.cs @@ -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; diff --git a/src/DbEx/Migration/Data/DataRow.cs b/src/DbEx/Migration/Data/DataRow.cs index 32c2b91..6f44342 100644 --- a/src/DbEx/Migration/Data/DataRow.cs +++ b/src/DbEx/Migration/Data/DataRow.cs @@ -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; diff --git a/src/DbEx/Migration/Data/DataTable.cs b/src/DbEx/Migration/Data/DataTable.cs index 9a83704..8627cd7 100644 --- a/src/DbEx/Migration/Data/DataTable.cs +++ b/src/DbEx/Migration/Data/DataTable.cs @@ -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; diff --git a/src/DbEx/Migration/SqlServer/Internal/SqlServerObjectReader.cs b/src/DbEx/Migration/SqlServer/Internal/SqlServerObjectReader.cs index 9b20c16..35a601d 100644 --- a/src/DbEx/Migration/SqlServer/Internal/SqlServerObjectReader.cs +++ b/src/DbEx/Migration/SqlServer/Internal/SqlServerObjectReader.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; namespace DbEx.Migration.SqlServer.Internal { @@ -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). diff --git a/src/DbEx/Migration/SqlServer/SqlServerMigrator.cs b/src/DbEx/Migration/SqlServer/SqlServerMigrator.cs index fa17a18..7eb1c4c 100644 --- a/src/DbEx/Migration/SqlServer/SqlServerMigrator.cs +++ b/src/DbEx/Migration/SqlServer/SqlServerMigrator.cs @@ -227,70 +227,32 @@ public override async Task ExecuteScriptsAsync(IEnumerable - /// Cleans and splits (based on the SQL Server 'GO' statement) the SQL command(s) from a . + /// Splits (based on the SQL Server 'GO' statement) and cleans (removes comments) the SQL command(s) from a . /// /// The . - /// A list of executable SQL statements. - /// The 'GO [count]' count syntax is not supported; i.e. will not be parsed correctly and will likely result in an error when executed. - public static List CleanAndSplitSql(TextReader tr) => SplitSql(CleanSql(tr)); - - /// - /// Split the SQL into multiple statements based on the SQL Server 'GO' statement. - /// - /// The originating SQL. - /// A list of executable SQL statements. - private static List SplitSql(string sql) + /// A list of executable SQL statement pairs. + /// 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). + /// Note: The 'GO [count]' count syntax is not supported; i.e. will not be parsed correctly and will likely result in an error when executed. + public static List<(string OriginalSql, string CleanSql)> SplitAndCleanSql(TextReader tr) { - var list = new List(); - 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; - } - - /// - /// Reads the SQL command from a and parses out all of the comments. - /// - /// The . - 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; @@ -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; + } + + /// + /// Adds the SQL statement to the list. + /// + 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)); } } } \ No newline at end of file diff --git a/src/DbEx/Resources/Schema_sql.hbs b/src/DbEx/Resources/Schema_sql.hbs index 2174128..2f5528c 100644 --- a/src/DbEx/Resources/Schema_sql.hbs +++ b/src/DbEx/Resources/Schema_sql.hbs @@ -3,9 +3,5 @@ {{! PARAM:Param1=Name }} -- Create schema: [{{lookup Parameters 'Param1'}}] -BEGIN TRANSACTION - CREATE SCHEMA [{{lookup Parameters 'Param1'}}] - AUTHORIZATION [dbo]; - -COMMIT TRANSACTION \ No newline at end of file + AUTHORIZATION [dbo]; \ No newline at end of file diff --git a/src/DbEx/Templates/SqlServer/SpEventOutboxDequeue_sql.hbs b/src/DbEx/Templates/SqlServer/SpEventOutboxDequeue_sql.hbs index 414880d..569ca53 100644 --- a/src/DbEx/Templates/SqlServer/SpEventOutboxDequeue_sql.hbs +++ b/src/DbEx/Templates/SqlServer/SpEventOutboxDequeue_sql.hbs @@ -43,6 +43,7 @@ BEGIN [Source], [Timestamp], [CorrelationId], + [Key], [TenantId], [PartitionKey], [ETag], diff --git a/src/DbEx/Templates/SqlServer/SpEventOutboxEnqueue_sql.hbs b/src/DbEx/Templates/SqlServer/SpEventOutboxEnqueue_sql.hbs index ff25c3d..03ddd85 100644 --- a/src/DbEx/Templates/SqlServer/SpEventOutboxEnqueue_sql.hbs +++ b/src/DbEx/Templates/SqlServer/SpEventOutboxEnqueue_sql.hbs @@ -35,6 +35,7 @@ BEGIN @source NVARCHAR(1023), @timestamp DATETIMEOFFSET, @correlationId NVARCHAR(127), + @key NVARCHAR(1023), @tenantId NVARCHAR(127), @partitionKey NVARCHAR(127), @etag NVARCHAR(127), @@ -43,10 +44,10 @@ BEGIN -- Declare, open, and fetch first event from cursor. DECLARE c CURSOR FORWARD_ONLY - FOR SELECT [EventId], [EventDequeued], [Destination], [Subject], [Action], [Type], [Source], [Timestamp], [CorrelationId], [TenantId], [PartitionKey], [ETag], [Attributes], [Data] FROM @EventList + FOR SELECT [EventId], [EventDequeued], [Destination], [Subject], [Action], [Type], [Source], [Timestamp], [CorrelationId], [Key], [TenantId], [PartitionKey], [ETag], [Attributes], [Data] FROM @EventList OPEN c - FETCH NEXT FROM c INTO @eventId, @eventDequeued, @destination, @subject, @action, @type, @source, @timestamp, @correlationId, @tenantId, @partitionKey, @etag, @attributes, @data + FETCH NEXT FROM c INTO @eventId, @eventDequeued, @destination, @subject, @action, @type, @source, @timestamp, @correlationId, @key, @tenantId, @partitionKey, @etag, @attributes, @data -- Iterate the event(s). WHILE @@FETCH_STATUS = 0 @@ -69,6 +70,7 @@ BEGIN [Source], [Timestamp], [CorrelationId], + [Key], [TenantId], [PartitionKey], [ETag], @@ -85,6 +87,7 @@ BEGIN @source, @timestamp, @correlationId, + @key, @tenantId, @partitionKey, @etag, @@ -93,7 +96,7 @@ BEGIN ) -- Fetch the next event from the cursor. - FETCH NEXT FROM c INTO @eventId, @eventDequeued, @destination, @subject, @action, @type, @source, @timestamp, @correlationId, @tenantId, @partitionKey, @etag, @attributes, @data + FETCH NEXT FROM c INTO @eventId, @eventDequeued, @destination, @subject, @action, @type, @source, @timestamp, @correlationId, @key, @tenantId, @partitionKey, @etag, @attributes, @data END -- Close the cursor. diff --git a/src/DbEx/Templates/SqlServer/TableEventOutboxData_sql.hbs b/src/DbEx/Templates/SqlServer/TableEventOutboxData_sql.hbs index 081b98b..5c45ddf 100644 --- a/src/DbEx/Templates/SqlServer/TableEventOutboxData_sql.hbs +++ b/src/DbEx/Templates/SqlServer/TableEventOutboxData_sql.hbs @@ -15,6 +15,7 @@ CREATE TABLE [{{OutboxSchema}}].[{{OutboxTable}}Data] ( [Source] NVARCHAR(1023) NULL, [Timestamp] DATETIMEOFFSET, [CorrelationId] NVARCHAR(127), + [Key] NVARCHAR(1023) NULL, [TenantId] NVARCHAR(127), [PartitionKey] NVARCHAR(127), [ETag] NVARCHAR(127), diff --git a/src/DbEx/Templates/SqlServer/UdtEventOutbox_sql.hbs b/src/DbEx/Templates/SqlServer/UdtEventOutbox_sql.hbs index e5c9a89..d61f389 100644 --- a/src/DbEx/Templates/SqlServer/UdtEventOutbox_sql.hbs +++ b/src/DbEx/Templates/SqlServer/UdtEventOutbox_sql.hbs @@ -13,6 +13,7 @@ CREATE TYPE [{{OutboxSchema}}].[udt{{OutboxTable}}List] AS TABLE ( [Source] NVARCHAR(1023) NULL, [Timestamp] DATETIMEOFFSET, [CorrelationId] NVARCHAR(127), + [Key] NVARCHAR(1023) NULL, [TenantId] NVARCHAR(127), [PartitionKey] NVARCHAR(127), [ETag] NVARCHAR(127), diff --git a/tests/DbEx.Test.Console/Schema/spGetContact.sql b/tests/DbEx.Test.Console/Schema/spGetContact.sql index 5e2fb39..2e050d7 100644 --- a/tests/DbEx.Test.Console/Schema/spGetContact.sql +++ b/tests/DbEx.Test.Console/Schema/spGetContact.sql @@ -1,7 +1,8 @@ CREATE PROCEDURE [Test].[spGetContact] - @ContactId AS INT + @ContactId AS INT /* this is a comment */ AS BEGIN + -- This is a comment. SET NOCOUNT ON; SELECT * FROM [Test].[Contact] AS [c] WHERE [c].[ContactId] = @ContactId END \ No newline at end of file diff --git a/tests/DbEx.Test.OutboxConsole/DbEx.Test.OutboxConsole.csproj b/tests/DbEx.Test.OutboxConsole/DbEx.Test.OutboxConsole.csproj index 8109905..77be753 100644 --- a/tests/DbEx.Test.OutboxConsole/DbEx.Test.OutboxConsole.csproj +++ b/tests/DbEx.Test.OutboxConsole/DbEx.Test.OutboxConsole.csproj @@ -14,7 +14,7 @@ PreserveNewest - + PreserveNewest diff --git a/tests/DbEx.Test.OutboxConsole/Migrations/102-create-outbox-eventoutboxdata-table.sql b/tests/DbEx.Test.OutboxConsole/Migrations/102-create-outbox-eventoutboxdata-table.sql index 2aaec61..fcc6cb6 100644 --- a/tests/DbEx.Test.OutboxConsole/Migrations/102-create-outbox-eventoutboxdata-table.sql +++ b/tests/DbEx.Test.OutboxConsole/Migrations/102-create-outbox-eventoutboxdata-table.sql @@ -12,6 +12,7 @@ CREATE TABLE [Outbox].[EventOutboxData] ( [Source] NVARCHAR(1023) NULL, [Timestamp] DATETIMEOFFSET, [CorrelationId] NVARCHAR(127), + [Key] NVARCHAR(1023) NULL, [TenantId] NVARCHAR(127), [PartitionKey] NVARCHAR(127), [ETag] NVARCHAR(127), diff --git a/tests/DbEx.Test.OutboxConsole/Schema/Outbox/Stored Procedures/Generated/spEventOutboxDequeue.sql b/tests/DbEx.Test.OutboxConsole/Schema/Outbox/Stored Procedures/Generated/spEventOutboxDequeue.sql index 8016970..3bc4462 100644 --- a/tests/DbEx.Test.OutboxConsole/Schema/Outbox/Stored Procedures/Generated/spEventOutboxDequeue.sql +++ b/tests/DbEx.Test.OutboxConsole/Schema/Outbox/Stored Procedures/Generated/spEventOutboxDequeue.sql @@ -42,6 +42,7 @@ BEGIN [Source], [Timestamp], [CorrelationId], + [Key], [TenantId], [PartitionKey], [ETag], diff --git a/tests/DbEx.Test.OutboxConsole/Schema/Outbox/Stored Procedures/Generated/spEventOutboxEnqueue.sql b/tests/DbEx.Test.OutboxConsole/Schema/Outbox/Stored Procedures/Generated/spEventOutboxEnqueue.sql index f6fad3e..c6130f0 100644 --- a/tests/DbEx.Test.OutboxConsole/Schema/Outbox/Stored Procedures/Generated/spEventOutboxEnqueue.sql +++ b/tests/DbEx.Test.OutboxConsole/Schema/Outbox/Stored Procedures/Generated/spEventOutboxEnqueue.sql @@ -34,6 +34,7 @@ BEGIN @source NVARCHAR(1023), @timestamp DATETIMEOFFSET, @correlationId NVARCHAR(127), + @key NVARCHAR(1023), @tenantId NVARCHAR(127), @partitionKey NVARCHAR(127), @etag NVARCHAR(127), @@ -42,10 +43,10 @@ BEGIN -- Declare, open, and fetch first event from cursor. DECLARE c CURSOR FORWARD_ONLY - FOR SELECT [EventId], [EventDequeued], [Destination], [Subject], [Action], [Type], [Source], [Timestamp], [CorrelationId], [TenantId], [PartitionKey], [ETag], [Attributes], [Data] FROM @EventList + FOR SELECT [EventId], [EventDequeued], [Destination], [Subject], [Action], [Type], [Source], [Timestamp], [CorrelationId], [Key], [TenantId], [PartitionKey], [ETag], [Attributes], [Data] FROM @EventList OPEN c - FETCH NEXT FROM c INTO @eventId, @eventDequeued, @destination, @subject, @action, @type, @source, @timestamp, @correlationId, @tenantId, @partitionKey, @etag, @attributes, @data + FETCH NEXT FROM c INTO @eventId, @eventDequeued, @destination, @subject, @action, @type, @source, @timestamp, @correlationId, @key, @tenantId, @partitionKey, @etag, @attributes, @data -- Iterate the event(s). WHILE @@FETCH_STATUS = 0 @@ -68,6 +69,7 @@ BEGIN [Source], [Timestamp], [CorrelationId], + [Key], [TenantId], [PartitionKey], [ETag], @@ -84,6 +86,7 @@ BEGIN @source, @timestamp, @correlationId, + @key, @tenantId, @partitionKey, @etag, @@ -92,7 +95,7 @@ BEGIN ) -- Fetch the next event from the cursor. - FETCH NEXT FROM c INTO @eventId, @eventDequeued, @destination, @subject, @action, @type, @source, @timestamp, @correlationId, @tenantId, @partitionKey, @etag, @attributes, @data + FETCH NEXT FROM c INTO @eventId, @eventDequeued, @destination, @subject, @action, @type, @source, @timestamp, @correlationId, @key, @tenantId, @partitionKey, @etag, @attributes, @data END -- Close the cursor. diff --git a/tests/DbEx.Test.OutboxConsole/Schema/Outbox/Types/User-Defined Table Types/Generated/udtEventOutboxList.sql b/tests/DbEx.Test.OutboxConsole/Schema/Outbox/Types/User-Defined Table Types/Generated/udtEventOutboxList.sql index fa4ccd0..28537ef 100644 --- a/tests/DbEx.Test.OutboxConsole/Schema/Outbox/Types/User-Defined Table Types/Generated/udtEventOutboxList.sql +++ b/tests/DbEx.Test.OutboxConsole/Schema/Outbox/Types/User-Defined Table Types/Generated/udtEventOutboxList.sql @@ -12,6 +12,7 @@ CREATE TYPE [Outbox].[udtEventOutboxList] AS TABLE ( [Source] NVARCHAR(1023) NULL, [Timestamp] DATETIMEOFFSET, [CorrelationId] NVARCHAR(127), + [Key] NVARCHAR(1023) NULL, [TenantId] NVARCHAR(127), [PartitionKey] NVARCHAR(127), [ETag] NVARCHAR(127), diff --git a/tests/DbEx.Test.OutboxConsole/Script.yaml b/tests/DbEx.Test.OutboxConsole/Scripts/Script.yaml similarity index 100% rename from tests/DbEx.Test.OutboxConsole/Script.yaml rename to tests/DbEx.Test.OutboxConsole/Scripts/Script.yaml diff --git a/tests/DbEx.Test/SqlServerMigratorTest.cs b/tests/DbEx.Test/SqlServerMigratorTest.cs index bf3ad97..864f223 100644 --- a/tests/DbEx.Test/SqlServerMigratorTest.cs +++ b/tests/DbEx.Test/SqlServerMigratorTest.cs @@ -237,45 +237,151 @@ public async Task B120_Execute_Console_Batch_Success() [Test] public void C100_CleanAndSplitSql() { - var stmts = SqlServerMigrator.CleanAndSplitSql(new StringReader("SELECT * FROM Test.Contact; /* end */ GO; SELECT * FROM Test.Contact -- comment")); + var stmts = SqlServerMigrator.SplitAndCleanSql(new StringReader("SELECT * FROM Test.Contact; /* end */ GO; SELECT * FROM Test.Contact -- comment")); Assert.That(stmts, Is.Not.Null); Assert.That(stmts, Has.Count.EqualTo(1)); - Assert.That(stmts[0], Is.EqualTo("SELECT * FROM Test.Contact; GO; SELECT * FROM Test.Contact ")); + Assert.That(stmts[0].OriginalSql, Is.EqualTo(@"SELECT * FROM Test.Contact; /* end */ GO; SELECT * FROM Test.Contact -- comment +")); + Assert.That(stmts[0].CleanSql, Is.EqualTo(@"SELECT * FROM Test.Contact; GO; SELECT * FROM Test.Contact +")); } [Test] public void C110_CleanAndSplitSql() { - var stmts = SqlServerMigrator.CleanAndSplitSql(new StringReader(@"SELECT * FROM Test.Contact; + var stmts = SqlServerMigrator.SplitAndCleanSql(new StringReader(@"SELECT * FROM Test.Contact; /* begin end */ GO SELECT * FROM Test.Contact -- comment")); Assert.That(stmts, Is.Not.Null); Assert.That(stmts, Has.Count.EqualTo(2)); - Assert.That(stmts[0], Is.EqualTo("SELECT * FROM Test.Contact;")); - Assert.That(stmts[1], Is.EqualTo("SELECT * FROM Test.Contact ")); + Assert.That(stmts[0].OriginalSql, Is.EqualTo(@"SELECT * FROM Test.Contact; +/* begin + end */ +")); + Assert.That(stmts[0].CleanSql, Is.EqualTo(@"SELECT * FROM Test.Contact; +")); + Assert.That(stmts[1].OriginalSql, Is.EqualTo(@"SELECT * FROM Test.Contact -- comment +")); + Assert.That(stmts[1].CleanSql, Is.EqualTo(@"SELECT * FROM Test.Contact +")); } [Test] public void C120_CleanAndSplitSql() { - var stmts = SqlServerMigrator.CleanAndSplitSql(new StringReader(@"SELECT 'abc\ + var stmts = SqlServerMigrator.SplitAndCleanSql(new StringReader(@"SELECT 'abc\ def' AS [ColumnResult];")); Assert.That(stmts, Is.Not.Null); Assert.That(stmts, Has.Count.EqualTo(1)); - Assert.That(stmts[0], Is.EqualTo(@"SELECT 'abc\ -def' AS [ColumnResult];")); + Assert.That(stmts[0].OriginalSql, Is.EqualTo(@"SELECT 'abc\ +def' AS [ColumnResult]; +")); + Assert.That(stmts[0].CleanSql, Is.EqualTo(@"SELECT 'abc\ +def' AS [ColumnResult]; +")); } [Test] public void C130_CleanAndSplitSql() { - var stmts = SqlServerMigrator.CleanAndSplitSql(new StringReader(@"SELECT * FROM Test.Contact; + var stmts = SqlServerMigrator.SplitAndCleanSql(new StringReader(@"SELECT * FROM Test.Contact; +GO +")); + Assert.AreEqual(2, stmts.Count); + Assert.That(stmts[0].OriginalSql, Is.EqualTo(@"SELECT * FROM Test.Contact; +")); + Assert.That(stmts[0].CleanSql, Is.EqualTo(@"SELECT * FROM Test.Contact; +")); + Assert.That(stmts[1].OriginalSql, Is.EqualTo(@"")); + Assert.That(stmts[1].CleanSql, Is.EqualTo(@"")); + } + + [Test] + public void C140_CleanAndSplitSql() + { + var stmts = SqlServerMigrator.SplitAndCleanSql(new StringReader(@"SELECT /* * FROM */ Test.Contact /* where */ WHERE; GO +SELECT COUNT(*) -- comment")); + + Assert.AreEqual(2, stmts.Count); + Assert.That(stmts[0].OriginalSql, Is.EqualTo(@"SELECT /* * FROM */ Test.Contact /* where */ WHERE; +")); + Assert.That(stmts[0].CleanSql, Is.EqualTo(@"SELECT Test.Contact WHERE; +")); + Assert.That(stmts[1].OriginalSql, Is.EqualTo(@"SELECT COUNT(*) -- comment +")); + Assert.That(stmts[1].CleanSql, Is.EqualTo(@"SELECT COUNT(*) +")); + } + + [Test] + public void C150_CleanAndSplitSql() + { + var stmts = SqlServerMigrator.SplitAndCleanSql(new StringReader(@"SELECT /* * FROM */ Test.Contact /* where */ WHERE; +GO +GO +SELECT COUNT(*) -- comment")); + + Assert.AreEqual(3, stmts.Count); + Assert.That(stmts[0].OriginalSql, Is.EqualTo(@"SELECT /* * FROM */ Test.Contact /* where */ WHERE; +")); + Assert.That(stmts[0].CleanSql, Is.EqualTo(@"SELECT Test.Contact WHERE; +")); + Assert.That(stmts[1].OriginalSql, Is.EqualTo(@"")); + Assert.That(stmts[1].CleanSql, Is.EqualTo(@"")); + Assert.That(stmts[2].OriginalSql, Is.EqualTo(@"SELECT COUNT(*) -- comment +")); + Assert.That(stmts[2].CleanSql, Is.EqualTo(@"SELECT COUNT(*) +")); + } + + [Test] + public void C160_CleanAndSplitSql() + { + var stmts = SqlServerMigrator.SplitAndCleanSql(new StringReader(@"SELECT /* * FROM */ Test.Contact /* where */ WHERE; +GO +-- Bananas +GO +SELECT COUNT(*) -- comment")); + + Assert.AreEqual(3, stmts.Count); + Assert.That(stmts[0].OriginalSql, Is.EqualTo(@"SELECT /* * FROM */ Test.Contact /* where */ WHERE; +")); + Assert.That(stmts[0].CleanSql, Is.EqualTo(@"SELECT Test.Contact WHERE; +")); + Assert.That(stmts[1].OriginalSql, Is.EqualTo(@"-- Bananas +")); + Assert.That(stmts[1].CleanSql, Is.EqualTo(@"")); + Assert.That(stmts[2].OriginalSql, Is.EqualTo(@"SELECT COUNT(*) -- comment +")); + Assert.That(stmts[2].CleanSql, Is.EqualTo(@"SELECT COUNT(*) +")); + } + + [Test] + public void C170_CleanAndSplitSql() + { + var stmts = SqlServerMigrator.SplitAndCleanSql(new StringReader(@"SELECT /* * FROM */ Test.Contact /* where */ WHERE; +GO +/* */ +GO +SELECT COUNT(*) -- comment")); + + Assert.AreEqual(3, stmts.Count); + Assert.That(stmts[0].OriginalSql, Is.EqualTo(@"SELECT /* * FROM */ Test.Contact /* where */ WHERE; +")); + Assert.That(stmts[0].CleanSql, Is.EqualTo(@"SELECT Test.Contact WHERE; +")); + Assert.That(stmts[1].OriginalSql, Is.EqualTo(@"/* */ +")); + Assert.That(stmts[1].CleanSql, Is.EqualTo(@"")); + Assert.That(stmts[2].OriginalSql, Is.EqualTo(@"SELECT COUNT(*) -- comment +")); + Assert.That(stmts[2].CleanSql, Is.EqualTo(@"SELECT COUNT(*) ")); - Assert.That(stmts[0], Is.EqualTo("SELECT * FROM Test.Contact;")); } } } \ No newline at end of file diff --git a/tests/DbEx.Test/SqlServerOutboxTest.cs b/tests/DbEx.Test/SqlServerOutboxTest.cs index df116ea..f74b86d 100644 --- a/tests/DbEx.Test/SqlServerOutboxTest.cs +++ b/tests/DbEx.Test/SqlServerOutboxTest.cs @@ -21,7 +21,7 @@ public class SqlServerOutboxTest public async Task Setup() { var cs = UnitTest.GetConfig("DbEx_").GetConnectionString("ConsoleDb"); - var l = UnitTest.GetLogger(); + var l = UnitTest.GetLogger(); var m = new SqlServerMigrator(cs, Migration.MigrationCommand.DropAndAll, l, typeof(Console.Program).Assembly, typeof(DbEx.Test.OutboxConsole.Program).Assembly); m.ParserArgs.Parameters.Add("DefaultName", "Bazza"); m.ParserArgs.RefDataColumnDefaults.Add("SortOrder", i => 1); @@ -32,7 +32,7 @@ public async Task Setup() public async Task A100_EnqueueDequeue() { var cs = UnitTest.GetConfig("DbEx_").GetConnectionString("ConsoleDb"); - var l = UnitTest.GetLogger(); + var l = UnitTest.GetLogger(); using var db = new SqlServerDatabase(() => new SqlConnection(cs)); await db.SqlStatement("DELETE FROM [Outbox].[EventOutbox]").NonQueryAsync().ConfigureAwait(false); @@ -47,6 +47,7 @@ public async Task A100_EnqueueDequeue() Source = new Uri("/source", UriKind.Relative), CorrelationId = "corrid", TenantId = "tenant", + Key = "kiwi", Destination = "queue-name", ETag = "etag", PartitionKey = "partition-key", @@ -74,6 +75,7 @@ public async Task A100_EnqueueDequeue() Assert.AreEqual(new Uri("/source", UriKind.Relative), e.Source); Assert.AreEqual("corrid", e.CorrelationId); Assert.AreEqual("tenant", e.TenantId); + Assert.AreEqual("kiwi", e.Key); Assert.AreEqual("queue-name", e.Destination); Assert.AreEqual("etag", e.ETag); Assert.AreEqual("partition-key", e.PartitionKey); @@ -89,7 +91,7 @@ public async Task A100_EnqueueDequeue() public async Task A110_EnqueueDequeue_MaxDequeueSize() { var cs = UnitTest.GetConfig("DbEx_").GetConnectionString("ConsoleDb"); - var l = UnitTest.GetLogger(); + var l = UnitTest.GetLogger(); using var db = new SqlServerDatabase(() => new SqlConnection(cs)); await db.SqlStatement("DELETE FROM [Outbox].[EventOutbox]").NonQueryAsync().ConfigureAwait(false); @@ -129,7 +131,7 @@ await eoe.SendAsync(new EventSendData[] public async Task A120_EnqueueDequeue_PartitionKey_Destination_Selection() { var cs = UnitTest.GetConfig("DbEx_").GetConnectionString("ConsoleDb"); - var l = UnitTest.GetLogger(); + var l = UnitTest.GetLogger(); using var db = new SqlServerDatabase(() => new SqlConnection(cs)); await db.SqlStatement("DELETE FROM [Outbox].[EventOutbox]").NonQueryAsync().ConfigureAwait(false); @@ -184,7 +186,7 @@ await eoe.SendAsync(new EventSendData[] public async Task B100_EnqueueDequeue_PrimarySender_Success() { var cs = UnitTest.GetConfig("DbEx_").GetConnectionString("ConsoleDb"); - var l = UnitTest.GetLogger(); + var l = UnitTest.GetLogger(); using var db = new SqlServerDatabase(() => new SqlConnection(cs)); await db.SqlStatement("DELETE FROM [Outbox].[EventOutbox]").NonQueryAsync().ConfigureAwait(false); @@ -211,7 +213,7 @@ public async Task B100_EnqueueDequeue_PrimarySender_Success() public async Task B110_EnqueueDequeue_PrimarySender_Error() { var cs = UnitTest.GetConfig("DbEx_").GetConnectionString("ConsoleDb"); - var l = UnitTest.GetLogger(); + var l = UnitTest.GetLogger(); using var db = new SqlServerDatabase(() => new SqlConnection(cs)); await db.SqlStatement("DELETE FROM [Outbox].[EventOutbox]").NonQueryAsync().ConfigureAwait(false);