我正在尝试使用 liquibase 来跟踪使用 dropwizard-migrations 的 postgresql 数据库的更改。我希望能够在现有的生产数据库上运行迁移,而不是从头开始重建。现在我正在 frog-dev 中进行测试。我用青蛙创建了一个变更集。
<changeSet id="3" author="me">
<preConditions onFail="CONTINUE">
<not>
<sequenceExists sequenceName="emails_id_seq"/>
</not>
</preConditions>
<createSequence sequenceName="emails_id_seq" startValue="1" incrementBy="1" />
</changeSet>
我的目标是如果序列已经存在,则跳过应用青蛙。看似简单,但行不通。
ERROR [2013-09-13 22:19:22,564] liquibase: Change Set migrations.xml::3::me failed. Error: Error executing SQL CREATE SEQUENCE emails_id_seq START WITH 1 INCREMENT BY 1: ERROR: relation "emails_id_seq" already exists
! liquibase.exception.DatabaseException: Error executing SQL CREATE SEQUENCE emails_id_seq START WITH 1 INCREMENT BY 1: ERROR: relation "emails_id_seq" already exists
我也尝试过使用 MARK_RAN 而不是 CONTINUE。青蛙没有运气。
将变更集应用到现有数据库(无需执行)的一种更简单的方法是使用 changelogSync 命令。
以下命令演示了如何提取更改日志,然后将其与当前数据库同步:
liquibase --changeLogFile=mydb.xml generateChangeLog
liquibase --changeLogFile=mydb.xml changelogSync
sync 命令的作用是创建变更日志表中的所有条目,以便现在可以正常使用 liquibase 文件来更新数据库:
liquibase --changeLogFile=mydb.xml update
我使用 sqlCheck 指令解决了这个问题:
<changeSet id="sys-0" context="structural">
<preConditions onFail="MARK_RAN"><sqlCheck expectedResult="0">SELECT count(c.relname) FROM pg_class c WHERE c.relkind = 'S' and c.relname = 'hibernate_sequence'</sqlCheck></preConditions>
<!-- <preConditions><not><sequenceExists schemaName="public" sequenceName="hibernate_sequence"/></not></preConditions> -->
<createSequence schemaName="public" sequenceName="hibernate_sequence"/>
</changeSet>
(在liquibase 2.0.1版本上测试)
我对视图做了同样的事情,对我来说它有效:
也许能给你一些想法:
<changeSet author="e-ballo" id="DropViewsAndcreateSynonyms" context="dev,int,uat,prod">
<preConditions onFail="CONTINUE" >
<viewExists viewName="PMV_PACKAGE_ITEMS" schemaName="ZON"/>
<viewExists viewName="PMV_SUBSPLAN_INSTALLTYPES" schemaName="ZON"/>
</preConditions>
<dropView schemaName="ZON" viewName="PMV_PACKAGE_ITEMS" />
<dropView schemaName="ZON" viewName="PMV_SUBSPLAN_INSTALLTYPES" />
<sqlFile path="environment-synonyms.sql" relativeToChangelogFile="true" splitStatements="true" stripComments="true"/>
</changeSet>
希望对你有帮助
我通过运行 dropwizard-migrations“快进”命令解决了这个问题,如下所示:
java -jar hello-world.jar db fast-forward helloworld.yml
这会将下一个变更集标记为已应用,而不实际应用它。对于每个要快进的变更集,您可能必须执行一次此操作。如果你想快进所有内容,还有一个 --all 标志。
更多详细信息可以在这里找到:http://dropwizard.codahale.com/manual/migrations/
还记得检查它们是否没有缓存,对我来说,我正在使用 OpenMRS 模块,但由于 openMRS 中的缓存,我的先决条件从未生效,这导致我认为我的代码失败了,而不是它们从未被执行