Skip to content

Commit 65c775f

Browse files
Set-DbaDbCompression - Fix indexed view metadata for compression output (review of #10248)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4c34619 commit 65c775f

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

docs/trackers/features/commit-bug-review-TRACKER.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Find real bugs (logic errors, null refs, incorrect behavior) and fix them. Skip
6969
| 6084ecbe5 | Get-DbaLastBackup - Add -ExcludeReplica switch for AlwaysOn preferred backup replica filtering (#10240) | DONE | Fixed empty filtered-set fallback that queried all backup history; added unit regression test. |
7070
| fe26c8764 | Export-DbaInstance - Wire up IncludeDbMasterKey to export certs and master keys (#10251) | DONE | Fixed FileInfo output contract and staged remote cert/master-key exports back into the local export folder; added unit regression test. |
7171
| 0ee03fc32 | New-DbaAgentJobStep - Fix OnFailAction ValidateSet order to match actual default (#10244) | DONE | Reviewed parameter metadata-only ValidateSet reorder; no bugs found. |
72-
| 63c906f9d | Set-DbaDbCompression - Add SortInTempDB parameter and fix views T-SQL bug (#10248) | PENDING | |
72+
| 63c906f9d | Set-DbaDbCompression - Add SortInTempDB parameter and fix views T-SQL bug (#10248) | DONE | Fixed indexed-view output/error metadata to use the view name instead of a stale table reference; added integration regression test. |
7373
| 4d1a9d80c | v2.7.27 | DONE | version bump - skip |
7474
| 232395207 | Set-DbaPrivilege, Get-DbaPrivilege - Add CreateGlobalObjects privilege support (#10235) | PENDING | |
7575
| 099624061 | Get-DbaDbRestoreHistory - Add BackupStartDate, StopAt, and LastRestorePoint columns (#10249) | PENDING | |

public/Set-DbaDbCompression.ps1

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ function Set-DbaDbCompression {
360360
}
361361
}
362362
foreach ($index in $($server.Databases[$($db.name)].Views | Where-Object { $_.Indexes }).Indexes) {
363+
$parentView = $index.Parent
363364
foreach ($p in $($index.PhysicalPartitions | Where-Object { $_.DataCompression -ne $CompressionType })) {
364365
Write-Message -Level Verbose -Message "Compressing $($index.IndexType) $($index.Name) Partition $($p.PartitionNumber)"
365366
try {
@@ -383,15 +384,15 @@ function Set-DbaDbCompression {
383384
$index.Rebuild()
384385
}
385386
} catch {
386-
Stop-Function -Message "Compression failed for $instance - $db - table $($obj.Schema).$($obj.Name) - index $($index.Name) - partition $($p.PartitionNumber)" -Target $db -ErrorRecord $_ -Continue
387+
Stop-Function -Message "Compression failed for $instance - $db - view $($parentView.Schema).$($parentView.Name) - index $($index.Name) - partition $($p.PartitionNumber)" -Target $db -ErrorRecord $_ -Continue
387388
}
388389
[PSCustomObject]@{
389390
ComputerName = $server.ComputerName
390391
InstanceName = $server.ServiceName
391392
SqlInstance = $server.DomainInstanceName
392393
Database = $db.Name
393-
Schema = $obj.Schema
394-
TableName = $obj.Name
394+
Schema = $parentView.Schema
395+
TableName = $parentView.Name
395396
IndexName = $index.Name
396397
Partition = $p.PartitionNumber
397398
IndexID = $index.Id

tests/Set-DbaDbCompression.Tests.ps1

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
22
param(
3-
$ModuleName = "dbatools",
3+
$ModuleName = "dbatools",
44
$CommandName = "Set-DbaDbCompression",
55
$PSDefaultParameterValues = $TestConfig.Defaults
66
)
@@ -35,12 +35,29 @@ Describe $CommandName -Tag IntegrationTests {
3535
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
3636

3737
$dbName = "dbatoolsci_test_$(Get-Random)"
38+
$indexedViewName = "dbatoolsci_syscolview"
39+
$indexedViewIndexName = "CL_dbatoolsci_syscolview"
3840
$server = Connect-DbaInstance -SqlInstance $TestConfig.InstanceSingle
3941
$null = $server.Query("Create Database [$dbName]")
4042
$null = $server.Query("select * into syscols from sys.all_columns
4143
select * into sysallparams from sys.all_parameters
4244
create clustered index CL_sysallparams on sysallparams (object_id)
4345
create nonclustered index NC_syscols on syscols (precision) include (collation_name)", $dbName)
46+
$null = $server.Query("SET ANSI_NULLS ON;
47+
SET QUOTED_IDENTIFIER ON;
48+
EXEC sys.sp_executesql N'CREATE VIEW dbo.[$indexedViewName]
49+
WITH SCHEMABINDING
50+
AS
51+
SELECT object_id, column_id
52+
FROM dbo.syscols'", $dbName)
53+
$null = $server.Query("SET ANSI_NULLS ON;
54+
SET ANSI_PADDING ON;
55+
SET ANSI_WARNINGS ON;
56+
SET ARITHABORT ON;
57+
SET CONCAT_NULL_YIELDS_NULL ON;
58+
SET QUOTED_IDENTIFIER ON;
59+
SET NUMERIC_ROUNDABORT OFF;
60+
CREATE UNIQUE CLUSTERED INDEX [$indexedViewIndexName] ON dbo.[$indexedViewName] (object_id, column_id)", $dbName)
4461

4562
# Get InputObject for testing
4663
$inputObject = Test-DbaDbCompression -SqlInstance $TestConfig.InstanceSingle -Database $dbName
@@ -161,4 +178,20 @@ Describe $CommandName -Tag IntegrationTests {
161178
}
162179
}
163180
}
164-
}
181+
182+
Context "Command returns indexed view metadata when rebuilding to None" {
183+
BeforeAll {
184+
$null = Set-DbaDbCompression -SqlInstance $TestConfig.InstanceSingle -Database $dbName -CompressionType Page
185+
$indexedViewResults = Set-DbaDbCompression -SqlInstance $TestConfig.InstanceSingle -Database $dbName -CompressionType None |
186+
Where-Object IndexName -eq $indexedViewIndexName
187+
}
188+
189+
It "Should return the indexed view schema and name" {
190+
$indexedViewResults | Should -Not -BeNullOrEmpty
191+
foreach ($row in $indexedViewResults) {
192+
$row.Schema | Should -Be "dbo"
193+
$row.TableName | Should -Be $indexedViewName
194+
}
195+
}
196+
}
197+
}

0 commit comments

Comments
 (0)