Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic generation of migration SQL for Flyway

Is it possible for new Flyway migrations to be generated by JPA/Hibernate's automatic schema generation when a new model / field etc. are added via Java code.

It would be useful to capture the auto-generated SQL and save it directly to a new Flyway migration, for review / editing / committing to a project repository.

Thank you in advance for any assistance or enlightenment you can offer.

like image 843
ocodo Avatar asked Mar 14 '16 09:03

ocodo


People also ask

Does Flyway automatically create schema?

If not specified in schemas, Flyway will automatically attempt to create and clean this schema first. This schema will also be the default for the database connection (provided the database supports this concept).

Does Flyway migrate data?

Flyway automatically discovers migrations on the filesystem and on the Java classpath. To keep track of which migrations have already been applied when and by whom, Flyway adds a schema history table to your schema.

What is Flyway DB migration?

Flyway is an open-source database migration tool. It strongly favors simplicity and convention over configuration. It is based around just 7 basic commands: Migrate, Clean, Info, Validate, Undo, Baseline and Repair.


2 Answers

If your IDE of choice is IntelliJ IDEA, I'd recommend using the JPA Buddy plugin to do this. It can generate Flyway migrations by comparing your Java model to the target DB.

You can use it to keep your evolving model and your SQL scripts in sync. Also, it can create the init script if your DB is empty.

Once you have it installed and have Flyway as your Maven/Gradle dependency, you can generate a migration like this: Generating Flyway migration with JPA Buddy

like image 135
Andrey Oganesyan Avatar answered Oct 17 '22 06:10

Andrey Oganesyan


Flyway doesn't have built-in support for diff, I use liquidbase within a maven spring boot project and changelogs can be created from JPA/hibernate changes by using:

mvn liquibase:diff

All of the options for liquibase diff are located here: http://www.liquibase.org/documentation/maven/maven_diff.html

like image 22
danzdoran Avatar answered Oct 17 '22 05:10

danzdoran