#1
This guide shows how to use Flyway migration with H2 database in Spring Boot for versioned and automated schema management.

1. Add Dependencies

In pom.xml:
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
</dependency>
<dependency>
  <groupId>org.flywaydb</groupId>
  <artifactId>flyway-core</artifactId>
</dependency>

2. Configure H2 and Flyway

In application.properties:
spring.datasource.url=jdbc:h2:mem:flywaydb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=none
spring.flyway.enabled=true

3. Create Migration Script

In src/main/resources/db/migration/V1__create_user_table.sql:
CREATE TABLE users (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL
);
Flyway will run this script automatically at startup.

4. Add Another Migration

src/main/resources/db/migration/V2__add_age_column.sql:
ALTER TABLE users ADD COLUMN age INT;
Each migration file must follow the naming pattern V<version>__<description>.sql.

5. Run Application

When the app starts, Flyway checks migrations and applies missing ones to the H2 database.

6. Verify

Enable the H2 console by adding:
spring.h2.console.enabled=true
Go to http://localhost:8080/h2-console to confirm the schema updates.

image quote pre code