From 96c09e5dac6dab879a4d689b1c30aa5e045084bc Mon Sep 17 00:00:00 2001 From: "Eric Sibly [chullybun]" Date: Mon, 1 Jul 2024 13:54:23 -0700 Subject: [PATCH] v2.5.8 (#57) - *Fixed:* SQL Server `data` merge statement fixed to include the `TenantIdColumn` where applicable to avoid possible duplicate key. --- CHANGELOG.md | 3 +++ Common.targets | 2 +- src/DbEx.SqlServer/Resources/DatabaseData_sql.hbs | 2 +- tests/DbEx.Test.Console/Data/Data.yaml | 7 ++++++- tests/DbEx.Test.Console/Data/Data2.yaml | 7 +++++++ tests/DbEx.Test.Console/DbEx.Test.Console.csproj | 2 ++ .../Migrations/007-create-test-status-table.sql | 11 +++++++++++ 7 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 tests/DbEx.Test.Console/Data/Data2.yaml create mode 100644 tests/DbEx.Test.Console/Migrations/007-create-test-status-table.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index 7315c7d..c4c8ba9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ Represents the **NuGet** versions. +## v2.5.8 +- *Fixed:* SQL Server `data` merge statement fixed to include the `TenantIdColumn` where applicable to avoid possible duplicate key. + ## v2.5.7 - *Fixed:* Corrected issue introduced in version `2.5.5` where some strings were being incorrectly converted to a guid. diff --git a/Common.targets b/Common.targets index 7c4f606..c32e530 100644 --- a/Common.targets +++ b/Common.targets @@ -1,6 +1,6 @@ - 2.5.7 + 2.5.8 preview Avanade Avanade diff --git a/src/DbEx.SqlServer/Resources/DatabaseData_sql.hbs b/src/DbEx.SqlServer/Resources/DatabaseData_sql.hbs index fa04e95..d3d62e8 100644 --- a/src/DbEx.SqlServer/Resources/DatabaseData_sql.hbs +++ b/src/DbEx.SqlServer/Resources/DatabaseData_sql.hbs @@ -15,7 +15,7 @@ INSERT INTO #temp ({{#each Columns}}[{{Name}}]{{#unless @last}}, {{/unless}}{{/e MERGE INTO [{{Schema}}].[{{Name}}] WITH (HOLDLOCK) as [t] USING (SELECT {{#each Columns}}[{{Name}}]{{#unless @last}}, {{/unless}}{{/each}} FROM #temp) AS [s] - ON ({{#if IsRefData}}[s].[{{DbTable.RefDataCodeColumn.Name}}] = [t].[{{DbTable.RefDataCodeColumn.Name}}]{{else}}{{#each PrimaryKeyColumns}}{{#unless @first}} ON {{/unless}}[s].[{{Name}}] = [t].[{{Name}}]{{/each}}{{/if}}) + ON ({{#if IsRefData}}[s].[{{DbTable.RefDataCodeColumn.Name}}] = [t].[{{DbTable.RefDataCodeColumn.Name}}]{{else}}{{#each PrimaryKeyColumns}}{{#unless @first}} ON {{/unless}}[s].[{{Name}}] = [t].[{{Name}}]{{/each}}{{/if}}{{#ifval DbTable.TenantIdColumn}} AND [s].[{{DbTable.TenantIdColumn.Name}}] = [t].[{{DbTable.TenantIdColumn.Name}}]{{/ifval}}) WHEN MATCHED AND EXISTS ( SELECT {{#each MergeMatchColumns}}[s].[{{Name}}]{{#unless @last}}, {{/unless}}{{/each}} EXCEPT diff --git a/tests/DbEx.Test.Console/Data/Data.yaml b/tests/DbEx.Test.Console/Data/Data.yaml index 2650701..6109e24 100644 --- a/tests/DbEx.Test.Console/Data/Data.yaml +++ b/tests/DbEx.Test.Console/Data/Data.yaml @@ -10,4 +10,9 @@ - { PersonId: ^88, Name: '^(DbEx.Test.Console.RuntimeValues.Name, DbEx.Test.Console)' } - { Name: '^(DefaultName)', AddressJson: { Street: "Main St", City: "Maine" }, NicknamesJson: ["Gaz", "Baz"] } - $Gender: - - X: Not specified \ No newline at end of file + - X: Not specified +- $Status: + - { Code: A, Text: Active, TenantId: ^88 } + - { Code: I, Text: Inactive, TenantId: ^88 } + - { Code: A, Text: Active, TenantId: ^99 } + - { Code: I, Text: Inactive, TenantId: ^99 } \ No newline at end of file diff --git a/tests/DbEx.Test.Console/Data/Data2.yaml b/tests/DbEx.Test.Console/Data/Data2.yaml new file mode 100644 index 0000000..b57f5eb --- /dev/null +++ b/tests/DbEx.Test.Console/Data/Data2.yaml @@ -0,0 +1,7 @@ +Test: +# Specified again, merge should not result in duplicate error +- $Status: + - { Code: A, Text: Active, TenantId: ^88 } + - { Code: I, Text: Inactive, TenantId: ^88 } + - { Code: A, Text: Active, TenantId: ^99 } + - { Code: I, Text: Inactive, TenantId: ^99 } \ No newline at end of file diff --git a/tests/DbEx.Test.Console/DbEx.Test.Console.csproj b/tests/DbEx.Test.Console/DbEx.Test.Console.csproj index 6e8d70b..a6e5c67 100644 --- a/tests/DbEx.Test.Console/DbEx.Test.Console.csproj +++ b/tests/DbEx.Test.Console/DbEx.Test.Console.csproj @@ -13,7 +13,9 @@ + + diff --git a/tests/DbEx.Test.Console/Migrations/007-create-test-status-table.sql b/tests/DbEx.Test.Console/Migrations/007-create-test-status-table.sql new file mode 100644 index 0000000..e455aec --- /dev/null +++ b/tests/DbEx.Test.Console/Migrations/007-create-test-status-table.sql @@ -0,0 +1,11 @@ +CREATE TABLE [Test].[Status] ( + [StatusId] UNIQUEIDENTIFIER NOT NULL DEFAULT (NEWSEQUENTIALID()) PRIMARY KEY, + [Code] NVARCHAR (50) NOT NULL, + [Text] VARCHAR (256) NOT NULL, + [CreatedBy] NVARCHAR (50) NULL, + [CreatedDate] DATETIME2 NULL, + [UpdatedBy] NVARCHAR (50) NULL, + [UpdatedDate] DATETIME2 NULL, + [TenantId] NVARCHAR(50) NOT NULL, + UNIQUE ([TenantId], [Code]) +) \ No newline at end of file