Database migration with Flyway

David Kumar Jan 2026
6 tabs
CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    username VARCHAR(100) NOT NULL UNIQUE,
    email VARCHAR(255) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_username ON users(username);
6 files · sql, yaml, java Explain with highlit

Flyway manages database schema evolution through versioned SQL scripts. Migration files follow naming convention—V1__initial_schema.sql, V2__add_users_table.sql. Flyway tracks applied migrations in a schema history table. Migrations run automatically on application startup or via Maven/Gradle. Repeatable migrations use R__ prefix for views, procedures. Baseline existing databases with flyway.baseline-on-migrate. Validation ensures applied migrations match filesystem. Flyway supports SQL and Java-based migrations. The tool enables version control for database changes, making deployments repeatable and auditable. Multiple environments use same migrations with different data. Flyway integrates seamlessly with Spring Boot via auto-configuration. Proper migration strategy is crucial for production database management.