forked from fsprojects/FAKE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLServerHelper.fs
More file actions
100 lines (80 loc) · 3.52 KB
/
Copy pathSQLServerHelper.fs
File metadata and controls
100 lines (80 loc) · 3.52 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
namespace Fake
open System
open System.Data.SqlClient
open Microsoft.SqlServer.Management.Smo
open Microsoft.SqlServer.Management.Common
open System.IO
type ServerInfo =
{ Server: Server;
ConnBuilder: SqlConnectionStringBuilder}
[<AutoOpen>]
module SqlServerSmoHelper =
/// Gets a connection to the SQL server and an instance to the ConnectionStringBuilder
let getServerInfo connectionString =
let connbuilder = new SqlConnectionStringBuilder(connectionString)
let conn = new ServerConnection()
if connbuilder.UserID <> "" then
conn.LoginSecure <- false
conn.Login <- connbuilder.UserID
if connbuilder.Password <> "" then
conn.LoginSecure <- false
conn.Password <- connbuilder.Password
conn.ServerInstance <- connbuilder.DataSource
conn.Connect()
let server = new Server(conn)
{Server = server; ConnBuilder = connbuilder}
/// gets the DatabaseNames from the server
let getDatabaseNamesFromServer (serverInfo:ServerInfo) =
seq {for db in serverInfo.Server.Databases -> db.Name}
/// Checks wether the given Database exists on the server
let existDBOnServer serverInfo dbName =
serverInfo
|> getDatabaseNamesFromServer
|> Seq.exists (fun d -> d = dbName)
/// Gets the name of the sercer
let getServerName serverInfo = serverInfo.ConnBuilder.DataSource
/// Gets the initial catalog name
let getDBName serverInfo = serverInfo.ConnBuilder.InitialCatalog
/// Gets the initial catalog as database instance
let getDatabase serverInfo =
new Database(serverInfo.Server,getDBName serverInfo )
/// Checks wether the given InitialCatalog exists on the server
let intitialCatalogExistsOnServer serverInfo =
getDBName serverInfo |> existDBOnServer serverInfo
/// Drops the given InitialCatalog from the server (if it exists)
let DropDb serverInfo =
if intitialCatalogExistsOnServer serverInfo then
log <| sprintf "Dropping database %s on server %s" (getDBName serverInfo) (getServerName serverInfo)
(getDatabase serverInfo).DropBackupHistory |> ignore
getDBName serverInfo |> serverInfo.Server.KillDatabase
serverInfo
/// Drops the given InitialCatalog from the server (if it exists)
let CreateDb serverInfo =
log <| sprintf "Creating database %s on server %s" (getDBName serverInfo) (getServerName serverInfo)
(getDatabase serverInfo).Create()
serverInfo
/// Runs a sql script on the server
let runScript serverInfo sqlFile =
log <| sprintf "Executing script %s" sqlFile
sqlFile
|> StringHelper.ReadFileAsString
|> (getDatabase serverInfo).ExecuteNonQuery
/// Closes the connection to the server
let Disconnect serverInfo =
serverInfo.Server.ConnectionContext.Disconnect()
/// Drops and creates the database (dropped if db exists. created nonetheless)
let DropAndCreateDatabase connectionString =
connectionString
|> getServerInfo
|> DropDb
|> CreateDb
|> Disconnect
/// Runs the given sql scripts on the server
let RunScripts connectionString scripts =
let serverInfo = getServerInfo connectionString
scripts |> Seq.iter (runScript serverInfo)
Disconnect serverInfo
/// Runs all sql scripts from the given directory on the server
let RunScriptsFromDirectory connectionString scriptDirectory =
let scripts = System.IO.Directory.GetFiles(scriptDirectory, "*.sql")
RunScripts connectionString scripts