Flyway quản lý database schema migration dưới dạng versioned SQL files — rollback, audit trail, reproducible schema.
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>Tạo migration script:
src/main/resources/db/migration/
V1__create_users_table.sql
V2__add_email_index.sql
V3__add_profile_table.sqlNaming convention: V{version}__{description}.sql
-- V1__create_users_table.sql
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);Spring Boot auto-run: Flyway tự chạy khi app startup — apply tất cả pending migration theo thứ tự version.
spring:
flyway:
locations: classpath:db/migration
baseline-on-migrate: true # khi DB đã có schema cũ
validate-on-migrate: true # checksum mismatch → failFlyway lưu trạng thái trong bảng flyway_schema_history — biết migration nào đã chạy.
Không sửa migration đã chạy — checksum thay đổi → Flyway fail. Tạo file mới V4__alter_... thay thế.
Vs Liquibase: Flyway đơn giản hơn (SQL thuần), Liquibase flexible hơn (YAML/XML, rollback tốt hơn).
Flyway manages database schema migrations as versioned SQL files — rollbacks, audit trail, reproducible schemas.
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>Creating migration scripts:
src/main/resources/db/migration/
V1__create_users_table.sql
V2__add_email_index.sql
V3__add_profile_table.sqlNaming convention: V{version}__{description}.sql
-- V1__create_users_table.sql
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);Spring Boot auto-run: Flyway runs on app startup — applies all pending migrations in version order.
spring:
flyway:
locations: classpath:db/migration
baseline-on-migrate: true # when an existing schema is already present
validate-on-migrate: true # checksum mismatch → fail fastFlyway tracks state in the flyway_schema_history table — knows which migrations have run.
Never edit a migration that has already run — checksum changes → Flyway fails. Create a new V4__alter_... file instead.
Flyway vs Liquibase: Flyway is simpler (plain SQL), Liquibase is more flexible (YAML/XML, better rollbacks).