Skip to content

Commit

Permalink
✅ Add merge export tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brianary committed Mar 17, 2024
1 parent 41f091a commit 653098a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
10 changes: 7 additions & 3 deletions test/Export-TableMerge.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ Describe 'Export-TableMerge' -Tag Export-TableMerge -Skip:$skip {
$server = if(!!$env:TestConnectionString) {Connect-DbaInstance -SqlInstance $env:TestConnectionString}
}
Context 'Exports table data' -Tag ExportTableMerge,Export,TableMerge,Database {
It "Exports AdventureWorks HumanResources.Department table data" -Skip:$(!$env:TestConnectionString) {
$result = Join-Path $datadir HumanResources.Department.merge.sql |Get-Item |Get-Content -Raw
It "Exports AdventureWorks HumanResources.Department table data" -Skip:$(!$env:TestConnectionString) -TestCases @(
@{ Schema = 'HumanResources'; Table = 'Department' }
@{ Schema = 'Person'; Table = 'PhoneNumberType' }
@{ Schema = 'Production'; Table = 'ProductModelIllustration' }
) {
$result = Join-Path $datadir "${Schema}.${Table}.merge.sql" |Get-Item |Get-Content -Raw
$result = $result.TrimEnd()
Get-DbaDbTable -SqlInstance $server -Schema HumanResources -Table Department |
Get-DbaDbTable -SqlInstance $server -Schema $Schema -Table $Table |
Export-TableMerge.ps1 |
Should -BeExactly $result
}
Expand Down
22 changes: 22 additions & 0 deletions test/data/Person.PhoneNumberType.merge.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
if exists (select * from information_schema.columns where table_schema = 'Person' and table_name = 'PhoneNumberType'
and columnproperty(object_id(table_name), column_name,'IsIdentity') = 1)
set identity_insert [Person].[PhoneNumberType] on;

merge [Person].[PhoneNumberType] as target
using ( values
(1, 'Cell', '2017-12-13 13:19:22.27300'),
(2, 'Home', '2017-12-13 13:19:22.27300'),
(3, 'Work', '2017-12-13 13:19:22.27300')
) as source ([PhoneNumberTypeID], [Name], [ModifiedDate])
on source.[PhoneNumberTypeID] = target.[PhoneNumberTypeID]
when matched then
update set [Name] = source.[Name],
[ModifiedDate] = source.[ModifiedDate]
when not matched by target then
insert ([PhoneNumberTypeID], [Name], [ModifiedDate])
values (source.[PhoneNumberTypeID], source.[Name], source.[ModifiedDate])
when not matched by source then delete ;

if exists (select * from information_schema.columns where table_schema = 'Person' and table_name = 'PhoneNumberType'
and columnproperty(object_id(table_name), column_name,'IsIdentity') = 1)
set identity_insert [Person].[PhoneNumberType] off;
26 changes: 26 additions & 0 deletions test/data/Production.ProductModelIllustration.merge.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
if exists (select * from information_schema.columns where table_schema = 'Production' and table_name = 'ProductModelIllustration'
and columnproperty(object_id(table_name), column_name,'IsIdentity') = 1)
set identity_insert [Production].[ProductModelIllustration] on;

merge [Production].[ProductModelIllustration] as target
using ( values
(7, 3, '2014-01-09 14:41:02.16700'),
(10, 3, '2014-01-09 14:41:02.16700'),
(47, 4, '2014-01-09 14:41:02.18300'),
(47, 5, '2014-01-09 14:41:02.18300'),
(48, 4, '2014-01-09 14:41:02.18300'),
(48, 5, '2014-01-09 14:41:02.18300'),
(67, 6, '2014-01-09 14:41:02.20000')
) as source ([ProductModelID], [IllustrationID], [ModifiedDate])
on source.[ProductModelID] = target.[ProductModelID]
and source.[IllustrationID] = target.[IllustrationID]
when matched then
update set [ModifiedDate] = source.[ModifiedDate]
when not matched by target then
insert ([ProductModelID], [IllustrationID], [ModifiedDate])
values (source.[ProductModelID], source.[IllustrationID], source.[ModifiedDate])
when not matched by source then delete ;

if exists (select * from information_schema.columns where table_schema = 'Production' and table_name = 'ProductModelIllustration'
and columnproperty(object_id(table_name), column_name,'IsIdentity') = 1)
set identity_insert [Production].[ProductModelIllustration] off;

0 comments on commit 653098a

Please sign in to comment.