-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecute-MySQLScript.ps1
65 lines (58 loc) · 2.14 KB
/
Execute-MySQLScript.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
[CmdletBinding()]
[OutputType([int])]
param
(
#Path to MySQL script file
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="Script")]
[ValidateScript({Test-Path $_ })]
[string]$ScriptPath,
#MySQL command to run
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="Command")]
[string]$Command
)
#Wake the site up, thereby starting MySQL. (MySQLInApp is started as a subprocess by IIS).
$ProgressPreference = "SilentlyContinue"
#Wake the site up, thereby starting MySQL. (MySQLInApp is started as a subprocess by IIS).
$curlPath = Invoke-Expression -Command 'cmd /c "where curl"'
$siteUrl = "https://$ENV:WEBSITE_HOSTNAME"
if (-not ([String]::IsNullOrWhiteSpace($curlPath))) {
Invoke-Expression -Command "& ""$curlPath"" ""$siteUrl""" -Verbose -ErrorAction SilentlyContinue | Out-Null
}
else {
Write-Warning "curl not in path."
}
if (Test-Path "D:\home\data\mysql\MYSQLCONNSTR_localdb.txt") {
$ConnectionString = [String]::Format("{0}{1}", "SslMode=none;", (Get-Content -LiteralPath "D:\home\data\mysql\MYSQLCONNSTR_localdb.txt").Replace(":",";Port="))
}
else {
Write-Error "MYSQLCONNSTR_localdb.txt was not found."
}
#Open the MysqlConnection
[System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
try {
$Connection = New-Object MySql.Data.MySqlClient.MySqlConnection -ArgumentList $ConnectionString
$Connection.Open()
Write-Debug "Connection open. (server version is $Connection.ServerVersion)"
}
catch {
Write-Error "Failed to open connection. ConnectionString was $ConnectionString."
Throw $_.Exception
}
#Prepare SQL statement(s)
$SQL = $Command
if ([String]::IsNullOrWhiteSpace($Command)) {
$SQL = Get-Content $ScriptPath
}
#Execute SQL statement(s)
try {
$MySqlScript = New-Object -TypeName MySql.Data.MySqlClient.MySqlScript -ArgumentList $Connection, $SQL
$statementsCount = $MySqlScript.Execute()
Write-Debug "$statementsCount statements were executed."
}
catch {
Write-Error 'Something went wrong trying to Execute() the script.'
Throw $_.Exception
}
finally {
$Connection.Close()
}