用flyway迁移springboot项目

一.sql和java都需要的配置:

 1. maven 的pom.xml配置:(我自己用的是h2的数据库,换成自己使用的数据库即可)
  <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>5.2.4</version>
            <configuration>
                <url>jdbc:h2:./test.db;AUTO_SERVER=TRUE</url>
                <user>sa</user>
                <password></password>
                <driver>org.h2.Driver</driver>
            </configuration>
  </plugin>

 <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>5.2.4</version>
 </dependency>
  1. spring boot 配置文件的配置:(一些其他的配置可以参考官网)
   !!关于flyway 的配置
   #  是否开启基础baseline 进行迁移
   spring.flyway.baseline-on-migrate=true
   # 是否开始flyway
   spring.flyway.enabled=true
   # 是否开启检查flyway配置文件的名字规范
   spring.flyway.validate-migration-naming=true

   !!验证springboot的配置:需要修改成update如果你的是validate
   spring.jpa.hibernate.ddl-auto=update
  1. 修改完成之后,将原来版本运行的数据库文件复制到现在的目录下,直接启动spring boot项目即可

二. 基于sql进行版本的迁移:

  1. 在resource目录下创建db.migration目录,(默认使用这个目录,也可以配置)
      此目录下放入你需要执行的sql文件:(必须这样写)
      例如:(V1后面是两个下划线,之后init_column是一些描述信息可以随便写)
      V1__init_column.sql 
  2. 配置完成直接把启动spring boot完成

三. 基于java进行版本迁移(两个例子)

   1. 在java目录下创建db.migration  放入java代码:简单的示例:(官网上也有可以借鉴)
       public void migrate(Context context) throws Exception {
     
    JdbcTemplate jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(context.getConnection(),
            true));
    jdbcTemplate.execute("alter table T_TEST_PIPELINE drop column type");
    jdbcTemplate.execute("alter table T_PIPELINE alter state rename to edit_state");
    jdbcTemplate.update("DROP table T_JOB");
    jdbcTemplate.execute("create table if not exists t_job\n" +
            "(\n" +
            "    id                 int primary key AUTO_INCREMENT,\n" +
            "    job_id             varchar(32) NOT NULL,\n" +
            "    pipeline_id        int         NOT NULL,\n" +
            "    state              varchar(16) NOT NULL,\n" +
            "    fail_plugin_id     varchar(32),\n" +
            "    fail_property_name varchar(32),\n" +
            "    start_time         timestamp   NOT NULL COMMENT '启动时间',\n" +
            "    finish_time        timestamp COMMENT '结束时间',\n" +
            "    create_time        timestamp   NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
            "    update_time        timestamp   NOT NULL DEFAULT now() ON UPDATE now() COMMENT '最近修改时间',\n" +
            "    publish_id         int         NOT NULL,\n" +
            "    publish_version    int,\n" +
            "    publish_committer varchar(32) NOT NULL ,\n" +
            "    publish_message   varchar(100) NOT NULL ,\n" +
            "    log                text\n" +
            ")");
}
 public class V3__Anonymize extends BaseJavaMigration {
     
    public void migrate(Context context) throws Exception {
     
        try (Statement select = context.getConnection().createStatement()) {
     
            try (ResultSet rows = select.executeQuery("SELECT id FROM person ORDER BY id")) {
     
                while (rows.next()) {
     
                    int id = rows.getInt(1);
                    String anonymizedName = "Anonymous" + id;
                    try (Statement update = context.getConnection().createStatement()) {
     
                        update.execute("UPDATE person SET name='" + anonymizedName + "' WHERE id=" + id);
                    }
                }
            }
        }
    }
}
  1. 相同的做法,等配置完成之后将原先项目的数据脚本放置到现在目录下,直接启动spring boot即可

四. sql和java 之间的区别:

从上述代码可以看出,java可以提供更加方便的操作。更加的灵活(查看官网)

你可能感兴趣的:(spring,boot)