Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get dba db sequence #7296

Merged
merged 5 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 53 additions & 14 deletions functions/Get-DbaDbSequence.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,36 +60,75 @@ function Get-DbaDbSequence {
PS C:\> Get-DbaDatabase -SqlInstance sqldev01 -Database TestDB | Get-DbaDbSequence -Name TestSequence -Schema TestSchema

Using a pipeline this command finds the sequence named TestSchema.TestSequence in the TestDB database on the sqldev01 instance.

.EXAMPLE
PS C:\> Get-DbaDbSequence -SqlInstance localhost

Finds all the sequences on the localhost instance.

.EXAMPLE
PS C:\> Get-DbaDbSequence -SqlInstance localhost -Database db

Finds all the sequences in the db database on the localhost instance.

.EXAMPLE
PS C:\> Get-DbaDbSequence -SqlInstance localhost -Name seq

Finds all the sequences named seq on the localhost instance.

.EXAMPLE
PS C:\> Get-DbaDbSequence -SqlInstance localhost -Schema sch

Finds all the sequences in the sch schema on the localhost instance.

#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
param (
[DbaInstanceParameter[]]$SqlInstance,
[parameter(ValueFromPipeline)]
[DbaInstance[]]$SqlInstance,
[PSCredential]$SqlCredential,
[string[]]$Database,
[Parameter(Mandatory)]
[string]$Name,
[string]$Schema = 'dbo',
[string[]]$Name,
[string[]]$Schema,
[parameter(ValueFromPipeline)]
[Microsoft.SqlServer.Management.Smo.Database[]]$InputObject,
[switch]$EnableException
)
process {

if ((Test-Bound -ParameterName SqlInstance) -and (Test-Bound -Not -ParameterName Database)) {
Stop-Function -Message "Database is required when SqlInstance is specified"
if (-not $InputObject -and -not $SqlInstance) {
Stop-Function -Message "You must pipe in a database or specify a SqlInstance"
return
}

# caller specified the instance info
foreach ($instance in $SqlInstance) {
foreach ($db in (Get-DbaDatabase -SqlInstance $instance -SqlCredential $SqlCredential -Database $Database)) {
$db.Sequences | Where-Object { $_.Schema -eq $Schema -and $_.Name -eq $Name }
}
if ($SqlInstance) {
$InputObject += Get-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database -ExcludeDatabase $ExcludeDatabase
}

# caller has piped in one or more databases
foreach ($db in $InputObject) {
$db.Sequences | Where-Object { $_.Schema -eq $Schema -and $_.Name -eq $Name }
if ($db.IsAccessible -eq $false) {
continue
}
$server = $db.Parent
Write-Message -Level 'Verbose' -Message "Getting Database Sequences for $db on $server"

$dbSequences = $db.Sequences

if ($Name) {
$dbSequences = $dbSequences | Where-Object { $_.Name -in $Name }
}

if ($Schema) {
$dbSequences = $dbSequences | Where-Object { $_.Schema -in $Schema }
}

foreach ($dbSequence in $dbSequences) {
Add-Member -Force -InputObject $dbSequence -MemberType NoteProperty -Name ComputerName -Value $server.ComputerName
Add-Member -Force -InputObject $dbSequence -MemberType NoteProperty -Name InstanceName -Value $server.ServiceName
Add-Member -Force -InputObject $dbSequence -MemberType NoteProperty -Name SqlInstance -Value $server.DomainInstanceName
Add-Member -Force -InputObject $dbSequence -MemberType NoteProperty -Name Database -Value $db.Name

Select-DefaultView -InputObject $dbSequence -Property "ComputerName", "InstanceName", "SqlInstance", "Database", "Schema", "Name", "DataType", "StartValue", "IncrementValue"
}
}
}
}
57 changes: 47 additions & 10 deletions tests/Get-DbaDbSequence.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,65 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
$instance2 = Connect-DbaInstance -SqlInstance $script:instance2
$null = Get-DbaProcess -SqlInstance $instance2 | Where-Object Program -match dbatools | Stop-DbaProcess -Confirm:$false
$newDbName = "dbatoolsci_newdb_$random"
$newDb = New-DbaDatabase -SqlInstance $instance2 -Name $newDbName
$newDbName2 = "dbatoolsci_newdb2_$random"
$newDb, $newDb2 = New-DbaDatabase -SqlInstance $instance2 -Name $newDbName, $newDbName2

$sequence = New-DbaDbSequence -SqlInstance $instance2 -Database $newDbName -Name "Sequence1_$random" -Schema "Schema_$random"
$sequence2 = New-DbaDbSequence -SqlInstance $instance2 -Database $newDbName -Name "Sequence2_$random" -Schema "Schema2_$random"
$sequence3 = New-DbaDbSequence -SqlInstance $instance2 -Database $newDbName -Name "Sequence1_$random" -Schema "Schema2_$random"
$sequence4 = New-DbaDbSequence -SqlInstance $instance2 -Database $newDbName -Name "Sequence2_$random" -Schema "Schema_$random"
$sequence5 = New-DbaDbSequence -SqlInstance $instance2 -Database $newDbName2 -Name "Sequence1_$random" -Schema "Schema_$random"
}

AfterAll {
$null = $newDb | Remove-DbaDatabase -Confirm:$false
$null = $newDb, $newDb2 | Remove-DbaDatabase -Confirm:$false
}

Context "commands work as expected" {

It "validates required Database param" {
$sequence = Get-DbaDbSequence -SqlInstance $instance2 -Name SequenceTest -ErrorVariable error
$sequence | Should -BeNullOrEmpty
$error | Should -Match "Database is required when SqlInstance is specified"
It "finds a sequence on an instance" {
$sequence = Get-DbaDbSequence -SqlInstance $instance2
$sequence.Count | Should -Be 5
}

It "finds a sequence" {
It "finds a sequence in a single database" {
$sequence = Get-DbaDbSequence -SqlInstance $instance2 -Database $newDbName
$sequence.Parent.Name | Select-Object -Unique | Should -Be $newDbName
$sequence.Count | Should -Be 4
}

It "finds a sequence in a single database by schema only" {
$sequence = Get-DbaDbSequence -SqlInstance $instance2 -Database $newDbName -Schema "Schema2_$random"
$sequence.Parent.Name | Select-Object -Unique | Should -Be $newDbName
$sequence.Schema | Select-Object -Unique | Should -Be "Schema2_$random"
$sequence.Count | Should -Be 2
}

It "finds a sequence in a single database by schema and by name" {
$sequence = Get-DbaDbSequence -SqlInstance $instance2 -Database $newDbName -Name "Sequence1_$random" -Schema "Schema_$random"
$sequence.Name | Should -Be "Sequence1_$random"
$sequence.Schema | Should -Be "Schema_$random"
$sequence.Parent.Name | Should -Be $newDbName
$sequence.Parent.Name | Select-Object -Unique | Should -Be $newDbName
$sequence.Name | Select-Object -Unique | Should -Be "Sequence1_$random"
$sequence.Schema | Select-Object -Unique | Should -Be "Schema_$random"
$sequence.Count | Should -Be 1
}

It "finds a sequence on an instance by name only" {
$sequence = Get-DbaDbSequence -SqlInstance $instance2 -Name "Sequence1_$random"
$sequence.Name | Select-Object -Unique | Should -Be "Sequence1_$random"
$sequence.Count | Should -Be 3
}

It "finds a sequence on an instance by schema only" {
$sequence = Get-DbaDbSequence -SqlInstance $instance2 -Schema "Schema2_$random"
$sequence.Schema | Select-Object -Unique | Should -Be "Schema2_$random"
$sequence.Count | Should -Be 2
}

It "finds a sequence on an instance by schema and name" {
$sequence = Get-DbaDbSequence -SqlInstance $instance2 -Schema "Schema_$random" -Name "Sequence1_$random"
$sequence.Schema | Select-Object -Unique | Should -Be "Schema_$random"
$sequence.Name | Select-Object -Unique | Should -Be "Sequence1_$random"
$sequence.Count | Should -Be 2
}

It "supports piping databases" {
Expand Down