Summary

Bingdoal introduces Flyway as a superior database migration solution for Spring Boot applications, contrasting it with JPA Hibernate’s built-in DDL auto-management. Flyway uses versioned SQL migration scripts that are tracked in a schema_version table, enabling precise version control of database schema changes alongside application code. This solves the multi-environment deployment problem and provides a clear audit trail.

本文介紹 Flyway 作為 Spring Boot 應用的資料庫 migration 解決方案,對比 JPA Hibernate 內建的 DDL 自動管理。Flyway 使用版本化 SQL 遷移腳本並以 schema_version 表追蹤,讓資料庫 Schema 變更與應用程式碼同步版本控制,解決多環境部署問題並提供清晰的變更記錄。

Key Points

  • JPA’s ddl-auto: update can’t handle column deletion/rename, leaves old columns alongside new ones
  • Flyway uses versioned SQL scripts (V1__init.sql, V2__add_column.sql) tracked in a schema_version table
  • Migrations are run automatically on application startup — guarantees DB is always in sync with code
  • Scripts are immutable once applied; checksums validate integrity across environments
  • Flyway supports both Java-based and SQL-based migrations for complex data transformations

Insights

The core insight is that database schema is code and should be versioned accordingly. JPA’s DDL-auto trades reliability for convenience — it works for development but fails in production scenarios requiring precise control. Flyway’s append-only migration model mirrors Git’s commit history: you never modify the past, you always add new changes. This makes rollback explicit (a new migration) rather than implicit (reverting DDL-auto).

Connections

Raw Excerpt

對資料庫的修改進行版本控制,利用 Git 這類版控工具將資料庫的修改與程式的版本同步