forked from Jcouls29/Development-Project-CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetup-InventoryDatabase.ps1
More file actions
146 lines (124 loc) · 4.92 KB
/
Copy pathSetup-InventoryDatabase.ps1
File metadata and controls
146 lines (124 loc) · 4.92 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
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
param(
[string]$Server = ".",
[string]$Database = "inventory",
[switch]$Reset
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
Add-Type -AssemblyName System.Data
function Invoke-SqlBatch {
param(
[string]$ConnectionString,
[string]$CommandText
)
if ([string]::IsNullOrWhiteSpace($CommandText)) {
return
}
$connection = New-Object System.Data.SqlClient.SqlConnection $ConnectionString
try {
$connection.Open()
$command = $connection.CreateCommand()
$command.CommandTimeout = 120
$command.CommandText = $CommandText
[void]$command.ExecuteNonQuery()
}
finally {
$connection.Dispose()
}
}
function Invoke-SqlFile {
param(
[string]$ConnectionString,
[string]$FilePath
)
if (-not (Test-Path -LiteralPath $FilePath)) {
throw "SQL file not found: $FilePath"
}
Write-Host "Running $FilePath"
$content = Get-Content -LiteralPath $FilePath -Raw
$batches = [System.Text.RegularExpressions.Regex]::Split($content, "(?im)^\s*GO\s*$")
foreach ($batch in $batches) {
$trimmed = $batch.Trim()
if ($trimmed.Length -eq 0) {
continue
}
Invoke-SqlBatch -ConnectionString $ConnectionString -CommandText $trimmed
}
}
function Invoke-SqlQuery {
param(
[string]$ConnectionString,
[string]$CommandText
)
$connection = New-Object System.Data.SqlClient.SqlConnection $ConnectionString
try {
$connection.Open()
$command = $connection.CreateCommand()
$command.CommandTimeout = 120
$command.CommandText = $CommandText
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter $command
$table = New-Object System.Data.DataTable
[void]$adapter.Fill($table)
return $table
}
finally {
$connection.Dispose()
}
}
$root = $PSScriptRoot
$masterConnectionString = "Data Source=$Server;Initial Catalog=master;Integrated Security=True;Encrypt=False;TrustServerCertificate=True"
$databaseConnectionString = "Data Source=$Server;Initial Catalog=$Database;Integrated Security=True;Encrypt=False;TrustServerCertificate=True"
$dropDatabaseCommand = @"
IF DB_ID(N'$Database') IS NOT NULL
BEGIN
ALTER DATABASE [$Database] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [$Database];
END
"@
$createDatabaseCommand = @"
IF DB_ID(N'$Database') IS NULL
BEGIN
CREATE DATABASE [$Database];
END
"@
if ($Reset) {
Write-Host "Resetting database [$Database] on server [$Server]"
Invoke-SqlBatch -ConnectionString $masterConnectionString -CommandText $dropDatabaseCommand
}
Invoke-SqlBatch -ConnectionString $masterConnectionString -CommandText $createDatabaseCommand
$sqlFiles = @(
"Development Project\Sparcpoint.Inventory.Database\Instances\Instances.sql",
"Development Project\Sparcpoint.Inventory.Database\Transactions\Transactions.sql",
"Development Project\Sparcpoint.Inventory.Database\Table Types\IntegerList.sql",
"Development Project\Sparcpoint.Inventory.Database\Table Types\StringList.sql",
"Development Project\Sparcpoint.Inventory.Database\Table Types\CustomAttributeList.sql",
"Development Project\Sparcpoint.Inventory.Database\Table Types\CorrelatedIntegerList.sql",
"Development Project\Sparcpoint.Inventory.Database\Table Types\CorrelatedStringList.sql",
"Development Project\Sparcpoint.Inventory.Database\Table Types\CorrelatedCustomAttributeList.sql",
"Development Project\Sparcpoint.Inventory.Database\Instances\Table Types\CorrelatedListItemList.sql",
"Development Project\Sparcpoint.Inventory.Database\Instances\Table Types\CorrelatedProductInstanceList.sql",
"Development Project\Sparcpoint.Inventory.Database\Instances\Tables\Categories.sql",
"Development Project\Sparcpoint.Inventory.Database\Instances\Tables\Products.sql",
"Development Project\Sparcpoint.Inventory.Database\Instances\Tables\CategoryAttributes.sql",
"Development Project\Sparcpoint.Inventory.Database\Instances\Tables\ProductAttributes.sql",
"Development Project\Sparcpoint.Inventory.Database\Instances\Tables\ProductCategories.sql",
"Development Project\Sparcpoint.Inventory.Database\Instances\Tables\CategoryCategories.sql",
"Development Project\Sparcpoint.Inventory.Database\Transactions\Tables\InventoryTransactions.sql"
)
foreach ($relativePath in $sqlFiles) {
$absolutePath = Join-Path $root $relativePath
Invoke-SqlFile -ConnectionString $databaseConnectionString -FilePath $absolutePath
}
$verifyCommand = @"
SELECT
s.name AS SchemaName,
t.name AS TableName
FROM sys.tables t
INNER JOIN sys.schemas s ON s.schema_id = t.schema_id
WHERE s.name IN ('Instances', 'Transactions')
ORDER BY s.name, t.name;
"@
Write-Host ""
Write-Host "Database setup complete. Created tables:"
$results = Invoke-SqlQuery -ConnectionString $databaseConnectionString -CommandText $verifyCommand
$results | Format-Table -AutoSize