functions/Test-DbaMigrationConstraint.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
function Test-DbaMigrationConstraint {
    <#
  .SYNOPSIS
   Show if you can migrate the database(s) between the servers.
 
  .DESCRIPTION
   When you want to migrate from a higher edition to a lower one there are some features that can't be used.
   This function will validate if you have any of this features in use and will report to you.
   The validation will be made ONLY on on SQL Server 2008 or higher using the 'sys.dm_db_persisted_sku_features' dmv.
 
   This function only validate SQL Server 2008 versions or higher.
   The editions supported by this function are:
    - Enterprise
    - Developer
    - Evaluation
    - Standard
    - Express
 
   Take into account the new features introduced on SQL Server 2016 SP1 for all versions. More information at https://blogs.msdn.microsoft.com/sqlreleaseservices/sql-server-2016-service-pack-1-sp1-released/
 
   The -Database parameter is auto-populated for command-line completion.
 
   .PARAMETER Source
    Source SQL Server. You must have sysadmin access and server version must be SQL Server version 2000 or higher.
 
   .PARAMETER SourceSqlCredential
    Allows you to login to servers using SQL Logins instead of Windows Authentication (AKA Integrated or Trusted). To use:
 
    $scred = Get-Credential, then pass $scred object to the -SourceSqlCredential parameter.
 
    Windows Authentication will be used if SourceSqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
 
    To connect as a different Windows user, run PowerShell as that user.
 
   .PARAMETER Destination
    Destination SQL Server. You must have sysadmin access and the server must be SQL Server 2000 or higher.
 
   .PARAMETER DestinationSqlCredential
    Allows you to login to servers using SQL Logins instead of Windows Authentication (AKA Integrated or Trusted). To use:
 
    $dcred = Get-Credential, then pass this $dcred to the -DestinationSqlCredential parameter.
 
    Windows Authentication will be used if DestinationSqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
 
    To connect as a different Windows user, run PowerShell as that user.
 
  .PARAMETER Database
   The database(s) to process. Options for this list are auto-populated from the server. If unspecified, all databases will be processed.
 
  .PARAMETER ExcludeDatabase
   The database(s) to exclude. Options for this list are auto-populated from the server.
 
  .NOTES
   Tags: Migration
 
   Author: Claudio Silva (@ClaudioESSilva)
   Website: https://dbatools.io
   Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
   License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
  .LINK
   https://dbatools.io/Test-DbaMigrationConstraint
 
  .EXAMPLE
   Test-DbaMigrationConstraint -Source sqlserver2014a -Destination sqlcluster
 
   All databases on sqlserver2014a will be verified for features in use that can't be supported on sqlcluster.
 
  .EXAMPLE
   Test-DbaMigrationConstraint -Source sqlserver2014a -Destination sqlcluster -SqlCredential $cred
 
   All databases will be verified for features in use that can't be supported on the destination server. SQL credentials are used to authenticate against sqlserver2014 and Windows Authentication is used for sqlcluster.
 
  .EXAMPLE
   Test-DbaMigrationConstraint -Source sqlserver2014a -Destination sqlcluster -Database db1
    
   Only db1 database will be verified for features in use that can't be supported on the destination server.
 #>

    [CmdletBinding(DefaultParameterSetName = "DbMigration")]
    Param (
        [parameter(Mandatory = $true, ValueFromPipeline = $True)]
        [DbaInstanceParameter]$Source,
        [PSCredential]$SourceSqlCredential,
        [parameter(Mandatory = $true)]
        [DbaInstanceParameter]$Destination,
        [PSCredential]$DestinationSqlCredential,
        [Alias("Databases")]
        [object[]]$Database,
        [object[]]$ExcludeDatabase
    )

    begin {
        <#
   1804890536 = Enterprise
   1872460670 = Enterprise Edition: Core-based Licensing
   610778273 = Enterprise Evaluation
   284895786 = Business Intelligence
   -2117995310 = Developer
   -1592396055 = Express
   -133711905= Express with Advanced Services
   -1534726760 = Standard
   1293598313 = Web
   1674378470 = SQL Database
  #>


        $editions = @{
            "Enterprise" = 10;
            "Developer" = 10;
            "Evaluation" = 10;
            "Standard"   = 5;
            "Express" = 1
        }
        $notesCanMigrate = "Database can be migrated."
        $notesCannotMigrate = "Database cannot be migrated."
    }
    process {

        Write-Output "Attempting to connect to Sql Servers."
        $sourceserver = Connect-SqlInstance -SqlInstance $Source -SqlCredential $SourceSqlCredential
        $destserver = Connect-SqlInstance -SqlInstance $Destination -SqlCredential $DestinationSqlCredential

        if ($Database -eq 0) {
            $Database = $sourceserver.Databases | Where-Object isSystemObject -eq 0 | Select-Object Name, Status
        }

        if ($ExcludeDatabase) {
            $Database = $sourceserver.Databases | Where-Object Name -NotIn $ExcludeDatabase
        }

        if ($Database -gt 0) {
            if ($Database -contains "master" -or $Database -contains "msdb" -or $Database -contains "tempdb") {
                throw "Migrating system databases is not currently supported."
            }

            if ($sourceserver.versionMajor -lt 9 -and $destserver.versionMajor -gt 10) {
                throw "Sql Server 2000 databases cannot be migrated to Sql Server versions 2012 and above. Quitting."
            }

            if ($sourceserver.collation -ne $destserver.collation) {
                Write-Warning "Collation on $Source, $($sourceserver.collation) differs from the $Destination, $($destserver.collation)."
            }

            if ($sourceserver.versionMajor -gt $destserver.versionMajor) {
                #indicate that must use 'Generate Scripts' and 'Export Data' options?
                throw "You can't migrate databases from a higher version to a lower one. Quitting."
            }

            if ($sourceserver.versionMajor -lt 10) {
                throw "This function does not support versions lower than SQL Server 2008 (v10)"
            }

            #if editions differs, from higher to lower one, verify the sys.dm_db_persisted_sku_features - only available from SQL 2008 +
            if (($sourceserver.versionMajor -ge 10 -and $destserver.versionMajor -ge 10)) {
                foreach ($db in $Database) {
                    if ([string]::IsNullOrEmpty($db.Status)) {
                        $dbstatus = ($sourceserver.Databases | Where-Object {$_.Name -eq $db}).Status.ToString()
                        $dbName = $db
                    }
                    else {
                        $dbstatus = $db.Status.ToString()
                        $dbName = $db.Name
                    }

                    Write-Verbose "Checking database '$dbName'."

                    if ($dbstatus.Contains("Offline") -eq $false) {
                        [long]$destVersionNumber = $($destserver.VersionString).Replace(".", "")
                        [string]$SourceVersion = "$($sourceServer.Edition) $($sourceServer.ProductLevel) ($($sourceserver.Version))"
                        [string]$DestinationVersion = "$($destserver.Edition) $($destserver.ProductLevel) ($($destserver.Version))"
                        [string]$dbFeatures = ""

                        try {
                            $sql = "SELECT feature_name FROM sys.dm_db_persisted_sku_features"

                            $skufeatures = $sourceServer.Databases[$dbName].ExecuteWithResults($sql)

                            Write-Verbose "Checking features in use..."

                            if ($skufeatures.Tables[0].Rows.Count -gt 0) {
                                foreach ($row in $skufeatures.Tables[0].Rows) {
                                    $dbFeatures += ",$($row["feature_name"])"
                                }

                                $dbFeatures = $dbFeatures.TrimStart(",")
                            }
                        }
                        catch {
                            Write-Warning "Can't execute SQL on $sourceserver. `r`n $($_)"
                            Continue
                        }

                        #If SQL Server 2016 SP1 (13.0.4001.0) or higher
                        if ($destVersionNumber -ge 13040010) {
                            <#
        Need to verify if Edition = EXPRESS and database uses 'Change Data Capture' (CDC)
        This means that database cannot be migrated because Express edition don't have SQL Server Agent
       #>

                            if ($editions.Item($destserver.Edition.ToString().Split(" ")[0]) -eq 1 -and $dbFeatures.Contains("ChangeCapture")) {
                                [pscustomobject]@{
                                    SourceInstance      = $sourceserver.Name
                                    DestinationInstance = $destserver.Name
                                    SourceVersion       = $SourceVersion
                                    DestinationVersion  = $DestinationVersion
                                    Database            = $dbName
                                    FeaturesInUse       = $dbFeatures
                                    Notes               = "$notesCannotMigrate. Destination server edition is EXPRESS which does not support 'ChangeCapture' feature that is in use."
                                }
                            }
                            else {
                                [pscustomobject]@{
                                    SourceInstance      = $sourceserver.Name
                                    DestinationInstance = $destserver.Name
                                    SourceVersion       = $SourceVersion
                                    DestinationVersion  = $DestinationVersion
                                    Database            = $dbName
                                    FeaturesInUse       = $dbFeatures
                                    Notes               = $notesCanMigrate
                                }
                            }
                        }
                        #Version is lower than SQL Server 2016 SP1
                        else {
                            Write-Verbose "Source Server Edition: $($sourceserver.Edition) (Weight: $($editions.Item($sourceserver.Edition.ToString().Split(" ")[0])))"
                            Write-Verbose "Destination Server Edition: $($destserver.Edition) (Weight: $($editions.Item($destserver.Edition.ToString().Split(" ")[0])))"

                            #Check for editions. If destination edition is lower than source edition and exists features in use
                            if (($editions.Item($destserver.Edition.ToString().Split(" ")[0]) -lt $editions.Item($sourceserver.Edition.ToString().Split(" ")[0])) -and (!([string]::IsNullOrEmpty($dbFeatures)))) {
                                [pscustomobject]@{
                                    SourceInstance      = $sourceserver.Name
                                    DestinationInstance = $destserver.Name
                                    SourceVersion       = $SourceVersion
                                    DestinationVersion  = $DestinationVersion
                                    Database            = $dbName
                                    FeaturesInUse       = $dbFeatures
                                    Notes               = "$notesCannotMigrate There are features in use not available on destination instance."
                                }
                            }
                            #
                            else {
                                [pscustomobject]@{
                                    SourceInstance      = $sourceserver.Name
                                    DestinationInstance = $destserver.Name
                                    SourceVersion       = $SourceVersion
                                    DestinationVersion  = $DestinationVersion
                                    Database            = $dbName
                                    FeaturesInUse       = $dbFeatures
                                    Notes               = $notesCanMigrate
                                }
                            }
                        }
                    }
                    else {
                        Write-Warning "Database '$dbName' is offline. Bring database online and re-run the command"
                    }

                }
            }
            else {
                #SQL Server 2005 or under
                Write-Warning "This validation will not be made on versions lower than SQL Server 2008 (v10)."
                Write-Verbose "Source server version: $($sourceserver.versionMajor)."
                Write-Verbose "Destination server version: $($destserver.versionMajor)."
            }
        }
        else {
            Write-Output "There are no databases to validate."
        }
    }
    END {
        $sourceserver.ConnectionContext.Disconnect()
        $destserver.ConnectionContext.Disconnect()
        Test-DbaDeprecation -DeprecatedOn "1.0.0" -Silent:$false -Alias Test-SqlMigrationConstraint
    }
}