Video in progress

What is Flyway, and how are migration files named?

Flyway is a tool that manages the database structure through versioned SQL files. Files are named V<version>__<description>.sql, and when the application starts, the ones not yet applied run in order.

📖 Answer

Code is versioned in git, but the database often stays in a state of "who changed what by hand, and when". Flyway solves that: a schema change is also a file, like code, and goes through review.

src/main/resources/db/migration/
  V1__create_users.sql
  V2__create_orders.sql
  V3__add_phone_to_users.sql
  R__refresh_reports_view.sql
  • V is a versioned migration that runs once. After the version there are two underscores.
  • R is repeatable: it runs again when the file content changes (handy for VIEWs and functions).

Flyway keeps a flyway_schema_history table in the database: which version was applied, when, and with what checksum. In Spring Boot it's enough to add flyway-core (for PostgreSQL in Flyway 10+, also flyway-database-postgresql), and the migrations run automatically at application start.

Where Flyway is used, don't use spring.jpa.hibernate.ddl-auto=update; set validate or none. Otherwise two tools manage the schema at the same time.