Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyway and JPA integration

My current Spring 3.0 project is integrating with Flyway.

Thanks to the google site so there are document I can counting on. But unfortunately there is not much talking about integration with JPA.

So the questions is:

  1. How to integrate Flyway with persistence.xml? And how does it work? Each time JPA provider will auto generate schema update so how we run a script before or after then?

  2. I guess the query by flyway so far does not support HQL and such so is there any sample code then I can go through to know how to integrate the migration event? Design an interceptor or a new aspect?What to do on a domain level?

Any hint is appreciated. Thanks in advance.

like image 274
Dreamer Avatar asked Aug 20 '12 22:08

Dreamer


1 Answers

Flyway has no support for JPA and Spring. It basically runs your SQL (not HQL) scripts in order and keeps track of them. And does it well. It remains agnostic to how you use your database and how you produce your upgrade scripts.

However, there's hope. Your persistence provider will most likely support updating existing schema (I know hibernate and eclipselink can), running ALTER and CREATE statements on startup. The migration SQL scripts aren't perfect and it won't always work, but it's a good start. Log these scripts, collect into SQL file, clean-up and use as V_*.sql file supplied to Flyway.

UPDATE: although there is no direct support for spring framework, you can easily integrate it with existing Spring application. This approach is proven to work on production and plays nicely:

<bean id="flyway" class="com.googlecode.flyway.core.Flyway" init-method="migrate">
  <property name="dataSource" ref="..."/>
  ...
</bean>

Bonus: it works great with Java configuration (with Scala) as well:

@Bean(initMethod = "migrate")
def flyway() = {
    val fly = new Flyway()
    fly.setDataSource(dataSource)
    fly
}
like image 131
Tomasz Nurkiewicz Avatar answered Oct 13 '22 00:10

Tomasz Nurkiewicz