forked from HighwayFramework/Highway.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseDeploy.psm1
160 lines (146 loc) · 5.73 KB
/
DatabaseDeploy.psm1
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
Push-Location; Import-Module SQLPS -DisableNameChecking; Pop-Location;
Import-Module PSWorkflow -DisableNameChecking
function Publish-DACPAC {
[CmdletBinding()] param(
[Parameter(Position=0,Mandatory=1)] [string]$DACPAC = $null,
[Parameter(Position=1,Mandatory=1)] [string]$PublishProfile = $null,
[Parameter(Position=2,Mandatory=1)] [string]$ConnectionString = $null,
[Parameter(Position=3,Mandatory=1)] [string]$Database = $null,
[Parameter(Position=4,Mandatory=0)] [switch]$Is2008R2 = $false
)
If($Is2008R2) {
add-type -path "${Env:ProgramFiles(x86)}\Microsoft SQL Server\100\DAC\bin\Microsoft.SqlServer.Dac.dll"
}
else {
add-type -path "${Env:ProgramFiles(x86)}\Microsoft SQL Server\110\DAC\bin\Microsoft.SqlServer.Dac.dll"
}
$dacProfile = [Microsoft.SqlServer.Dac.DacProfile]::Load($PublishProfile)
Write-Verbose "Profile Loaded"
$dacService = New-Object Microsoft.SqlServer.dac.dacservices ($ConnectionString)
$dacPackage = [Microsoft.SqlServer.Dac.DacPackage]::Load($dacpac)
Try {
register-objectevent -in $dacService -eventname Message -source "msg" -action { Out-host -in $Event.SourceArgs[1].Message.Message } | Out-Null
}
Catch {
Write-Verbose "Could not attach listener"
}
Write-Verbose "Deploying the DB with the following settings"
Write-Verbose "SqlServer: $ConnectionString"
Write-Verbose "dacpac: $DACPAC"
Write-Verbose "Database: $Database"
$dacService.deploy($dacPackage, $Database, $true, $dacProfile.DeployOptions)
Try {
unregister-event -source "msg"
}
Catch {
Write-Verbose "Unable to Detach listener"
}
Write-Output "Database Deployed"
}
workflow Publish-DACPACMultipleDatabases {
[CmdletBinding()] param(
[Parameter(Position=0,Mandatory=1)] [string]$DACPAC,
[Parameter(Position=1,Mandatory=1)] [string]$PublishProfile,
[Parameter(Position=2,Mandatory=1)] [string]$ConnectionString,
[Parameter(Position=3,Mandatory=1)] [string[]]$Databases
)
foreach -parallel($Database in $Databases){
Publish-DACPAC -DACPAC $DACPAC -PublishProfile $PublishProfile -ConnectionString $connectionString -Database $Database
}
}
workflow Publish-DACPACMultipleServers {
[CmdletBinding()] param(
[Parameter(Position=0,Mandatory=1)] [string]$DACPAC,
[Parameter(Position=1,Mandatory=1)] [string]$PublishProfile,
[Parameter(Position=2,Mandatory=1)] [string[]]$ConnectionStrings,
[Parameter(Position=3,Mandatory=1)] [string]$Database
)
foreach -parallel($connectionString in $ConnectionStrings){
Publish-DACPAC -DACPAC $DACPAC -PublishProfile $PublishProfile -ConnectionString $connectionString -Database $Database
}
}
workflow Publish-DACPACMultipleServersMultipleDatabases {
[CmdletBinding()] param(
[Parameter(Position=0,Mandatory=1)] [string]$DACPAC = "",
[Parameter(Position=1,Mandatory=1)] [string]$PublishProfile = "",
[Parameter(Position=2,Mandatory=1)] [string[]]$ConnectionStrings,
[Parameter(Position=3,Mandatory=1)] [string[]]$Databases
)
foreach -parallel($connectionString in $ConnectionStrings){
Publish-DACPACMultipleDatabases -DACPAC $DACPAC -PublishProfile $PublishProfile -ConnectionString $connectionString -Databases $Databases
}
}
workflow Invoke-SqlMultipleServersMultipleDatabases{
[CmdletBinding()] param(
[Parameter(Position=0,Mandatory=1)] [string]$Query,
[Parameter(Position=1,Mandatory=1)] [string[]]$ServerInstances,
[Parameter(Position=2,Mandatory=1)] [string[]]$Databases
)
foreach -parallel($ServerInstance in $ServerInstances){
Invoke-SqlMultipleDatabases -Query $Query -ServerInstance $ServerInstance -Databases $Databases
}
}
workflow Invoke-SqlMultipleServers{
[CmdletBinding()] param(
[Parameter(Position=0,Mandatory=1)] [string]$Query,
[Parameter(Position=1,Mandatory=1)] [string[]]$ServerInstances,
[Parameter(Position=2,Mandatory=1)] [string]$Database
)
foreach -parallel($ServerInstance in $ServerInstances){
Invoke-SqlCmd -Query $Query -ServerInstance $ServerInstance -Database $Database
}
}
workflow Invoke-SqlMultipleDatabases{
[CmdletBinding()] param(
[Parameter(Position=0,Mandatory=1)] [string]$Query,
[Parameter(Position=1,Mandatory=1)] [string]$ServerInstance,
[Parameter(Position=2,Mandatory=1)] [string[]]$Databases
)
foreach -parallel($Database in $Databases){
Invoke-SqlCmd -Query $Query -ServerInstance $ServerInstance -Database $Database
}
}
Function Select-DatabaseExists {[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)] [string]$SqlServer = $null,
[Parameter(Position=1,Mandatory=1)] [string]$Database = $null
)
$exists = $FALSE
try
{
$conn = New-Object system.Data.SqlClient.SqlConnection
$conn.connectionstring = [string]::format("Server={0};Database={1};Integrated Security=SSPI;",$SqlServer,$Database)
$conn.open()
$exists = $true
}
catch
{
Write-Error "Failed to connect to DB $Database on $SqlServer"
}
Write-Output $exists
}
Function Select-TableExists {[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)] [string]$SqlServer = $null,
[Parameter(Position=1,Mandatory=1)] [string]$Database = $null,
[Parameter(Position=2,Mandatory=1)] [string]$TableName = $null,
[Parameter(Position=3,Mandatory=0)] [string]$SchemaName = "dbo"
)
$exists = $FALSE
try
{
$query = [string]::Format("SELECT Count(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}'", $SchemaName, $TableName)
$result = Invoke-Sqlcmd -Query $query -ServerInstance $SqlServer -Database $Database
if($result[0] -gt 0){
$exists = $true
}
else{
$exists = $FALSE
}
}
catch
{
Write-Error "Failed to connect to DB $Database on $SqlServer"
}
Write-Output $exists
}