This guide shows how to manage
database migrations with
Flyway in a
Spring Boot project using
SAP HANA Express.
1. Add Flyway Dependency
In
pom.xml
:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
2. Configure application.properties
spring.datasource.url=jdbc:sap://localhost:39015/?databaseName=HXE
spring.datasource.username=SYSTEM
spring.datasource.password=YourPassword
spring.datasource.driver-class-name=com.sap.db.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none
spring.flyway.enabled=true
spring.flyway.locations=classpath:db/migration
Set
ddl-auto=none
to prevent Hibernate from auto-creating tables.
3. Create Migration Script
In
src/main/resources/db/migration
, create
V1__init.sql
:
CREATE TABLE PRODUCT (
ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
NAME NVARCHAR(255) NOT NULL,
PRICE DECIMAL(10,2) NOT NULL
);
Flyway runs this automatically on app startup.
4. Add Another Migration
Create
V2__add_column.sql
:
ALTER TABLE PRODUCT ADD DESCRIPTION NVARCHAR(500);
When restarting the app, Flyway applies this migration incrementally.
5. Test in Spring Boot
Create entity:
import jakarta.persistence.*;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;
private String description;
// getters and setters
}
Repository:
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductRepository extends JpaRepository<Product, Long> {
}
6. Run the Application
mvn spring-boot:run
Flyway will check migration history and apply pending scripts.
image quote pre code