🍃 Spring Boot Intermediate

What is Spring Boot Flyway/Liquibase database migration?

Answer

Database migration tools manage schema changes version-controlled, similar to VCS for database structure. Never use spring.jpa.hibernate.ddl-auto=update in production — use Flyway or Liquibase instead. Flyway (simpler — SQL migrations): add spring-boot-starter-flyway or flyway-core. Place migration files in src/main/resources/db/migration/. File naming: V{version}__{description}.sql: V1__Create_users_table.sql V2__Add_email_index.sql V3__Add_user_roles.sql. Spring Boot auto-runs Flyway on startup. Flyway tracks applied migrations in flyway_schema_history table. flyway.baseline-on-migrate=true for existing databases. Commands: mvn flyway:migrate, flyway:validate, flyway:repair. Configuration: spring.flyway.locations=classpath:db/migration spring.flyway.baseline-on-migrate=true spring.flyway.out-of-order=false. Liquibase (more flexible — XML/YAML/JSON/SQL changesets): add liquibase-core. Place db/changelog/db.changelog-master.yaml: databaseChangeLog: - include: file: db/changelog/001-create-users.yaml - include: file: db/changelog/002-add-email-index.yaml. Each changeset has an ID and author. Liquibase tracks in databasechangelog table. Best practices: never modify applied migrations; create new migration for every change; run in CI/CD; test migrations against production-like data; rollback scripts for Flyway Pro/Liquibase.