I'm starting to use flyway in my spring managed java project (I'm using also hibernate).
I followed the documentation of flyway.I configured also maven plugin and all works fine.
Trying to use maven plugin to do clean,init and migrate, all works fine.
Instead if I try to run my application (in which I've my flyway bean):
<bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
<property name="dataSource" ref="dataSource" />
</bean>
I see that:
- the schema of the db is created
- the schema_version table is created
- my script to create the entire db works
Unfortunally my app can't start due to an error during the migration that I don't understand:
31/10/2014 11:03:34 INFO DbSupportFactory:43 - Database: jdbc:h2:~/db/db (H2 1.4)
31/10/2014 11:03:34 INFO DbValidate:43 - Validated 1 migration (execution time 00:00.015s)
31/10/2014 11:03:34 INFO MetaDataTableImpl:43 - Creating Metadata table: "PUBLIC"."schema_version"
31/10/2014 11:03:34 INFO DbMigrate:43 - Current version of schema "PUBLIC": << Empty Schema >>
31/10/2014 11:03:34 INFO DbMigrate:43 - Migrating schema "PUBLIC" to version 3.0.0
31/10/2014 11:03:34 ERROR ContextLoader:331 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in ServletContext resource [/WEB-INF/spring/app-jpa-config.xml]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Unable to insert row for version '3.0.0' in metadata table "PUBLIC"."schema_version"
...
...
...
Caused by: org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY_KEY_6 ON PUBLIC.""schema_version""(""version"") VALUES ( /* 3 */ CAST('3.0.0' AS VARCHAR_IGNORECASE) )"; SQL statement:
INSERT INTO "PUBLIC"."schema_version" ("version_rank","installed_rank","version","description","type","script","checksum","installed_by","execution_time","success") VALUES (?, ?, ?, ?, ?, ?, ?, USER(), ?, ?) [23505-182]
So seems a duplicate exception but it is very strange. After this I seen the db and always seems correct:
- schema_versione contains two row: flyway init and my script that has version 3.0.0
- I see that the entire db is created
So my question is: what is wrong? Is flyway trying to make the migration twice?
Thanks