-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManage_Config_Tables.ps1
More file actions
274 lines (231 loc) · 9.54 KB
/
Manage_Config_Tables.ps1
File metadata and controls
274 lines (231 loc) · 9.54 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
#Requires -Version 7.0
<#
.SYNOPSIS
Konfiguration Manager - Firebird Tabellen Auswahl (Toggle Logik)
.DESCRIPTION
Dieses Skript liest alle Tabellen aus Firebird aus.
Logik:
- Tabellen auswählen, die GEÄNDERT werden sollen.
- Ist eine Tabelle NOCH NICHT in der Config -> Wird HINZUGEFÜGT.
- Ist eine Tabelle BEREITS in der Config -> Wird ENTFERNT.
- Nicht ausgewählte Tabellen bleiben UNVERÄNDERT.
.NOTES
Version: 2.0 (Refactored - Modul-basiert)
.LINK
https://github.com/gitnol/PSFirebirdToMSSQL
#>
# -----------------------------------------------------------------------------
# 0. MODUL IMPORTIEREN
# -----------------------------------------------------------------------------
$ScriptDir = $PSScriptRoot
$ModulePath = Join-Path $ScriptDir "SQLSyncCommon.psm1"
if (-not (Test-Path $ModulePath)) {
Write-Error "KRITISCH: SQLSyncCommon.psm1 nicht gefunden in $ScriptDir"
exit 1
}
Import-Module $ModulePath -Force
# -----------------------------------------------------------------------------
# 1. KONFIGURATION LADEN
# -----------------------------------------------------------------------------
$ConfigPath = Join-Path $ScriptDir "config.json"
if (-not (Test-Path $ConfigPath)) {
Write-Error "config.json fehlt!"
exit 1
}
# Raw Config laden (für Modifikation)
$ConfigJsonContent = Get-Content -Path $ConfigPath -Raw
$Config = $ConfigJsonContent | ConvertFrom-Json
# Aktuelle Tabellenliste
$CurrentTables = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
if ($Config.Tables) {
$Config.Tables | ForEach-Object { [void]$CurrentTables.Add($_) }
}
# Column Configuration (v2.10)
$RawIdColumn = Get-ConfigValue $Config.General "IdColumn" "ID"
$IdColumn = Protect-SqlString $RawIdColumn # Testweise SQL-Injection Schutz, müsste aber an vielen anderen Stellen auch gemacht werden
$RawTimestampColumns = @(Get-ConfigValue $Config.General "TimestampColumns" @("GESPEICHERT"))
$TimestampColumns = $RawTimestampColumns | ForEach-Object { Protect-SqlString $_ } # Testweise SQL-Injection Schutz, müsste aber an vielen anderen Stellen auch gemacht werden
# -----------------------------------------------------------------------------
# 2. CREDENTIALS AUFLÖSEN
# -----------------------------------------------------------------------------
try {
$FbCreds = Resolve-FirebirdCredentials -Config $Config
}
catch {
Write-Error $_.Exception.Message
exit 5
}
# -----------------------------------------------------------------------------
# 3. TREIBER LADEN
# -----------------------------------------------------------------------------
try {
$DllPath = Get-ConfigValue $Config.Firebird "DllPath" ""
$ResolvedDllPath = Initialize-FirebirdDriver -DllPath $DllPath -ScriptDir $ScriptDir
}
catch {
Write-Error $_.Exception.Message
exit 3
}
# -----------------------------------------------------------------------------
# 4. FIREBIRD DATEN ABRUFEN
# -----------------------------------------------------------------------------
$FBServer = $Config.Firebird.Server
$FBDatabase = $Config.Firebird.Database
$FBPort = Get-ConfigValue $Config.Firebird "Port" 3050
$FBCharset = Get-ConfigValue $Config.Firebird "Charset" "UTF8"
$ConnectionString = New-FirebirdConnectionString `
-Server $FBServer `
-Database $FBDatabase `
-Username $FbCreds.Username `
-Password $FbCreds.Password `
-Port $FBPort `
-Charset $FBCharset
$TableList = @()
$FbConn = $null
try {
Write-Host "Verbinde zu Firebird ($FBServer)..." -ForegroundColor Cyan
$FbConn = New-Object FirebirdSql.Data.FirebirdClient.FbConnection($ConnectionString)
$FbConn.Open()
# Dynamische Bedingung für Timestamp-Spalten (wird via Format-Operator eingefügt)
$TsConditions = ($TimestampColumns | ForEach-Object { "TRIM(FLD.RDB`$FIELD_NAME) = '$_'" }) -join " OR "
# Fallback auf "1=0" (nie wahr), falls keine Timestamp-Spalten konfiguriert
if (-not $TsConditions) { $TsConditions = "1=0" }
# Literal Here-String mit Format-Operator für sichere RDB$-Referenzen
$Sql = @'
SELECT
TRIM(REL.RDB$RELATION_NAME) as TABELLENNAME,
MAX(CASE WHEN TRIM(FLD.RDB$FIELD_NAME) = '{0}' THEN 1 ELSE 0 END) as HAT_ID,
MAX(CASE WHEN {1} THEN 1 ELSE 0 END) as HAT_DATUM
FROM RDB$RELATIONS REL
LEFT JOIN RDB$RELATION_FIELDS FLD ON REL.RDB$RELATION_NAME = FLD.RDB$RELATION_NAME
WHERE REL.RDB$SYSTEM_FLAG = 0
AND REL.RDB$VIEW_BLR IS NULL
GROUP BY REL.RDB$RELATION_NAME
ORDER BY REL.RDB$RELATION_NAME
'@ -f $IdColumn, $TsConditions
$Cmd = $FbConn.CreateCommand()
$Cmd.CommandText = $Sql
$Reader = $Cmd.ExecuteReader()
while ($Reader.Read()) {
$Name = $Reader["TABELLENNAME"]
$HatId = [int]$Reader["HAT_ID"] -eq 1
$HatDatum = [int]$Reader["HAT_DATUM"] -eq 1
$Status = "Neu"
if ($CurrentTables.Contains($Name)) {
$Status = "Aktiv (Konfiguriert)"
}
$Hinweis = ""
if (-not $HatId) { $Hinweis = "ACHTUNG: Keine $IdColumn Spalte (Snapshot Modus)" }
elseif (-not $HatDatum) { $Hinweis = "Warnung: Kein Timestamp (Full Merge)" }
$TableList += [PSCustomObject]@{
Aktion = if ($Status -like "Aktiv*") { "Löschen bei Auswahl" } else { "Hinzufügen bei Auswahl" }
Tabelle = $Name
Status = $Status
"Hat ID" = $HatId
"Hat Datum" = $HatDatum
Hinweis = $Hinweis
}
}
$Reader.Close()
}
catch {
Write-Error "Fehler beim Lesen der Firebird-Metadaten: $($_.Exception.Message)"
if ($_.Exception.InnerException) {
Write-Host "Details: $($_.Exception.InnerException.Message)" -ForegroundColor Red
}
exit 2
}
finally {
# WICHTIG: Connection immer aufräumen
if ($FbConn) {
try { $FbConn.Close() } catch { }
try { $FbConn.Dispose() } catch { }
}
}
# -----------------------------------------------------------------------------
# 5. GUI AUSWAHL
# -----------------------------------------------------------------------------
Write-Host "Öffne Auswahlfenster..." -ForegroundColor Yellow
Write-Host "ANLEITUNG (TOGGLE MODUS):" -ForegroundColor White
Write-Host "1. Wählen Sie die Tabellen aus, deren Status Sie ÄNDERN wollen."
Write-Host " - Neue Tabellen auswählen -> Werden HINZUGEFÜGT."
Write-Host " - Aktive Tabellen auswählen -> Werden ENTFERNT."
Write-Host "2. Nicht ausgewählte Tabellen bleiben UNVERÄNDERT."
$SelectedItems = $TableList | Sort-Object Status, Tabelle | Out-GridView -Title "Tabellen zum Ändern auswählen (Toggle: Add/Remove)" -PassThru
if (-not $SelectedItems) {
Write-Host "Keine Auswahl getroffen. Keine Änderungen." -ForegroundColor Yellow
exit 0
}
# -----------------------------------------------------------------------------
# 6. TOGGLE LOGIK
# -----------------------------------------------------------------------------
$SelectedNames = $SelectedItems | Select-Object -ExpandProperty Tabelle
$TablesToAdd = @()
$TablesToRemove = @()
$FinalTableList = [System.Collections.Generic.List[string]]::new()
# Bestehende Liste übernehmen (Standard: Behalten)
foreach ($Tab in $Config.Tables) {
if ($Tab -in $SelectedNames) {
# War drin UND wurde ausgewählt -> LÖSCHEN
$TablesToRemove += $Tab
}
else {
# War drin UND NICHT ausgewählt -> BEHALTEN
$FinalTableList.Add($Tab)
}
}
# Neue hinzufügen
foreach ($Sel in $SelectedNames) {
if ($Sel -notin $Config.Tables) {
# War NICHT drin UND wurde ausgewählt -> HINZUFÜGEN
$TablesToAdd += $Sel
$FinalTableList.Add($Sel)
}
}
# Sortieren
$FinalTableList.Sort()
# -----------------------------------------------------------------------------
# 7. VORSCHAU & BESTÄTIGUNG
# -----------------------------------------------------------------------------
if ($TablesToAdd.Count -eq 0 -and $TablesToRemove.Count -eq 0) {
Write-Host "Keine effektiven Änderungen." -ForegroundColor Yellow
exit 0
}
Write-Host "GEPLANTE ÄNDERUNGEN:" -ForegroundColor Cyan
if ($TablesToAdd.Count -gt 0) {
Write-Host " [+] Hinzufügen ($($TablesToAdd.Count)):" -ForegroundColor Green
$TablesToAdd | ForEach-Object { Write-Host " $_" -ForegroundColor Green }
}
if ($TablesToRemove.Count -gt 0) {
Write-Host " [-] Entfernen ($($TablesToRemove.Count)):" -ForegroundColor Red
$TablesToRemove | ForEach-Object { Write-Host " $_" -ForegroundColor Red }
}
Write-Host "Soll diese Änderung angewendet werden?" -ForegroundColor White
$Choice = ""
while ($Choice -notin "J", "N") {
$Choice = Read-Host "[J]a, speichern / [N]ein, abbrechen"
$Choice = $Choice.ToUpper()
}
if ($Choice -eq "N") {
Write-Host "Abbruch." -ForegroundColor Yellow
exit 0
}
# -----------------------------------------------------------------------------
# 8. SPEICHERN
# -----------------------------------------------------------------------------
Write-Host "Erstelle Backup und speichere..." -ForegroundColor Cyan
$Timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$BackupPath = "$ConfigPath.$Timestamp.bak"
Copy-Item -Path $ConfigPath -Destination $BackupPath
if (Test-Path $BackupPath) {
$Config.Tables = $FinalTableList
$FinalJson = $Config | ConvertTo-Json -Depth 10
Set-Content -Path $ConfigPath -Value $FinalJson
Write-Host "ERFOLG: config.json aktualisiert." -ForegroundColor Green
Write-Host "Anzahl Tabellen jetzt: $($FinalTableList.Count)" -ForegroundColor Green
Write-Host "Backup erstellt: $BackupPath" -ForegroundColor Gray
}
else {
Write-Error "Backup fehlgeschlagen. Abbruch."
exit 4
}