Question

I am testing Flyway for the first time, version 2.1.1. I am using the Ant task migrate which should create the schema_version table if it does not already exist (and it does not exists in my schema), and I get this error:

C:\Users\dmusiani\Desktop\flyaway-test>ant
Buildfile: build.xml

migrate-db:
[flyway:migrate] com.googlecode.flyway.core.api.FlywayException: Found non-empty
 schema "SYSTEM" without metadata table! Use init() first to initialize the meta
data table.

BUILD FAILED
C:\Users\dmusiani\Desktop\flyaway-test\build.xml:37: Flyway Error: com.googlecod
e.flyway.core.api.FlywayException: Found non-empty schema "SYSTEM" without metad
ata table! Use init() first to initialize the metadata table.

Total time: 0 seconds
C:\Users\dmusiani\Desktop\flyaway-test>

I tried adding the init before the migration, and the first time it all works, but as I launch the build a second time, I get an error from the init sayng the table already exists.

Here is my Ant target:

<target name="migrate-db">

    <path id="flyway.lib.path">
        <fileset dir="./lib">
            <include name="**/flyway*.jar"/>
        </fileset>
    </path>

    <path id="flyway.classpath">
        <fileset dir="./lib" includes="ojdbc6-11.2.0.3.jar"/>
    </path>

    <taskdef uri="antlib:com.googlecode.flyway.ant"
             resource="com/googlecode/flyway/ant/antlib.xml"
             classpathref="flyway.lib.path"/>

    <flyway:init  driver="oracle.jdbc.OracleDriver"
                     url="jdbc:oracle:thin:@localhost:1521:WBMD"
                     user="system"
                     password="manager" 
                     initVersion="0"
                     initDescription="Version table initialization (table &quot;USERNAME&quot;.&quot;schema_version&quot; )"/>

    <flyway:migrate driver="oracle.jdbc.driver.OracleDriver"
                     url="jdbc:oracle:thin:@localhost:1521:WBMD"
                     user="system"
                     password="manager"
                     sqlMigrationPrefix="patch" >
        <locations>
            <location path="integrations-runtime/HELIOS/1-9-0/nlip-0-5-0/migration/system"/>
        </locations>
    </flyway:migrate>

</target>

Any suggestion on how to fix it?

Was it helpful?

Solution 2

You have to init the db once for Flyway. That is when Flyway creates the table used to track the migrations. As you have noticed, any attempt to init again fails because the table already exists.

I suggest making a separate ant task that calls Flyway's init that you can run manually to initialize Flyway, something along the lines of:

<target name="init-flyway">

    <path id="flyway.lib.path">
        <fileset dir="./lib">
            <include name="**/flyway*.jar"/>
        </fileset>
    </path>

    <path id="flyway.classpath">
        <fileset dir="./lib" includes="ojdbc6-11.2.0.3.jar"/>
    </path>

    <taskdef uri="antlib:com.googlecode.flyway.ant"
             resource="com/googlecode/flyway/ant/antlib.xml"
             classpathref="flyway.lib.path"/>

    <flyway:init  driver="oracle.jdbc.OracleDriver"
                     url="jdbc:oracle:thin:@localhost:1521:WBMD"
                     user="system"
                     password="manager" 
                     initVersion="0"
                     initDescription="Version table initialization (table &quot;USERNAME&quot;.&quot;schema_version&quot; )"/>

</target>

OTHER TIPS

While waiting for 2.2, I have successfully tested (to an oracle DB) the following "automatic" way to initialize Flyway metadata table:

<target name="flyway.init.check">
    <!-- Select on the DB to see if the metadata table is already in place-->
    <sql driver="oracle.jdbc.OracleDriver"
         url="jdbc:oracle:thin:@localhost:1521:WBMD"
         userid="user"
         password="pwd" 
         print="yes" 
         output="temp.properties"
         expandProperties="true" 
         showheaders="false" 
         showtrailers="false" 
         classpathref="flyway.classpath">
        <![CDATA[
        select 'flyway.metadata.initialized=true' from user_tables where table_name = 'schema_version';
        ]]>
    </sql>
    <!-- load as properies the output from the select -->
    <property file="temp.properties" />
    <delete file="temp.properties" quiet="true"/>
</target>

<target name="flyway.init" depends="flyway.init.check" unless="flyway.metadata.initialized">
    <flyway:init  driver="oracle.jdbc.OracleDriver"
                 url="jdbc:oracle:thin:@localhost:1521:WBMD"
                 user="user"
                 password="pwd" 
                 initVersion="0"
                 initDescription="Version table initialization"/>
</target>

<target name="migrate-db" depends="flyway.init">
   ......
</target>

Enjoy if you like it (maybe using ant-contrib you could get a simpler and more compact solution)

Davide

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top