Java.DBMigrationTools.What is flyway.migrate() used for?

**flyway.migrate()** is the core method in Flyway used to apply pending migrations to your database.

It scans the migration files, checks which ones haven’t been applied (based on flyway_schema_history), and executes them in order.


🧠 When Would You Use It?

  • In Java code (e.g. Spring, CLI tools, or tests) to trigger migrations programmatically
  • As an alternative to the CLI command:
flyway migrate

🛠️ Example (Java API)

Flyway flyway = Flyway.configure()
    .dataSource("jdbc:postgresql://localhost:5432/mydb", "user", "password")
    .load();

flyway.migrate();

✅ This connects to the database
✅ Looks for migration files in the default location (db/migration)
✅ Applies any unapplied V__ or R__ migration files
✅ Logs them in flyway_schema_history


🔍 What Does It Actually Do?

  1. Scans migration files (in classpath or specified location)
  2. Checks what’s already applied (via flyway_schema_history)
  3. Validates checksums and version order
  4. Executes unapplied versioned and repeatable migrations
  5. Updates the tracking table with new entries

🚫 It Will Not:

  • Re-run already executed versioned migrations
  • Run undo scripts (U__) — those require explicit commands
  • Automatically repair broken checksums — for that, use flyway.repair()

✅ When to Use flyway.migrate() (Instead of CLI)

  • Inside a Spring Boot app’s startup logic
  • In JUnit tests that need fresh schema setup
  • In custom admin tools or bootstrap scripts

📌 Key Takeaways

flyway.migrate() is the Java method to apply migrations programmatically
✅ It works just like the flyway migrate CLI command
✅ Applies pending versioned and repeatable migrations, logs them, and ensures safe, idempotent schema changes

This entry was posted in Без рубрики. Bookmark the permalink.