|
81 | 81 | if (-not $Database) {
|
82 | 82 | $Database = "PSDatabaseClone"
|
83 | 83 | }
|
| 84 | + |
| 85 | + # Set the flag for the new database |
| 86 | + [bool]$newDatabase = $false |
84 | 87 | }
|
85 | 88 |
|
86 | 89 | process {
|
|
105 | 108 | }
|
106 | 109 |
|
107 | 110 | # Get the databases from the instance
|
108 |
| - $databases = Get-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential |
| 111 | + #$databases = Get-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential |
109 | 112 |
|
110 | 113 | # 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 | + |
112 | 119 | try {
|
113 | 120 | # Setup the query to create the database
|
114 | 121 | $query = "CREATE DATABASE [$Database]"
|
|
122 | 129 | Stop-PSFFunction -Message "Couldn't create database $Database on $SqlInstance" -ErrorRecord $_ -Target $SqlInstance
|
123 | 130 | }
|
124 | 131 | }
|
| 132 | + else{ |
| 133 | + # Check if there are any user objects already in the database |
| 134 | + $newDatabase = ($server.Databases[$Database].Tables.Count -eq 0) |
| 135 | + } |
125 | 136 |
|
126 | 137 | # 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) |
134 | 142 |
|
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 |
138 | 146 |
|
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 | + } |
141 | 157 | }
|
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 |
144 | 160 | }
|
145 | 161 |
|
| 162 | + |
| 163 | + |
146 | 164 | # Writing the setting to the configuration file
|
147 | 165 | Write-PSFMessage -Message "Registering config values" -Level Verbose
|
148 | 166 | Set-PSFConfig -Module PSDatabaseClone -Name database.server -Value $SqlInstance -Initialize -Validation string
|
|
0 commit comments