如何使用Spring操作生产数据库数据格式?

How to manipulate the production database data format using Spring?

情况是这样的:

这个过程的两个已知步骤是:

问题:

这是一个更一般的 Spring 问题 - 当您需要更改生产中现有的结构并操纵旧数据以适应新格式时,您如何处理这种情况?

例如在PHP中,我在应用程序环境中使用与应用程序集成的终端脚本和运行(在Laravel中使用"artisan"命令);我可以轻松地创建正确的操作顺序:获取旧数据并记住它、更改数据库结构、操作旧数据以及将数据插入新结构——这一切都在一个脚本和一个事务中。我不知道如何在 Spring.

中执行此操作

我找到了答案 - Flyway Java-based 迁移:https://flywaydb.org/documentation/migrations#java-based-migrations

Java-based migrations are a great fit for all changes that can not easily be expressed using SQL. These would typically be things like BLOB & CLOB changes, Advanced bulk data changes (Recalculations, advanced format changes, …)

看起来像这样:

package db.migration;

import org.flywaydb.core.api.migration.spring.SpringJdbcMigration;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Example of a Spring Jdbc migration.
 */
public class V1_2__Another_user implements SpringJdbcMigration {
    public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
        jdbcTemplate.execute("INSERT INTO test_user (name) VALUES ('Obelix')");
    }
}

数据库操作可以在此处与 Java 代码和数据操作交错。

我怀疑 Liquibase 具有类似的功能。