Skip to content

Fix Flyway Callback Script – Callback Not Executing

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Fix Flyway Callback Script. We cover key concepts, practical examples, and best practices.

You add a Flyway callback script — beforeMigrate.sql or afterEachMigrate.sql — to the migrations directory. You run flyway migrate, and the callback doesn't execute. No errors, no log output — it's completely ignored.

Wrong ❌

-- beforeMigrate.sql — placed in the migrations directory
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly_user;

```bash
flyway migrate

Output:

Successfully applied 0 migrations (execution time 00:00.045s)

The `beforeMigrate.sql` callback didn't execute. The `readonly_user` has no new privileges.

## Right ✅

**Place callbacks in the correct directory:**

```bash
flyway.locations=filesystem:./migrations,callbacks:./callbacks

project/
├── migrations/
│   ├── V1__create_users.sql
│   └── V2__add_index.sql
└── callbacks/
    ├── beforeMigrate.sql
    └── afterMigrate.sql

**Or use the default directory (same as migrations) with correct naming:**

Flyway looks for callback scripts in the **same locations as migrations**. But the filename must match the exact callback event:

```bash
# In the migrations directory:
migrations/
├── beforeMigrate.sql         ← executes before flyway migrate
├── afterMigrate.sql          ← executes after flyway migrate
├── beforeEachMigrate.sql     ← before each individual migration
├── afterEachMigrate.sql      ← after each individual migration
├── V1__create_users.sql
└── V2__add_index.sql

**Verify callback execution:**

```bash
flyway migrate -outputType=json
# Look for "callbacks" in the JSON output

Or check logs:

```bash
flyway migrate -X   # debug mode

[DEBUG] Executing SQL callback: beforeMigrate
[DEBUG] Executing SQL callback: afterMigrate

**Available callback events:**

| Filename | When it runs |
|----------|-------------|
| `beforeMigrate.sql` | Before all migrations |
| `afterMigrate.sql` | After all migrations |
| `beforeEachMigrate.sql` | Before each versioned migration |
| `afterEachMigrate.sql` | After each versioned migration |
| `beforeRepeatables.sql` | Before repeatable migrations |
| `afterRepeatables.sql` | After repeatable migrations |
| `beforeValidate.sql` | Before validate |
| `afterValidate.sql` | After validate |
| `beforeClean.sql` | Before clean |
| `afterClean.sql` | After clean |

**If the callback SQL has an error — Flyway may ignore it:**

```sql
-- afterMigrate.sql — intentionally broken
BROKEN SQL STATEMENT;

Flyway continues even if the callback fails (unless configured otherwise):

```bash
flyway.failOnMissingLocations=true
# Callback failures fail the migration

## Root Cause

Flyway callbacks must use an exact filename matching a callback event (`beforeMigrate.sql`, `afterEachMigrate.sql`, etc.). Placing them in a directory that's not in `flyway.locations`, or naming them incorrectly (e.g., `before_migrate.sql`), causes Flyway to silently ignore them.

## Prevention

- Use a separate callbacks directory configured in `flyway.locations`.
- Name callback files exactly as specified in Flyway's documentation.
- Run with `-X` (debug) to confirm callbacks are detected and executed.
- Test callback execution by including a `RAISE` or `SELECT` that produces output.


## Common Mistakes with callback script

1. **Mixing let bindings with <- bindings in do notation, producing type errors**
2. **Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors**
3. **Non-exhaustive pattern matches that compile with warnings then crash at runtime**

These mistakes appear frequently in real-world FLYWAY code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

## Practice Exercise

**Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.**

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

## FAQ

<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Can callbacks be written in SQL only?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Yes  SQL callbacks are the simplest. Flyway Teams also supports Java and Python callbacks.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: What happens if a callback fails?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: By default, Flyway logs the error but continues. Set <code>flyway.failOnMissingLocations=true</code> to fail on callback errors.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Can I use callbacks in Flyway Community?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Yes  SQL callbacks are available in Community edition.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: How do I pass variables to a callback?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Flyway placeholders are available in callbacks: <code>${flyway.user}</code>, <code>${flyway.schema}</code>.</p>
</div></details>

---

*Callback scripts are covered in the [DodaTech Flyway Automation course](https://dodatech.com/courses/flyway).*

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro