Browse Source

Improve SQL Server setup scripts

1. Add setup-sqlserver-bootstrap.ps1 that can be run from the web.
2. Run T-SQL scripts at the end of setup-sqlserver.ps1.
3. Make T-SQL scripts delete stuff if necessary.
Malcolm Evershed 12 years ago
parent
commit
7e9e77b28f

+ 4 - 0
config/create-sqlserver-login-and-database.sql

@@ -5,6 +5,10 @@
 -- "%ProgramFiles%\Microsoft SQL Server\110\Tools\binn\sqlcmd.exe" -i <filename of this file>
 --
 
+IF EXISTS (SELECT * FROM sys.server_principals WHERE name = 'benchmarkdbuser')
+    DROP LOGIN benchmarkdbuser
+GO
+
 -- This password has mixed-case and a number to satisfy the Windows password policy
 CREATE LOGIN benchmarkdbuser WITH PASSWORD = 'B3nchmarkDBPass'
 GO

+ 6 - 2
config/create-sqlserver.sql

@@ -5,7 +5,9 @@
 --
 -- "%ProgramFiles%\Microsoft SQL Server\110\Tools\binn\sqlcmd.exe" -U benchmarkdbuser -P B3nchmarkDBPass -d hello_world -i <filename of this file>
 
--- TODO: Check for an existing World table and drop it.
+IF OBJECT_ID('World', 'U') IS NOT NULL
+    DROP TABLE World
+GO
 
 CREATE TABLE World (
   id int NOT NULL IDENTITY PRIMARY KEY,
@@ -27,7 +29,9 @@ END
 
 GO
 
--- TODO: Check for an existing Fortune table and drop it.
+IF OBJECT_ID('Fortune', 'U') IS NOT NULL
+    DROP TABLE Fortune
+GO
 
 -- Note that this uses nvarchar to make sure that the column is Unicode.
 CREATE TABLE Fortune (

+ 25 - 0
setup-sqlserver-bootstrap.ps1

@@ -0,0 +1,25 @@
+# To download and run this script, open an elevated Command Prompt and then run:
+#
+# powershell -ExecutionPolicy Bypass -Command "iex (New-Object Net.WebClient).DownloadString('https://raw.github.com/TechEmpower/FrameworkBenchmarks/master/setup-sqlserver-bootstrap.ps1')"
+
+$basedir = "C:\FrameworkBenchmarks"
+$rawRepo = "https://raw.github.com/TechEmpower/FrameworkBenchmarks/master"
+
+$config_url = $basedir + "/config"
+$config_local = $basedir + "\config"
+$setup_sqlserver_url = $rawRepo + "/setup-sqlserver.ps1"
+$setup_sqlserver_local = $basedir + "\setup-sqlserver.ps1"
+$create_sqlserver_login_and_database_url = $config_url + "/create-sqlserver-login-and-database.sql"
+$create_sqlserver_login_and_database_local = $config_local + "/create-sqlserver-login-and-database.sql"
+$create_sqlserver_url = $config_url + "/create-sqlserver.sql"
+$create_sqlserver_local = $config_local + "/create-sqlserver.sql"
+
+Write-Host "Creating directory: $config`n"
+New-Item -Path $config -Type Directory -Force | Out-Null
+
+Write-Host "Downloading setup files...`n"
+(New-Object System.Net.WebClient).DownloadFile($setup_sqlserver_url, $setup_sqlserver_local)
+(New-Object System.Net.WebClient).DownloadFile($create_sqlserver_login_and_database_url, $create_sqlserver_login_and_database_local)
+(New-Object System.Net.WebClient).DownloadFile($create_sqlserver_url, $create_sqlserver_local)
+
+powershell -ExecutionPolicy Bypass -File $setup_sqlserver_local

+ 7 - 1
setup-sqlserver.ps1

@@ -34,4 +34,10 @@ Start-Process "$sqlserver_exe_local" "/q /action=install /features=SQLEngine /IN
 Write-Host "Configuring firewall...`n"
 New-NetFirewallRule -DisplayName "SQL 1433" -Action Allow -Direction Inbound -LocalPort 1433 -Protocol TCP | Out-Null
 
-# TODO: Run T-SQL files to create database and tables? To do that, we'd need to have the files config\create-sqlserver*.sql locally
+Write-Host "Creating SQL Server login and populated database...`n"
+
+Import-Module sqlps
+
+Invoke-Sqlcmd -InputFile "$basedir\config\create-sqlserver-login-and-database.sql" -OutputSqlErrors $True
+
+Invoke-Sqlcmd -Username benchmarkdbuser -Password B3nchmarkDBPass -Database hello_world -InputFile "$basedir\config\create-sqlserver.sql" -OutputSqlErrors $True