From 0b1b815cf41186420a35258b748b15653735f157 Mon Sep 17 00:00:00 2001 From: Nano Taboada Date: Fri, 10 Apr 2026 11:25:35 -0300 Subject: [PATCH 1/7] feat(db): integrate Flyway for database migrations (#130) Co-authored-by: Claude Sonnet 4.6 --- CHANGELOG.md | 11 +++++ README.md | 47 +++++++++++++++++++ pom.xml | 23 +++++++++ src/main/resources/application.properties | 13 ++++- .../db/migration/V1__Create_players_table.sql | 21 +++++++++ .../db/migration/V2__Seed_starting11.sql | 16 +++++++ .../db/migration/V3__Seed_substitutes.sql | 20 ++++++++ src/test/resources/application.properties | 4 ++ 8 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/db/migration/V1__Create_players_table.sql create mode 100644 src/main/resources/db/migration/V2__Seed_starting11.sql create mode 100644 src/main/resources/db/migration/V3__Seed_substitutes.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index 17e1a6e..2bb1d23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,17 @@ Release names follow the **historic football clubs** naming convention (A–Z): ### Added +- Integrate Flyway for database schema versioning and automated migrations; + add `flyway-core` and `flyway-database-postgresql` to `pom.xml`; create + migration directory `src/main/resources/db/migration/` with three versioned + scripts: `V1__Create_players_table.sql` (schema), `V2__Seed_starting11.sql` + (11 Starting XI players), `V3__Seed_substitutes.sql` (15 substitute players); + configure `spring.flyway.enabled=true` with `baseline-on-migrate=true` for + backwards compatibility with existing databases; disable Flyway in test + environment which continues to use SQLite in-memory with `ddl.sql`/`dml.sql`; + switch `spring.jpa.hibernate.ddl-auto` from `none` to `validate` so Hibernate + verifies entity mappings against the Flyway-managed schema (#130) + ### Changed - Switch runtime base image from `eclipse-temurin:25-jdk-alpine` to diff --git a/README.md b/README.md index fab0231..83e3665 100644 --- a/README.md +++ b/README.md @@ -269,6 +269,53 @@ spring.datasource.url=jdbc:sqlite::memory: spring.jpa.hibernate.ddl-auto=create-drop ``` +## Database Migrations + +Schema versioning is managed by [Flyway](https://documentation.red-gate.com/flyway), which runs automatically on application startup and applies any pending migrations in order. + +### Migration files + +Versioned SQL scripts live in `src/main/resources/db/migration/` and follow the Flyway naming convention: + +``` +V{version}__{description}.sql +``` + +| File | Description | +| ---- | ----------- | +| `V1__Create_players_table.sql` | Creates the `players` table (schema) | +| `V2__Seed_starting11.sql` | Seeds 11 Starting XI players (`starting11 = 1`) | +| `V3__Seed_substitutes.sql` | Seeds 15 Substitute players (`starting11 = 0`) | + +All migration SQL is written to be compatible with both **SQLite** (local dev) and **PostgreSQL** (see #286). + +### Adding a new migration + +Create a new file in `src/main/resources/db/migration/` with the next version number: + +```bash +touch src/main/resources/db/migration/V4__Add_nationality_column.sql +``` + +Flyway applies it automatically on the next application startup. View the applied history by querying the `flyway_schema_history` table. + +### Existing databases + +`baseline-on-migrate=true` ensures that databases created before Flyway was introduced are recognised as already at `V3` (schema + full seed data), so no migrations run against them. Fresh databases (new file) run V1 → V2 → V3 from scratch. + +### Reset local database + +Delete the SQLite file and restart the application — Flyway recreates the schema and seed data automatically: + +```bash +rm storage/players-sqlite3.db +./mvnw spring-boot:run +``` + +### Tests + +The test environment keeps `spring.flyway.enabled=false` and uses SQLite in-memory with `ddl.sql`/`dml.sql` via Spring SQL init for fast, isolated test execution. + ## Contributing Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details on: diff --git a/pom.xml b/pom.xml index 2b5ba25..a2a00e2 100644 --- a/pom.xml +++ b/pom.xml @@ -169,6 +169,29 @@ org.hibernate.orm hibernate-community-dialects + + + + org.flywaydb + flyway-core + + + + + org.flywaydb + flyway-database-postgresql + + - org.flywaydb - flyway-core + org.springframework.boot + spring-boot-starter-flyway