-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlywayMigrationConfig.java.disabled
More file actions
57 lines (47 loc) · 2.06 KB
/
FlywayMigrationConfig.java.disabled
File metadata and controls
57 lines (47 loc) · 2.06 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
package com.example.azure_sql_demo.config;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import jakarta.annotation.PostConstruct;
import javax.sql.DataSource;
@Configuration
@Profile("docker") // Apenas para perfil docker (SQL Server)
public class FlywayMigrationConfig {
@Autowired
private DataSource dataSource;
@PostConstruct
public void migrateFlyway() {
try {
System.out.println("🚀 Iniciando migrações Flyway para SQL Server...");
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.locations("classpath:db/migration")
.baselineOnMigrate(true)
.validateOnMigrate(true)
.outOfOrder(false)
.cleanDisabled(false)
.load();
// Informações sobre migrações
var info = flyway.info();
System.out.println("📊 Status das migrações:");
for (var migration : info.all()) {
System.out.println(" - " + migration.getVersion() + ": " + migration.getDescription() + " [" + migration.getState() + "]");
}
// Executar migrações
var result = flyway.migrate();
System.out.println("✅ Flyway executado com sucesso! " + result.migrationsExecuted + " migrações aplicadas.");
// Mostrar migrações aplicadas
if (result.migrationsExecuted > 0) {
System.out.println("📋 Migrações aplicadas:");
for (var output : result.warnings) {
System.out.println(" ⚠️ " + output);
}
}
} catch (Exception e) {
System.err.println("❌ Erro no Flyway: " + e.getMessage());
e.printStackTrace();
throw new RuntimeException("Falha na migração do banco de dados", e);
}
}
}