Repository navigation
Expand file tree
/
Copy pathBuild-DatabaseScripts.ps1
More file actions
275 lines (228 loc) · 8.91 KB
/
Copy pathBuild-DatabaseScripts.ps1
File metadata and controls
275 lines (228 loc) · 8.91 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Build-DatabaseScripts.ps1
# Consolidates SQL scripts from src/<Database>/<Provider>/ into dist/<Database>/<Database>.<Provider>.sql
param(
[string]$SrcDir = "",
[string]$DistDir = "dist",
[string]$ConfigFile = "config.json"
)
# Default paths - assumes script runs from jaunty project root
if ([string]::IsNullOrEmpty($SrcDir)) {
$SrcDir = "C:\home\syed\code\remote\databases\src"
}
if (-not (Test-Path $SrcDir)) {
$SrcDir = "..\..\home\syed\code\remote\databases\src"
}
if (-not (Test-Path $SrcDir)) {
$SrcDir = "src"
}
$ErrorActionPreference = "Stop"
# Load config
$configPath = Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) $ConfigFile
if (-not (Test-Path $configPath)) {
$configPath = $ConfigFile
}
if (Test-Path $configPath) {
$config = Get-Content $configPath -Raw | ConvertFrom-Json
} else {
Write-Host "Config file not found: $configPath" -ForegroundColor Yellow
$config = $null
}
# Get default connection string based on provider
function Get-DefaultConnectionString {
param([string]$Provider, [string]$DatabaseName)
switch ($Provider) {
"SqlServer" {
return "Server=localhost;Database=$DatabaseName;Trusted_Connection=True;TrustServerCertificate=True"
}
"Postgres" {
return "Host=localhost;Database=$DatabaseName;Username=postgres;Password=postgres"
}
"MySql" {
return "Server=localhost;Database=$DatabaseName;Uid=root;Pwd=root"
}
"Sqlite" {
return "Data Source=$DatabaseName.db"
}
default {
return ""
}
}
}
# Get connection string from config or use default
function Get-ConnectionString {
param([string]$Provider, [string]$DatabaseName)
if ($config) {
$providerKey = $Provider
if ($Provider -eq "MySql") { $providerKey = "MySql" }
$connStr = $config.$providerKey.ConnectionString
if (-not [string]::IsNullOrWhiteSpace($connStr)) {
return $connStr
}
}
return Get-DefaultConnectionString -Provider $Provider -DatabaseName $DatabaseName
}
# Directories to ignore (not database providers)
$IgnoreDirs = @("bin", "obj", ".git", ".vs", "node_modules")
# Folder order mapping (prefix -> display order)
$FolderOrder = @{
"1. " = 1
"2. " = 2
"3. " = 3
"4. " = 4
"5. " = 5
"6. " = 6
"7. " = 7
"8. " = 8
"9. " = 9
}
# Default folder order if not prefixed
$DefaultFolders = @("Tables", "Views", "Data", "Constraints", "Stored Procedures")
function Get-OrderedFolders {
param([string]$ProviderPath)
$folders = Get-ChildItem -Directory -Path $ProviderPath | Where-Object {
$_.Name -notin $IgnoreDirs
}
# Check if folders have number prefixes
$hasPrefix = $false
foreach ($folder in $folders) {
foreach ($prefix in $FolderOrder.Keys) {
if ($folder.Name.StartsWith($prefix)) {
$hasPrefix = $true
break
}
}
if ($hasPrefix) { break }
}
$ordered = @()
foreach ($folder in $folders) {
if ($hasPrefix) {
# Use explicit order from prefix
$order = 999
$cleanName = $folder.Name
foreach ($prefix in $FolderOrder.Keys) {
if ($folder.Name.StartsWith($prefix)) {
$order = $FolderOrder[$prefix]
$cleanName = $folder.Name.Substring($prefix.Length).Trim()
break
}
}
$ordered += [PSCustomObject]@{
Order = $order
Name = $cleanName
DisplayName = $folder.Name
FullPath = $folder.FullName
}
} else {
# Use default order based on name matching
$order = 999
$cleanName = $folder.Name
for ($i = 0; $i -lt $DefaultFolders.Count; $i++) {
if ($folder.Name -eq $DefaultFolders[$i]) {
$order = $i + 1
break
}
}
$ordered += [PSCustomObject]@{
Order = $order
Name = $folder.Name
DisplayName = $folder.Name
FullPath = $folder.FullName
}
}
}
return $ordered | Sort-Object Order
}
function Build-DatabaseScripts {
param([System.IO.DirectoryInfo]$DatabaseDir)
$dbName = $DatabaseDir.Name
Write-Host "`n=== Building: $dbName ===" -ForegroundColor Cyan
# Find all provider directories
$providers = Get-ChildItem -Directory -Path $DatabaseDir.FullName | Where-Object {
$_.Name -notin $IgnoreDirs
}
if ($providers.Count -eq 0) {
Write-Host " No providers found in $dbName" -ForegroundColor Yellow
return
}
foreach ($provider in $providers) {
$providerName = $provider.Name
Write-Host " Provider: $providerName" -ForegroundColor Gray
# Get ordered folders
$folders = Get-OrderedFolders -ProviderPath $provider.FullName
# Output file
$outputDir = Join-Path $DistDir $dbName
if (-not (Test-Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
}
$outputFile = Join-Path $outputDir "$dbName.$providerName.sql"
# Clear output file
if (Test-Path $outputFile) {
Remove-Item $outputFile
}
# Ensure file exists with a comment header
"-- Consolidated SQL script for $dbName - $providerName" | Out-File $outputFile -Encoding utf8
# Special handling for SQL Server - set quoted identifier
if ($providerName -eq "SqlServer") {
"SET QUOTED_IDENTIFIER ON;" | Out-File $outputFile -Append -Encoding utf8
"GO" | Out-File $outputFile -Append -Encoding utf8
}
# Special handling for SQLite - add header
if ($providerName -eq "Sqlite") {
"PRAGMA foreign_keys = OFF;" | Out-File $outputFile -Append -Encoding utf8
"BEGIN TRANSACTION;" | Out-File $outputFile -Append -Encoding utf8
}
# Process each folder in order
foreach ($folder in $folders) {
$sqlFiles = Get-ChildItem -Path $folder.FullPath -Filter "*.sql" | Sort-Object Name
if ($sqlFiles.Count -gt 0) {
Write-Host " $($folder.DisplayName): $($sqlFiles.Count) files" -ForegroundColor DarkGray
foreach ($file in $sqlFiles) {
# Add GO before Views and Stored Procedures for SQL Server
if ($providerName -eq "SqlServer" -and $folder.Name -match "View|Stored") {
"GO" | Out-File $outputFile -Append -Encoding utf8
}
# Add separators to prevent "bleeding" between statements
"`n;" | Out-File $outputFile -Append -Encoding utf8
$content = Get-Content $file.FullName -Raw
$content | Out-File $outputFile -Append -Encoding utf8
"`n;" | Out-File $outputFile -Append -Encoding utf8
# Add GO after Views and Stored Procedures for SQL Server
if ($providerName -eq "SqlServer" -and $folder.Name -match "View|Stored") {
"GO" | Out-File $outputFile -Append -Encoding utf8
}
}
}
}
# Special handling for SQLite - add footer
if ($providerName -eq "Sqlite") {
"COMMIT;" | Out-File $outputFile -Append -Encoding utf8
"PRAGMA foreign_keys = ON;" | Out-File $outputFile -Append -Encoding utf8
}
Write-Host " -> $outputFile" -ForegroundColor Green
# Get and display connection string
$connStr = Get-ConnectionString -Provider $providerName -DatabaseName $dbName
Write-Host " Connection: $connStr" -ForegroundColor DarkGray
}
}
# Main
Write-Host "=== SQL Script Builder ===" -ForegroundColor Magenta
Write-Host "Source: $SrcDir"
Write-Host "Output: $DistDir`n" -ForegroundColor Magenta
# Find all database directories
$databases = Get-ChildItem -Directory -Path $SrcDir | Where-Object {
$_.Name -notin $IgnoreDirs
}
if ($databases.Count -eq 0) {
Write-Host "No database directories found in $SrcDir" -ForegroundColor Red
exit 1
}
Write-Host "Found databases: $($databases.Name -join ', ')" -ForegroundColor Yellow
# Create dist directory if needed
if (-not (Test-Path $DistDir)) {
New-Item -ItemType Directory -Path $DistDir -Force | Out-Null
}
# Build scripts for each database
foreach ($db in $databases) {
Build-DatabaseScripts -DatabaseDir $db
}
Write-Host "`n=== Build Complete ===" -ForegroundColor Magenta