3

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

例如,我的旧数据库有一个表,它有 2 列(名字、姓氏),但我的新数据库只有一列用于这两个(用户名)。

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

理想情况下,我希望能够调用我的 java 代码来进行更改,尽管在这种情况下它在其他情况下可能需要过度工程;)

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

1 回答 1

3

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

所以你会

  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);
}
于 2012-08-27T15:12:22.413 回答