5
votes

I'm trying to do a Flyway migration on multiple (6 or so) instances of our server. Each one is built and deployed automatically from Git using Bamboo, so I'd really like to be able to use the flyway:migrate Maven goal so each server migrates itself when it is next deployed.

However I'm just trying out Flyway now, so none of the existing DBs have been init'd. I'm wondering is it possible for me to somehow specify in Maven that Flyway should init if it hasn't already, and then migrate every time?

The migrate docs suggest that "Flyway will create the metadata table automatically if it doesn't exist", but in fact when the flyway:migrate goal is executed, I get the error:

Failed to execute goal com.googlecode.flyway:flyway-maven-plugin:2.2:migrate (default) on project mutopia-server: Flyway Error: com.googlecode.flyway.core.api.FlywayException: Found non-empty schema "public" without metadata table! Use init() first to initialize the metadata table. -> [Help 1]

2

2 Answers

12
votes

I didn't figure it out with Maven, since I switched to using the API version with Spring (which seems to be better in all respects).

Initially it had the same problem - the bean's init-method="migrate" would fail on non-empty schemas, requiring init be called first. However by looking at the source of the Flyway bean, I noticed I could just set initOnMigrate to true, and it would do it for me. I'm not sure why this isn't in the documentation; it's not trivially obvious to someone who doesn't play around with beans very much.

Anyway, if you also have a non-empty schema about which Flyway is complaining, the bean in applicationContext.xml should look like this:

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

If you are using spring-boot, put this in your prop/yml file

flyway.initOnMigrate=true

or
flyway:
  initOnMigrate: true

Please note: Be careful when enabling this as it removes the safety net that ensures - Flyway does not migrate the wrong database in case of a configuration mistake!