Skip to content

Latest commit

 

History

History
57 lines (35 loc) · 2.35 KB

D - Database Migration.md

File metadata and controls

57 lines (35 loc) · 2.35 KB

Database Migrations

Flyway is used by this project for database migrations. Flyway is able to generate migration scripts (via JPA buddy) and Spring will automatically apply the changes to the database (if Flyway is enabled).

Applying Migrations

This project is configured to apply any pending migrations automatically when the application is started. If you want to manually apply migrations (https://flywaydb.org/documentation/getstarted/firststeps/commandline), auto-apply can be disabled in the Anchor Platform Configuration:

spring.flyway.enabled: false

Adding a New Migration

We usually generate the migrations using IntelliJ IDEA's JPA Buddy plugin. If you don't have it installed yet, please go ahead and download it at https://plugins.jetbrains.com/plugin/15075-jpa-buddy.

There's also an option to generate the migration from command-line or even manually, but we won't be covering that in this tutorial.

A. Adding a Database Connection

Flyway migration generator tool will need to compare your current database state with the project database entities, in order to be able to generate a diff that can bring your database to the desired state mapped in the code.

To connect a database, go to JPA Structure -> DB connections -> right click new -> DB connection

img_4.png

B. Generating & Registering a new Changelog File

  1. When data models or table names are changed, a new changelog can be generated. New(+) -> Diff Versioned Migration:

    img.png

  2. Select the DB connection to create a "diff" against

    img_3.png

  3. Review the changes and generate the migration file.

    Note: The migration file format =<Prefix><Version>__<Description>.sql

    Note: Directory should be set to platform/src/main/resources/db/migration/ so all the changelog files are in one place.

    img_1.png