Skip to content

Commit a95c81a

Browse files
committed
Fixed check fo tables
1 parent 181986c commit a95c81a

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

functions/Set-PSDCConfiguration.ps1

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@
8181
if (-not $Database) {
8282
$Database = "PSDatabaseClone"
8383
}
84+
85+
# Set the flag for the new database
86+
[bool]$newDatabase = $false
8487
}
8588

8689
process {
@@ -105,10 +108,14 @@
105108
}
106109

107110
# Get the databases from the instance
108-
$databases = Get-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential
111+
#$databases = Get-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential
109112

110113
# Check if the database exists
111-
if ($databases.Name -notcontains $Database) {
114+
if ($server.Databases.Name -notcontains $Database) {
115+
116+
# Set the flag
117+
$newDatabase = $true
118+
112119
try {
113120
# Setup the query to create the database
114121
$query = "CREATE DATABASE [$Database]"
@@ -122,27 +129,38 @@
122129
Stop-PSFFunction -Message "Couldn't create database $Database on $SqlInstance" -ErrorRecord $_ -Target $SqlInstance
123130
}
124131
}
132+
else{
133+
# Check if there are any user objects already in the database
134+
$newDatabase = ($server.Databases[$Database].Tables.Count -eq 0)
135+
}
125136

126137
# Setup the path to the sql file
127-
try {
128-
$path = "$($MyInvocation.MyCommand.Module.ModuleBase)\internal\scripts\database.sql"
129-
$query = [System.IO.File]::ReadAllText($path)
130-
}
131-
catch {
132-
Stop-PSFFunction -Message "Couldn't find database script. Make sure you have a valid installation of the module" -ErrorRecord $_ -Target $SqlInstance
133-
}
138+
if ($newDatabase) {
139+
try {
140+
$path = "$($MyInvocation.MyCommand.Module.ModuleBase)\internal\scripts\database.sql"
141+
$query = [System.IO.File]::ReadAllText($path)
134142

135-
# Create the objects
136-
try {
137-
Write-PSFMessage -Message "Creating database objects" -Level Verbose
143+
# Create the objects
144+
try {
145+
Write-PSFMessage -Message "Creating database objects" -Level Verbose
138146

139-
# Executing the query
140-
Invoke-DbaSqlQuery -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database -Query $query
147+
# Executing the query
148+
Invoke-DbaSqlQuery -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database -Query $query
149+
}
150+
catch {
151+
Stop-PSFFunction -Message "Couldn't create database objects" -ErrorRecord $_ -Target $SqlInstance
152+
}
153+
}
154+
catch {
155+
Stop-PSFFunction -Message "Couldn't find database script. Make sure you have a valid installation of the module" -ErrorRecord $_ -Target $SqlInstance
156+
}
141157
}
142-
catch {
143-
Stop-PSFFunction -Message "Couldn't create database objects" -ErrorRecord $_ -Target $SqlInstance
158+
else{
159+
Write-PSFMessage -Message "Database already contains objects" -Level Verbose
144160
}
145161

162+
163+
146164
# Writing the setting to the configuration file
147165
Write-PSFMessage -Message "Registering config values" -Level Verbose
148166
Set-PSFConfig -Module PSDatabaseClone -Name database.server -Value $SqlInstance -Initialize -Validation string

0 commit comments

Comments
 (0)