database 如何使用 liquibase,一个具体的例子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1148663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 07:24:23  来源:igfitidea点击:

How to work with liquibase, a concrete example

databaseversion-controlchange-managementliquibase

提问by apelliciari

Following the quickstart on liquibasei've created a changeset (very dumb :) )

按照liquibase的快速入门,我创建了一个变更集(非常愚蠢:))

Code:

代码:

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.6"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.6
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.6.xsd">

    <changeSet id="1" author="me">
        <createTable tableName="first_table">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <createTable tableName="new_table">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false"/>
            </column>
        </createTable>
    </changeSet>

</databaseChangeLog>

I've created a clean schema and i've launched the migrate command.

我已经创建了一个干净的架构并且我已经启动了 migrate 命令。

Liquibase created the database, with the support tables databasechangelog and ..lock.

Liquibase 创建了数据库,支持表 databasechangelog 和 ..lock。

Now how i can track the changes?? i've modified the changeset adding a new createTable element but when i try the command "update" liquibase tells me this

现在我如何跟踪更改?我已经修改了变更集,添加了一个新的 createTable 元素,但是当我尝试命令“更新”时,liquibase 告诉我这个

Migration Failed: Validation Failed:
     1 change sets check sum

so i don't think to have understood the way to work with liquibase.

所以我认为我没有理解使用 liquibase 的方式。

Someone may point me to the right direction??

有人可能会指出我正确的方向?

Thanks

谢谢

回答by ChssPly76

You should never modify a <changeSet>that was already executed. Liquibase calculates checksums for all executed changeSets and stores them in the log. It will then recalculate that checksum, compare it to the stored ones and fail the next time you run it if the checksums differ.

您永远不应该修改<changeSet>已经执行的 a。Liquibase 计算所有执行的变更集的校验和并将它们存储在日志中。然后它将重新计算该校验和,将其与存储的校验和进行比较,如果校验和不同,则下次运行它时会失败。

What you need to do instead is to add another<changeSet>and put your new createTable element in it.

您需要做的是添加另一个<changeSet>并将新的 createTable 元素放入其中。

QuickStart is good readin' but it is indeed quick :-) Check out the full manual, particularly its ChangeSetsection.

QuickStart 读起来不错,但确实很快 :-) 查看完整手册,尤其是它的ChangeSet部分。

回答by Javid Jamae

This currently accepted answer is slightly out of date based on changes in Liquibase 2.x. In the 2.x version, Liquibase will still fail if the md5 checksum has changed for a changeset, but you can specify the runOnChangeattribute if you want to be able modify it.

根据 Liquibase 2.x 中的更改,当前接受的答案略有过时。在 2.x 版本中,如果变更集的 md5 校验和已更改,Liquibase 仍然会失败,但如果您希望能够修改它,则可以指定runOnChange属性。

From the documentation:

文档

runOnChange- Executes the change the first time it is seen and each time the change set has been changed

runOnChange- 在第一次看到更改和每次更改集更改时执行更改

回答by rogerdpack

If it's a change to a changeset that basically has already been done, you can manually modify the database so that its md5 for that changeset matches the new one. Good for minor textual changes. Or you can delete that changeset row from your table.

如果它是对基本上已经完成的变更集的更改,您可以手动修改数据库,以便该变更集的 md5 与新的匹配。适用于较小的文本更改。或者您可以从表中删除该变更集行。