使用Liquibase和Spring合并DB中的值 [英] Merging values from DB using Liquibase and Spring

查看:52
本文介绍了使用Liquibase和Spring合并DB中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用liquibase更改数据库的布局,但是我有一个问题:

I'm trying to use liquibase to change the layout of my DB, but and I have a question that is:

例如,假设我的旧数据库的表包含2列(firstName,lastName),而我的新数据库中只有两列(userName).

Lets say for example that my old DB has a table which had 2 columns (firstName, lastName) but my new DB has only one column for those two (userName).

如何使用liquibase和Spring进行此迁移.因为按照以下逻辑,我会丢失原始值.

How could I do this migration using liquibase and Spring. Because with the following logic I would lose the original values.

理想情况下,我希望能够调用我的Java代码进行更改,尽管在这种情况下,在某些情况下可能需要过度设计;)

Ideally I would like to be able to call my java code to make the changes, event though in this case it's over engineering in other cases it may be required ;)

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

    <changeSet author="gcardoso" id="2012082703">

        <dropColumn columnName="firstName" tableName="t_user"/>
        <dropColumn columnName="lastName" tableName="t_user"/>

        ?????? How to migrate the names ??????

        <addColumn tableName="t_user">
            <column name="userName" type="VARCHAR2(255,0)">
                <constraints nullable="false"/>
            </column>
        </addColumn>
    </changeSet>
</databaseChangeLog>

推荐答案

您需要自定义重构.有两种可能性:

You need a custom refactoring. There are two possibilities:

  • 自定义SQL 用于使用sql可以实现的更改
  • 自定义重构类,以进行更复杂的更改.通过这种方法,您可以使用Java来实现重构.
  • Custom SQL for changes that can be achieved with sql
  • Custom Refactoring class for more complex changes. With this approach you can use Java to implement your refactoring.

所以你会

  1. 添加新列
  2. 通过自定义重构更改将数据从旧列迁移到新列
  3. 删除旧列

如何在Spring JdbcTemplate中使用自定义重构类

@Override
public void execute(Database database) throws CustomChangeException {
    JdbcConnection connection = (JdbcConnection) database.getConnection();
    DataSource dataSource = new SingleConnectionDataSource(connection.getUnderlyingConnection(), true);
    JdbcTemplate template = new JdbcTemplate(dataSource, false);
}

这篇关于使用Liquibase和Spring合并DB中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆