2

我第一次测试 Flyway,版本 2.1.1。我正在使用 Ant 任务,如果该表尚不存在(并且它在我的模式中不存在),则该任务migrate应创建该表,并且出现此错误:schema_version

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>

我尝试init在迁移之前添加,第一次一切正常,但是当我第二次启动构建时,我从initsayng 中得到一个错误,表已经存在。

这是我的 Ant 目标:

<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>

关于如何修复它的任何建议?

4

2 回答 2

1

在等待 2.2 时,我已经成功地测试了(到 oracle DB)以下“自动”方式来初始化 Flyway 元数据表:

<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>

如果您喜欢,请尽情享用(也许使用 ant-contrib 您可以获得更简单、更紧凑的解决方案)

戴维德

于 2013-07-10T10:33:55.740 回答
0

您必须为 Flyway 初始化一次数据库。那是 Flyway 创建用于跟踪迁移的表的时候。正如您所注意到的,任何重新初始化的尝试都会失败,因为该表已经存在。

我建议制作一个单独的 ant 任务来调用Flyway 的 init,您可以手动运行它来初始化 Flyway,类似于:

<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>
于 2013-07-09T16:41:09.203 回答