Hacker News new | ask | show | jobs
by nek4life 4108 days ago
I've been learning Flyway and so far it seems to work well for applying handwritten sql scripts. I was hoping it could generate the migration files for you based on your entity classes, but that doesn't seem to be the case. Is there any tools for Java that handle both applying migrations and generating migration files based on your JPA Entity classes?

I've been using Alembic with SQLAlchemy with Python and I really like the ability to auto generate migrations and then be able to hand tune them if need be. I'm looking for the same kind of tooling for Java and haven't really been able to find it yet.

2 comments

While that approach may seem convenient at first, I recommend the exact opposite for two reasons:

1. Your data will outlive your application

2. I strongly prefer contract-first over code-first interfaces between systems for stability and reliability issues

Following the latter, as the interface of your database towards your application is the schema, you should absolutely have that being the driving force and not the entity classes in your language of choice

I watched this video https://www.youtube.com/watch?v=vPwWQvvBWEg where (I think you?) explained these points in greater detail and I tend to agree with you that sql first is a very sensible approach.

I'm probably missing something, but is there a configuration with JPA that can explain the tables that hibernate's expecting based on the Entity classes? I guess that's where my biggest pain point going from sql first to writing my classes is getting the tables to validate and map properly. It's been a little time consuming being new to the ecosystem trying to get my sql (postgres) to map to JPA Entity classes going in reverse. Going from Entity classes to SQL you don't have this issue because the tool is writing the SQL for you (not saying this is the best approach) so without this a tool for debugging Entities would be handy to get up to speed faster when using Flyway or another SQL first approach.

You might want to take a look at the Grails Migration Plugin (which uses Hibernate and Liquibase "under the covers")

It allows you to write your domain classes in a Groovy DSL, called GORM http://grails.github.io/grails-doc/latest/guide/GORM.html and generate SQL and SQL migrations as you change your classes.

Here's the Getting Started page from the documentation http://grails-plugins.github.io/grails-database-migration/do...

There is a standalone GORM that allows you to use the Groovy DSL without Grails, I'm not sure how the Migration plugin could be made to work with that.

Thanks! I'll take a look at that.