# Script d'audit simplifié des migrations Flyway vs Entités JPA # Auteur: Lions Dev # Date: 2026-03-13 $ErrorActionPreference = "Stop" $projectRoot = $PSScriptRoot Write-Host "`n╔══════════════════════════════════════════════════════════╗" -ForegroundColor Cyan Write-Host "║ Audit Migrations Flyway vs Entités JPA - UnionFlow ║" -ForegroundColor Cyan Write-Host "╚══════════════════════════════════════════════════════════╝`n" -ForegroundColor Cyan # 1. Extraire toutes les entités et leurs noms de tables Write-Host "[1/3] Extraction des entités JPA..." -ForegroundColor Yellow $entityFiles = Get-ChildItem -Path "$projectRoot\src\main\java\dev\lions\unionflow\server\entity" -Filter "*.java" -Recurse | Where-Object { $_.Name -ne "package-info.java" } $entities = @{} foreach ($file in $entityFiles) { $content = Get-Content $file.FullName -Raw # Extraire le nom de la classe if ($content -match 'class\s+(\w+)') { $className = $Matches[1] $tableName = $null # Chercher @Table(name="...") if ($content -match '@Table.*name\s*=\s*"(\w+)"') { $tableName = $Matches[1] } else { # Conversion basique du nom de classe vers snake_case # Organisation -> organisation, TransactionApproval -> transaction_approval $temp = $className -replace '([a-z])([A-Z])', '$1_$2' $tableName = $temp.ToLower() } $entities[$className] = $tableName Write-Host " → $className : $tableName" -ForegroundColor Gray } } Write-Host " → $($entities.Count) entités trouvées" -ForegroundColor Green # 2. Lister toutes les migrations et extraire les tables Write-Host "`n[2/3] Analyse des migrations Flyway..." -ForegroundColor Yellow $migrations = Get-ChildItem -Path "$projectRoot\src\main\resources\db\migration" -Filter "V*.sql" | Sort-Object Name $allTables = @{} foreach ($migration in $migrations) { Write-Host " → $($migration.Name)" -ForegroundColor Gray $content = Get-Content $migration.FullName -Raw # Méthode simple : chercher ligne par ligne $lines = Get-Content $migration.FullName foreach ($line in $lines) { if ($line -match '^\s*CREATE\s+TABLE') { # Extraire le nom de la table if ($line -match 'TABLE\s+(IF\s+NOT\s+EXISTS\s+)?(\w+)') { $tableName = $Matches[2] if (-not $allTables.ContainsKey($tableName)) { $allTables[$tableName] = @() } $allTables[$tableName] += $migration.Name Write-Host " • $tableName" -ForegroundColor DarkGray } } } } Write-Host " → $($allTables.Keys.Count) tables uniques trouvées dans les migrations" -ForegroundColor Green # 3. Comparaison Write-Host "`n[3/3] Comparaison et génération du rapport..." -ForegroundColor Yellow $report = @" # Rapport d'Audit - Migrations Flyway vs Entités JPA Date: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss") ## Résumé - **Entités JPA**: $($entities.Count) - **Tables dans migrations**: $($allTables.Keys.Count) --- ## 1. Entités JPA et leurs tables attendues | Entité | Table attendue | Existe dans migrations? | Migrations | |--------|----------------|------------------------|------------| "@ $okCount = 0 $missingCount = 0 foreach ($entity in $entities.Keys | Sort-Object) { $tableName = $entities[$entity] $exists = $allTables.ContainsKey($tableName) $migrations = if ($exists) { $allTables[$tableName] -join ", " } else { "❌ MANQUANT" } if ($exists) { $okCount++ $report += "`n| $entity | ``$tableName`` | ✅ Oui | $migrations |" } else { $missingCount++ $report += "`n| **$entity** | ``$tableName`` | **❌ NON** | - |" } } $report += @" **Résultat**: $okCount/$($entities.Count) entités ont une table correspondante, $missingCount manquantes. --- ## 2. Tables dans migrations SANS entité correspondante "@ $orphanTables = @() foreach ($table in $allTables.Keys | Sort-Object) { $found = $false foreach ($entity in $entities.Keys) { if ($entities[$entity] -eq $table) { $found = $true break } } if (-not $found) { $orphanTables += @{ Table = $table Migrations = $allTables[$table] -join ", " } } } if ($orphanTables.Count -gt 0) { $report += "**⚠️ ATTENTION**: $($orphanTables.Count) tables n'ont pas d'entité correspondante!`n`n" $report += "| Table | Migration(s) | Action recommandée |`n" $report += "|-------|--------------|-------------------|`n" foreach ($item in $orphanTables) { $report += "| ``$($item.Table)`` | $($item.Migrations) | Supprimer ou créer entité |`n" } } else { $report += "✅ Toutes les tables ont une entité correspondante.`n" } $report += @" --- ## 3. Duplications de CREATE TABLE "@ $duplicates = $allTables.GetEnumerator() | Where-Object { $_.Value.Count -gt 1 } if ($duplicates.Count -gt 0) { $report += "**⚠️ ATTENTION**: $($duplicates.Count) tables sont créées dans plusieurs migrations!`n`n" $report += "| Table | Migrations | Action recommandée |`n" $report += "|-------|------------|-------------------|`n" foreach ($dup in $duplicates | Sort-Object { $_.Key }) { $report += "| ``$($dup.Key)`` | $($dup.Value -join ", ") | Garder seulement une version |`n" } } else { $report += "✅ Aucune duplication détectée.`n" } $report += @" --- ## Recommandations de nettoyage ### Priorité 1 - Tables manquantes ($missingCount) "@ if ($missingCount -gt 0) { $report += "`nCréer les tables manquantes pour ces entités :`n`n" foreach ($entity in $entities.Keys | Sort-Object) { $tableName = $entities[$entity] if (-not $allTables.ContainsKey($tableName)) { $report += "- [ ] ``$tableName`` (entité: $entity)`n" } } } else { $report += "`n✅ Aucune table manquante.`n" } $report += @" ### Priorité 2 - Tables obsolètes ($($orphanTables.Count)) "@ if ($orphanTables.Count -gt 0) { $report += "`nSupprimer ces tables des migrations (ou créer les entités) :`n`n" foreach ($item in $orphanTables | Sort-Object { $_.Table }) { $report += "- [ ] ``$($item.Table)`` (migrations: $($item.Migrations))`n" } } else { $report += "`n✅ Aucune table obsolète.`n" } $report += @" ### Priorité 3 - Duplications ($($duplicates.Count)) "@ if ($duplicates.Count -gt 0) { $report += "`nÉliminer les duplications en gardant seulement la version avec IF NOT EXISTS :`n`n" foreach ($dup in $duplicates | Sort-Object { $_.Key }) { $report += "- [ ] ``$($dup.Key)`` → Nettoyer dans: $($dup.Value -join ", ")`n" } } else { $report += "`n✅ Aucune duplication.`n" } $report += @" --- *Généré par audit-migrations-simple.ps1 - Lions Dev* "@ # Sauvegarder le rapport $reportPath = "$projectRoot\AUDIT_MIGRATIONS.md" $report | Out-File -FilePath $reportPath -Encoding UTF8 Write-Host "`n✅ Rapport sauvegardé: $reportPath" -ForegroundColor Green # Résumé Write-Host "`n╔══════════════════════════════════════════════════════════╗" -ForegroundColor Cyan Write-Host "║ RÉSUMÉ ║" -ForegroundColor Cyan Write-Host "╚══════════════════════════════════════════════════════════╝" -ForegroundColor Cyan Write-Host " ✅ OK: $okCount/$($entities.Count)" -ForegroundColor Green Write-Host " ❌ Tables manquantes: $missingCount" -ForegroundColor $(if ($missingCount -gt 0) { "Yellow" } else { "Green" }) Write-Host " ⚠️ Tables orphelines: $($orphanTables.Count)" -ForegroundColor $(if ($orphanTables.Count -gt 0) { "Yellow" } else { "Green" }) Write-Host " ⚠️ Duplications: $($duplicates.Count)" -ForegroundColor $(if ($duplicates.Count -gt 0) { "Red" } else { "Green" }) Write-Host "`n📄 Rapport complet: $reportPath`n" -ForegroundColor Cyan