This guide explains the Flyway migration structure used in our Spring Boot, MySQL, and Docker template repository.
Flyway is a database migration tool that helps you version control your database schema. In our project, we use Flyway to manage and apply database migrations automatically.
Our Flyway migrations are located in the migrations/ directory at the root of the project. This location is specified in our Docker Compose files:
SPRING_FLYWAY_LOCATIONS: filesystem:migrationsFlyway migration files follow a specific naming convention:
V{version_date}__{description}.sql
V: Indicates a versioned migration. You can also useUfor undo migration andRfor repeatable migration.{version_date}: The date of your migration change. This helps keep the migrations in a proper order (YYYY_MM_DD_HHMMSS)__: Double underscore separating version from description{description}: A brief description of the migration, using underscores for spaces
Example: V2024_09_26_082319__create_users_table.sql
Migrations are applied in the order of their version numbers. The version number is based on the timestamp in the filename. This ensures that migrations are applied in the correct sequence.
If you try and apply a pull request that contains a migration with a version number lower than the current version in the database, the workflow will fail. This is to prevent applying migrations out of order.
Each migration file contains SQL statements to modify the database schema. For example:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);Migrations are applied automatically when the application starts. Spring Boot's Flyway integration handles this process.
- Flyway checks the current state of the database.
- It looks for any new migration files that haven't been applied yet.
- It applies these new migrations in order, based on their version numbers.
-
Never modify existing migrations: Once a migration has been applied and committed, treat it as immutable. Create new migrations for further changes.
-
Use meaningful descriptions: The description in the filename should clearly indicate what the migration does.
-
Keep migrations small and focused: Each migration should do one thing, making it easier to understand and manage.
-
Version control your migrations: Always commit your migration files to your version control system.
-
Test migrations: Before applying migrations to production, test them thoroughly in a development or staging environment.
Here's an example of how your migrations might evolve:
V2024_09_26_082319__create_users_table.sqlV2024_09_26_134529__add_role_column_to_users.sqlV2024_09_27_063243_create_products_table.sqlV2024_09_28_223847__add_index_to_users_email.sql
Each of these would contain the necessary SQL to make the described changes.
By using Flyway, we ensure that our database schema is version controlled and that all environments (development, staging, production) stay in sync. This structure allows for easy tracking of database changes and simplifies the process of setting up new environments or updating existing ones.