使用Liquibase将数据库从一个版本迁移到另一个版本 [英] Database Migration from one version to another using Liquibase
问题描述
我推出了应用程序的第一个版本,并为此安装了一个Postgres服务器.
I rolled out the first version of application and a Postgres server is set up for the same.
我计划推出第二个版本的应用程序,该版本在表中进行了结构更改.
I am planning to roll out my second version of my application which has structural changes in my tables.
例如:我的App表中有一个名为version的列,现在我还有另一个名为releaseVersion的列,我必须应用alter
来添加此列.在这种情况下,如何使用liquibase
来生成/应用migration
脚本?
For example : I had App table with a column called version , now I have another column called releaseVersion and I have to apply alter
to add this column.In such a case, how can I use liquibase
to generate/apply the migration
script?
liquibase
是否可以提供这样的migration
??
Is liquibase
capable of such migration
.?
简而言之,在我的第一个版本中,我使用DDL创建了表格
In short, for my first version I created my table using the DDL
CREATE TABLE App (version varchar); // I manually generated this using liquibase offline mode and my metadata.
现在我的数据库在上面的列中.
Now I have my db with above column.
我需要使用liquibase
生成alter来添加列.像这样
And I need to generate the alter to add column using liquibase
. Something like this
ALTER TABLE App ADD releaseVersion varchar;
是否可以使用Liquibase
,因为它是migration
的行业标准.
Is it possible using Liquibase
as it is the industry standard for migration
.
我使用了liquibase:diff
,但是它只能从两个数据库(target db
和base db
)创建差异changelog
.就我而言,只有一个生产数据库.
I used liquibase:diff
, but it is only capable of creating the difference changelog
from two databases (target db
and base db
). In my case, there is only a production database.
推荐答案
是的,有可能.
创建一个changeSet,如:
Create a changeSet like:
<changeSet author="foo" id="bar">
<preConditions onFail="MARK_RAN">
<and>
<columnExists tableName="App" columnName="version"/>
<not>
<columnExists tableName="App" columnName="releaseVersion"/>
</not>
</and>
</preConditions>
<renameColumn tableName="App" oldColumnName="version" newColumnName="releaseVersion" columnDataType="varchar(100)"/>
</changeSet>
并使用liquibase update
命令应用它.
如果您只需要添加一个新列,那么您的changeSet将如下所示:
If you need to just add a new column, then your changeSet will look like this:
<changeSet id="foo" author="bar">
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="App" columnName="releaseVersion"/>
</not>
</preConditions>
<addColumn tableName="App">
<column name="releaseVersion" type="varchar(100)"/>
</addColumn>
</changeSet>
这篇关于使用Liquibase将数据库从一个版本迁移到另一个版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!