0
# Callback System
1
2
Extensible lifecycle hooks enabling custom logic execution at any point during migration operations, providing comprehensive integration points for auditing, logging, and custom behavior.
3
4
## Capabilities
5
6
### Callback Interface
7
8
```java { .api }
9
/**
10
* Callback for receiving notifications before and after each migration.
11
*/
12
public interface Callback {
13
/** Whether this callback supports the given event and context */
14
boolean supports(Event event, Context context);
15
16
/** Handle the callback event */
17
void handle(Event event, Context context);
18
}
19
20
/**
21
* Base implementation of Callback with convenient defaults
22
*/
23
public abstract class BaseCallback implements Callback {
24
public boolean supports(Event event, Context context) {
25
return true; // Support all events by default
26
}
27
28
public abstract void handle(Event event, Context context);
29
}
30
```
31
32
### Callback Events
33
34
```java { .api }
35
/**
36
* Enumeration of all callback events
37
*/
38
public enum Event {
39
// Migration events
40
BEFORE_MIGRATE, AFTER_MIGRATE, BEFORE_EACH_MIGRATE, AFTER_EACH_MIGRATE,
41
BEFORE_EACH_MIGRATE_STATEMENT, AFTER_EACH_MIGRATE_STATEMENT,
42
43
// Validation events
44
BEFORE_VALIDATE, AFTER_VALIDATE,
45
46
// Clean events
47
BEFORE_CLEAN, AFTER_CLEAN,
48
49
// Baseline events
50
BEFORE_BASELINE, AFTER_BASELINE,
51
52
// Repair events
53
BEFORE_REPAIR, AFTER_REPAIR,
54
55
// Info events
56
BEFORE_INFO, AFTER_INFO,
57
58
// Undo events
59
BEFORE_UNDO, AFTER_UNDO, BEFORE_EACH_UNDO, AFTER_EACH_UNDO
60
}
61
```
62
63
### Callback Context
64
65
```java { .api }
66
/**
67
* The context relevant to a callback.
68
*/
69
public interface Context {
70
/** Get the configuration currently in use */
71
Configuration getConfiguration();
72
73
/** Get the JDBC connection to use to execute statements */
74
Connection getConnection();
75
76
/** Get info about the migration being executed (when available) */
77
MigrationInfo getMigrationInfo();
78
79
/** Get the SQL statement being executed (for statement-level events) */
80
Statement getStatement();
81
}
82
```
83
84
## Usage Examples
85
86
```java
87
// Audit callback
88
public class AuditCallback extends BaseCallback {
89
@Override
90
public void handle(Event event, Context context) {
91
if (event == Event.AFTER_EACH_MIGRATE) {
92
MigrationInfo info = context.getMigrationInfo();
93
System.out.println("Applied migration: " + info.getVersion() + " - " + info.getDescription());
94
}
95
}
96
}
97
98
// Configuration with callbacks
99
Flyway flyway = Flyway.configure()
100
.dataSource(dataSource)
101
.callbacks(new AuditCallback(), new LoggingCallback())
102
.load();
103
```