# Compare sql/*.sql migrations against db.sql $dbPath = "c:\Users\wilso\Desktop\oms0414-back\db.sql" $sqlDir = "c:\Users\wilso\Desktop\oms0414-back\sql" $db = [IO.File]::ReadAllText($dbPath) function HasCol([string]$name) { return $db.Contains("`$name`".Replace('$name', $name)) -or $db.Contains("``$name``") } # simpler: look for `ColName` function ColInDb([string]$name) { return $db.IndexOf("`" + $name + "`" ) -ge 0 -or $db.IndexOf("``$name``") -ge 0 } function ColInDb2([string]$name) { $needle = [char]96 + $name + [char]96 # backtick Col backtick return $db.Contains($needle) } function TableInDb([string]$name) { $needle = "CREATE TABLE " + [char]96 + $name + [char]96 return $db.Contains($needle) } $files = Get-ChildItem "$sqlDir\*.sql" | Where-Object { $_.Name -notmatch 'menu_notes' } | Sort-Object Name $missing = @() foreach ($f in $files) { $content = [IO.File]::ReadAllText($f.FullName) $createMatches = [regex]::Matches($content, 'CREATE TABLE(?: IF NOT EXISTS)?\s+`([^`]+)`', 'IgnoreCase') $addMatches = [regex]::Matches($content, 'ADD COLUMN(?: IF NOT EXISTS)?\s+`([^`]+)`', 'IgnoreCase') $dropMatches = [regex]::Matches($content, 'DROP COLUMN\s+`([^`]+)`', 'IgnoreCase') $modMatches = [regex]::Matches($content, 'MODIFY COLUMN\s+`([^`]+)`', 'IgnoreCase') $section = @() foreach ($m in $createMatches) { $t = $m.Groups[1].Value if (-not (TableInDb $t)) { $section += "MISSING TABLE: $t" $missing += "$($f.Name) -> TABLE $t" } else { $section += "OK TABLE: $t" } } foreach ($m in $addMatches) { $c = $m.Groups[1].Value if (-not (ColInDb2 $c)) { $section += "MISSING COL (ADD): $c" $missing += "$($f.Name) -> ADD $c" } else { $section += "OK COL (ADD): $c" } } foreach ($m in $dropMatches) { $c = $m.Groups[1].Value if (ColInDb2 $c) { $section += "STILL IN db (DROP intended): $c" $missing += "$($f.Name) -> DROP not applied in db.sql: $c" } else { $section += "OK DROPPED: $c" } } foreach ($m in $modMatches) { $c = $m.Groups[1].Value if (-not (ColInDb2 $c)) { $section += "MISSING COL (MODIFY): $c" $missing += "$($f.Name) -> MODIFY $c (col absent)" } } if ($section.Count -gt 0) { Write-Output "==== $($f.Name) ====" $section | ForEach-Object { Write-Output " $_" } } } Write-Output "" Write-Output "==== SUMMARY MISSING ====" if ($missing.Count -eq 0) { Write-Output "(none)" } else { $missing | ForEach-Object { Write-Output $_ } }