forked from davidbwire/bvcms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrate-Databases.ps1
More file actions
150 lines (136 loc) · 4.46 KB
/
Copy pathMigrate-Databases.ps1
File metadata and controls
150 lines (136 loc) · 4.46 KB
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
param (
[string]$scripts = ".\CmsData\Migrations",
[switch]$prod = $false,
[string]$db = $null,
[string]$dbname = $null,
[int]$start = 0,
[int]$end = 10000
)
$logfile = "Migrate-Databases-$start.log"
$page = $end - $start
$Global:SERVERNAME = if ($prod) {$env:ProductionDatabaseServer} else {"(local)"}
$Global:SQLUSERNAME = $env:ProductionDatabaseUser
$Global:SQLPASSWORD = $env:ProductionDatabasePassword
function Get-Connection {
param(
[string] $dbname
)
try {
$connection = New-Object System.Data.SQLClient.SQLConnection
if ($prod) {
$connection.ConnectionString = "server=$SERVERNAME;database=$dbname;User Id=$SQLUSERNAME;Password=$SQLPASSWORD"
}
else {
$connection.ConnectionString = "server=$SERVERNAME;database=$dbname;Integrated Security=true"
}
$connection.Open()
}
catch {
Write-Log "Failed to connect to SQL Server: $PSItem"
exit
}
return $connection
}
function Get-QueryResult {
param(
[System.Data.SqlClient.SqlConnection] $connection,
[string] $query
)
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $query
$command.Connection = $connection
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter
$adapter.SelectCommand = $command
$dataset = New-Object System.Data.DataSet
$adapter.fill($dataset) | out-null
return $dataset
}
function Get-AllSQLScripts {
param (
[string] $folder
)
Write-Log "Loading scripts from $folder"
$list = New-Object System.Collections.Generic.List[object]
Get-ChildItem $folder -Filter *.sql | Foreach-Object {
$cmds = New-Object System.Collections.Generic.List[string]
Write-Log $_.Name
$content = [IO.File]::ReadAllText($_.FullName)
$content -split '^GO.*$', 0, "multiline" | Foreach-Object {
$cmds.Add($_)
}
$o = @{}
$o.name = $_.Name
$o.commands = $cmds
$list.Add($o)
}
return $list
}
function Get-IsMigrationApplied {
param (
[string] $id,
[System.Data.DataSet] $migrations
)
foreach ($data in $migrations.tables[0]) {
if ($id -eq $data[0]) {
return $true
}
}
return $false
}
function Invoke-SqlCommand {
param (
[System.Data.SqlClient.SqlConnection] $connection,
[string] $commandText
)
$command = New-Object System.Data.SqlClient.SqlCommand
$command.Connection = $connection
$command.CommandTimeout = 120
$command.CommandText = $commandText
return $command.ExecuteNonQuery() | Out-Null
}
function Write-Log {
param (
[string] $message
)
Write-Host $message
"$message" | Out-File -FilePath $logfile -Append
}
if ((Test-Path $logfile) -And (Get-Item $logfile).length -gt 2mb) {
Remove-Item $logfile
}
Write-Log (Get-Date).toString("yyyy-MM-dd HH:mm:ss")
$msg = if ($prod) {"Connecting to Production"} else {"Local connection"}
Write-Log $msg
$commandsToRun = Get-AllSQLScripts $scripts
$db = if ($db -eq $null) { $dbname } else { $db }
$SQLConnection = Get-Connection "master"
$SQLDataset = Get-QueryResult $SQLConnection "SELECT name FROM sys.databases WHERE name LIKE 'CMS[_]%' AND (LEN('$db') = 0 OR name='$db') ORDER BY name OFFSET $start ROWS FETCH NEXT $page ROWS ONLY"
$SQLConnection.close()
foreach ($data in $SQLDataset.tables[0]) {
$cmsdbname = $data[0]
Write-Log $cmsdbname
$connection = Get-Connection $cmsdbname
$migrations = Get-QueryResult $connection "IF EXISTS (SELECT 1 FROM sys.tables WHERE name = '__SqlMigrations') SELECT Id FROM dbo.__SqlMigrations"
foreach($cmd in $commandsToRun) {
$id = $cmd.name
$commands = $cmd.commands
$applied = Get-IsMigrationApplied $id $migrations
if (-Not $applied) {
$commands | Foreach-Object {
if ($_.Trim().Length -gt 0) {
try {
Invoke-SqlCommand $connection $_ -ErrorAction Stop
} catch {
Write-Log "Error in $id"
Write-Log $_
Write-Error "Error in $id"
Write-Error $_
break
}
}
}
Write-Log "Applied $id"
Invoke-SqlCommand $connection "INSERT INTO dbo.__SqlMigrations (Id) VALUES('$id')"
}
}
}