gpt4 book ai didi

java - 使用 Liquibase 和 Spring 合并来自数据库的值

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:20 24 4
gpt4 key购买 nike

我正在尝试使用 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>

最佳答案

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

所以你会

  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);
}

关于java - 使用 Liquibase 和 Spring 合并来自数据库的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12144520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com