forked from jmt4/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Migrate-Db.ps1
183 lines (152 loc) · 5.41 KB
/
Migrate-Db.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
<#
The MIT License (MIT)
Copyright (c) 2015 Michael Kropat
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
#>
<#
.SYNOPSIS
Run schema migration scripts on a SQL Server databse
.DESCRIPTION
The database version is kept track of in an extended property ('db-version') on
the SQL Server database. The version number comes from the prefix of the
filename of each .sql migration script (see -SchemaDir parameter help for more
information).
.PARAMETER Server
The SQL Server instance to connect to. For example: (local)\SQLEXPRESS
.PARAMETER Database
The name of the database to run the migration on.
.PARAMETER SchemaDir
A directory containing one or more .sql files that have a numeric prefix in the
filename. The numeric prefix represents the database version that that
particular migration script upgrades the database to. Versions are compared by
_string ordering_ to determine which one is greater. To minimize version
control merge conflicts, it is recommended to use the current date in YYYYMMDD
format, followed by a -NN (-01, -02, etc.) sequentially incrementing counter.
An example schema directory might look like:
20150221-01-create-table.sql
20150221-02-populate-table.sql
20150222-disallow-nulls.sql
.EXAMPLE
.\migrate-db.ps1 -Database 'your-db' -SchemaDir .\Db\Schema
.NOTES
Downgrade/rollback scripts aren't supported. It wouldn't be hard to add
support for them.
If you get errors like: '"Could not load file or assembly 'Microsoft.SqlServer.BatchParser'
then try running the x86 version of PowerShell.
#>
param(
[String]
$Server = '(local)\SQLEXPRESS',
[parameter(Mandatory=$true)]
[String]
$Database,
[parameter(Mandatory=$true)]
[String]
$SchemaDir
)
Add-Type -AssemblyName 'Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'
function main {
$db = Open-Database $Server $Database
$original = Get-DbVersion $db
Write-Host "Database '$Database' is at version: $original"
$migrations = Get-SchemaMigrations $SchemaDir
try {
Invoke-Migrations $db $migrations
}
finally {
$current = Get-DbVersion $db
if ($current -gt $original) {
Write-Host "It has been migrated to: $current"
}
else {
Write-Host "No migration performed — $original is the current version."
}
}
}
function Open-Database($Server, $Database) {
$s = New-Object Microsoft.SqlServer.Management.Smo.Server $Server
$db = $s.Databases[$Database]
if ($db -eq $null) {
throw "Unable to open database '$Database'"
}
$db
}
function Get-SchemaMigrations($Dir) {
Get-ChildItem -Path $Dir -File -Filter '*.sql' |
where { $_ -match '^([-_0-9]+)' } |
foreach {
$prefix = ($_.Name | Select-String -Pattern '^([-_0-9]+)').Matches[0].Groups[1].Value
@{
Path = $_.FullName
}
}
}
function Invoke-Migrations($Database, $Migrations) {
try {
foreach ($m in $Migrations) {
Invoke-Migration $Database $m
}
}
catch {
throw $_.Exception
}
}
function Invoke-Migration($Database, $Migration) {
$conn = Get-DbConnection $Database
$script = Get-Content $Migration.Path -Raw
$conn.BeginTransaction()
$Database.ExecuteNonQuery($script)
Set-DbVersion $Database $Migration.Version
$conn.CommitTransaction()
}
function Get-DbConnection {
param(
[Microsoft.SqlServer.Management.Smo.Database]
$Database
)
[Microsoft.SqlServer.Management.Common.ServerConnection]$Database.Parent.ConnectionContext
}
$script:versionPropertyName = 'db-version'
function Get-DbVersion {
param(
[Microsoft.SqlServer.Management.Smo.Database]
$Database
)
if ($Database.ExtendedProperties.Contains($script:versionPropertyName)) {
[String]$Database.ExtendedProperties['db-version'].Value
}
else {
'0'
}
}
function Set-DbVersion {
param(
[Microsoft.SqlServer.Management.Smo.Database]
$Database,
$Version
)
if ($Database.ExtendedProperties.Contains($script:versionPropertyName)) {
$property = $Database.ExtendedProperties[$script:versionPropertyName]
$property.Value = $Version
$property.Alter()
}
else {
$property = New-Object Microsoft.SqlServer.Management.Smo.ExtendedProperty -ArgumentList $Database,$script:versionPropertyName,$Version
$property.Create()
}
}
main