-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSetupDevMachine.ps1
220 lines (178 loc) · 5.21 KB
/
SetupDevMachine.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
<#
This script creates a LocalDB instance and a CodeCamp database. It also seeds the database with sample values.
There must be a single default Event in the event table in order for app to run.
If the LocalDB already exists then it does not try to create it.
If the CodeCamp database already exist then it does not try to create it or seed it. It is up to you to ensure it contains the necessary values.
If the '-recreate' command line argument is provided, it will drop the existing database if it exists and then create a fresh one from the setup scripts.
#>
Param(
[switch]$recreate
)
cls
$localDBInstance = "MSSQLLocalDB"
$dbname = "CodeCamp"
function OpenConnection {
Param ($server, $database)
Write-Host "Opening connection to '$database'"
$conn = new-object ('System.Data.SqlClient.SqlConnection')
$connString = "Server=$server;Integrated Security=SSPI;Database=$database"
$conn.ConnectionString = $connString
$conn.open()
return $conn
}
function ExecuteScalar {
Param ($conn, $query)
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandText = $query
$sqlCmd.Connection = $conn
return $sqlCmd.ExecuteScalar()
}
function ExecuteNonQuery {
Param ($conn, $query)
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandText = "SET NOCOUNT ON; $query"
$sqlCmd.Connection = $conn
# Just setting the result here but not using it.
# This is to just prevent '-1' from being printed out
# each time this is called.
$result = $sqlCmd.ExecuteNonQuery()
}
function DbExists{
Param ($conn)
try
{
$exists = ExecuteScalar $conn "SELECT TOP 1 * FROM master.dbo.sysdatabases WHERE name = '$dbname'"
# did we get Null? If so, the database doesn't exist.
if($exists)
{
return $true
}
else
{
return $false
}
}
catch
{
Write-Host "Failed checking for database. Exiting..."
$conn.Dispose()
exit
}
}
function CreateDb {
Param ($conn)
try
{
ExecuteNonQuery $conn "create database $dbname"
}
catch
{
Write-Host "Failed to create database. Exiting..."
$conn.Dispose()
exit
}
}
function DropDb {
Param ($conn)
try
{
ExecuteNonQuery $conn "alter database $dbname set single_user with rollback immediate; drop database $dbname"
}
catch
{
Write-Host "Failed to drop database. Exiting..."
$conn.Dispose()
exit
}
}
function RunSetupScript {
Param ($conn, $file, $errorMessage)
try
{
$path = "CC.Data.Database\SetupScripts\"
Write-Host "Executing $file"
$sql = Get-Content "$path$file"
ExecuteNonQuery $conn $sql
}
catch
{
Write-Host "$errorMessage Exiting..."
$conn.Dispose()
exit
}
}
function CreateTables {
RunSetupScript $conn "create.sql" "Failed to create tables without constraints."
}
function AddConstraints {
RunSetupScript $conn "AddConstraints.sql" "Failed to add constraints."
}
function Seed {
RunSetupScript $conn "seed.sql" "Failed to seed the database."
}
function GetLocalInstance {
$command = "SQLLocalDB i `"$($localDBInstance)`""
$info = Invoke-Expression $command
return $info
}
function CreateLocalInstance {
$command = "SQLLocalDB create `"$($localDBInstance)`""
$info = Invoke-Expression $command
return $info
}
function StartLocalInstance {
$command = "SQLLocalDB start `"$($localDBInstance)`""
$info = Invoke-Expression $command
return $info
}
$instanceInfo = GetLocalInstance
if($instanceInfo.Get(0) -like "*failed*"){
Write-Host "Could not find $localDBInstance."
Write-Host "Creating $localDBInstance"
CreateLocalInstance
}
StartLocalInstance
$pair = GetLocalInstance | where {$_ -like '*pipe*'}
$len = $pair.Length
$server = $pair.Substring($pair.IndexOf('np:\\.\pipe\LOCALDB'), $len - $pair.IndexOf('np:\\.\pipe\LOCALDB'))
# Open a connection to the master database to see if the CodeCamp database exists.
$conn = OpenConnection $server "master"
$dbExists = DbExists $conn
Write-Host "Database Exists: $dbExists"
Write-Host "Recreate: $recreate"
if ($dbExists -and $recreate) {
Write-Host "Dropping '$dbname' database"
DropDb $conn
Write-Host "'$dbname' database dropped"
}
if (-not $dbExists -or $recreate) {
# it doesn't exist, so create it, then change connection to
# to CodeCamp database and start populating it.
Write-Host "Creating '$dbname' database"
CreateDb $conn
Write-Host "'$dbname' database created"
# Switch connections
Write-Host "Closing connection to 'master'"
$conn.close()
$conn = OpenConnection $server $dbname
Write-Host "Creating tables that do not need to be seeded."
CreateTables $conn
Write-Host "Tables not needing to be seeded created."
Write-Host "Seeding '$dbname' with a default sample values and a default Event because it is necessary for site to run."
Seed $conn
Write-Host "Seeding compelete."
Write-Host "Adding constraints to '$dbname'."
AddConstraints $conn
Write-Host "Constraints added."
# we're done, so dispose of our connection.
Write-Host "Closing connection to '$dbname'"
$conn.close()
}
else
{
# If we are here, then the database exists, and
# we aren't recreating it. At this point,
# we still have a connection to 'master', so close it.
Write-Host "Closing connection to 'master'"
$conn.close()
}