移除flyway,手动进行数据库的迁移

news2024/11/18 20:20:01

 国产数据库如达梦、金仓数据库,开源数据迁移工具貌似支持的很少,手写工具类进行数据库脚本的迁移,主要有2个类如下:

 

/**
 * 模拟flyway进行sql迁移
 */
@Component
@Slf4j
public class SqlInitialize implements InitializingBean, Ordered {
  /**
   * 初始化SQL文件
   *
   * @param sqlFileName sql文件名,规范:xxx.sql
   */

  @Resource
  private JdbcTemplate template;


  @Value("${sql.log.path}")
  private String sqlLogPath;


  private static Map<String, String> map = null;

  private String validateSqlFileName = "V3.3.0.0__inti_baseline.sql";

  @Override
  public void afterPropertiesSet() throws Exception {
    log.info("<<=============bengin to init sql==============>>");
    DataSource dataSource = template.getDataSource();
    assert dataSource != null;
    try (Connection connection = dataSource.getConnection()) {
      JpaScriptRunner runner = new JpaScriptRunner(connection);
      File file = new File(sqlLogPath);
      //判断父级目录是否存在
      String parentPath = file.getParent();
      File folder = new File(parentPath);

      if (!folder.exists()) {
        if (folder.mkdirs()) {
          log.info("文件夹已创建:" + folder.getAbsolutePath());
        } else {
          log.error("文件夹创建失败:" + folder.getAbsolutePath());
        }
      } else {
        log.debug("文件夹已存在:" + folder.getAbsolutePath());
      }
      if (!file.exists()) {
        log.debug(sqlLogPath);
        file.createNewFile();
        log.debug("文件已创建:" + file.getAbsolutePath());
      }
      runner.setErrorLogWriter(new PrintWriter(new BufferedOutputStream(new FileOutputStream(sqlLogPath))));
      runner.setLogWriter(null);
      ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
      org.springframework.core.io.Resource[] resources = resolver.getResources(getDatasourceName(runner) + File.separator + "*.sql");
      org.springframework.core.io.Resource resourceVa = resolver.getResource(getDatasourceName(runner) + File.separator + validateSqlFileName);
      initValideInfo(runner, resourceVa);
      if (resources.length == 0) {
        log.error("[{}] is not a directory,please check your config of sql file location", getDatasourceName(runner));
      } else {
        sort(resources);
        if (resources == null || resources.length == 0) {
          log.error("no sql file found");
        } else {
          for (org.springframework.core.io.Resource item : resources) {
            String sqlFileName = item.getFilename();
            if (!StringUtils.equals(sqlFileName, validateSqlFileName)) {
              String md5 = SecureUtil.md5(sqlFileName);
              if (map != null && map.containsKey(sqlFileName)) {
                String hash = map.get(sqlFileName);
                if (!StringUtils.equals(hash, md5)) {
                  throw new Exception("sql validate failed");
                } else {
                  continue;
                }
              }
              runner.runScript(IoUtil.getReader(item.getInputStream(), Charset.forName("UTF-8")));
              template.update("insert into \"sql_schema_validate\"(\"name\", \"hash\") values(?, ?);", sqlFileName, md5);
              log.info("execute {} sucess", sqlFileName);
            }
          }
        }
      }
      log.info("<<=============init sql end==============>>");
    } catch (Exception e) {
      log.error(e.getMessage());
      throw e;
    } finally {
      if (map != null) {
        map = null;
      }
    }
  }

  @Override
  public int getOrder() {
    return 0;
  }

  private void initValideInfo(JpaScriptRunner runner, org.springframework.core.io.Resource item) {
    try {
      runner.runScript(IoUtil.getReader(item.getInputStream(), Charset.forName("UTF-8")));
      String sql = "select aaa.\"name\", aaa.\"hash\" from \"sql_schema_validate\" aaa";
      template.query(sql, new RowMapper<Map>() {
        @Override
        public Map mapRow(ResultSet resultSet, int i) throws SQLException {
          String name = resultSet.getString("name");
          String hash = resultSet.getString("hash");
          if (map == null) {
            map = new HashMap<String, String>();
          }
          map.put(name, hash);
          return map;
        }
      });
    } catch (Exception e) {
      log.info(e.getMessage());
    }
  }

  private void sort(org.springframework.core.io.Resource[] files) {
    for (int i = 0; i < files.length - 1; ++i) {
      for (int j = i + 1; j < files.length; ++j) {
        String o1 = files[i].getFilename();
        String o2 = files[j].getFilename();
        int i1 = StringUtils.indexOf(o1, "__");
        int i2 = StringUtils.indexOf(o2, "__");
        String digtal1 = StringUtils.substring(o1, 7, i1);
        String digtal2 = StringUtils.substring(o2, 7, i2);
        if (Integer.valueOf(digtal1) > Integer.valueOf(digtal2)) {
          org.springframework.core.io.Resource tmp = files[i];
          files[i] = files[j];
          files[j] = tmp;
        }
      }
    }
  }

  public static String getDatasourceName(JpaScriptRunner runner) {
    String datasourceName = "";
    String dbType = "postgreSQL";
    if (!Strings.isNullOrEmpty(System.getenv("DB_TYPE"))) {
      dbType = System.getenv("DB_TYPE");
    }
    switch (dbType) {
      //多种类数据库适配
      case "postgreSQL":
        runner.setAutoCommit(true);
        runner.setSendFullScript(true);
        datasourceName = "postgreSQL";
        break;
      case "dm":
        runner.setAutoCommit(false);
        runner.setSendFullScript(false);
        datasourceName = "dm";
        break;
      default:
        throw new RuntimeException("不支持的数据库类型" + dbType);
    }
    return datasourceName;
  }
}

jpaRunner:

@Slf4j
public class JpaScriptRunner {

  private static final String LINE_SEPARATOR = System.lineSeparator();

  private static final String DEFAULT_DELIMITER = ";";

  private static final Pattern DELIMITER_PATTERN = Pattern.compile("^\\s*((--)|(//))?\\s*(//)?\\s*@DELIMITER\\s+([^\\s]+)", Pattern.CASE_INSENSITIVE);

  private final Connection connection;

  private boolean stopOnError;
  private boolean throwWarning;
  private boolean autoCommit;
  private boolean sendFullScript;
  private boolean removeCRs;
  private boolean escapeProcessing = true;

  private PrintWriter logWriter = new PrintWriter(System.out);
  private PrintWriter errorLogWriter = new PrintWriter(System.err);

  private String delimiter = DEFAULT_DELIMITER;
  private boolean fullLineDelimiter;

  public JpaScriptRunner(Connection connection) {
    this.connection = connection;
  }

  public void setStopOnError(boolean stopOnError) {
    this.stopOnError = stopOnError;
  }

  public void setThrowWarning(boolean throwWarning) {
    this.throwWarning = throwWarning;
  }

  public void setAutoCommit(boolean autoCommit) {
    this.autoCommit = autoCommit;
  }

  public void setSendFullScript(boolean sendFullScript) {
    this.sendFullScript = sendFullScript;
  }

  public void setRemoveCRs(boolean removeCRs) {
    this.removeCRs = removeCRs;
  }

  /**
   * Sets the escape processing.
   *
   * @param escapeProcessing
   *          the new escape processing
   * @since 3.1.1
   */
  public void setEscapeProcessing(boolean escapeProcessing) {
    this.escapeProcessing = escapeProcessing;
  }

  public void setLogWriter(PrintWriter logWriter) {
    this.logWriter = logWriter;
  }

  public void setErrorLogWriter(PrintWriter errorLogWriter) {
    this.errorLogWriter = errorLogWriter;
  }

  public void setDelimiter(String delimiter) {
    this.delimiter = delimiter;
  }

  public void setFullLineDelimiter(boolean fullLineDelimiter) {
    this.fullLineDelimiter = fullLineDelimiter;
  }

  public void runScript(Reader reader) throws Exception {
    setAutoCommit();

    try {
      if (sendFullScript) {
        executeFullScript(reader);
      } else {
        executeLineByLine(reader);
      }
    } finally {
      rollbackConnection();
    }
  }

  private void executeFullScript(Reader reader) throws Exception {
    StringBuilder script = new StringBuilder();
    try {
      BufferedReader lineReader = new BufferedReader(reader);
      String line;
      while ((line = lineReader.readLine()) != null) {
        script.append(line);
        script.append(LINE_SEPARATOR);
      }
      String command = script.toString();
      println(command);
      executeStatement(command);
      commitConnection();
    } catch (Exception e) {
      String message = "Error executing: " + script + ".  Cause: " + e;
      printlnError(message);
      throw new Exception(message, e);
    }
  }

  private void executeLineByLine(Reader reader) throws Exception {
    StringBuilder command = new StringBuilder();
    try {
      BufferedReader lineReader = new BufferedReader(reader);
      String line;
      while ((line = lineReader.readLine()) != null) {
        handleLine(command, line);
      }
      commitConnection();
    } catch (Exception e) {
      String message = "Error executing: " + command + ".  Cause: " + e;
      printlnError(message);
      throw new Exception(message, e);
    }
  }

  /**
   * @deprecated Since 3.5.4, this method is deprecated. Please close the {@link Connection} outside of this class.
   */
  @Deprecated
  public void closeConnection() {
    try {
      connection.close();
    } catch (Exception e) {
      // ignore
    }
  }

  private void setAutoCommit() throws Exception {
    try {
      if (autoCommit != connection.getAutoCommit()) {
        connection.setAutoCommit(autoCommit);
      }
    } catch (Throwable t) {
      throw new Exception("Could not set AutoCommit to " + autoCommit + ". Cause: " + t, t);
    }
  }

  private void commitConnection() throws Exception {
    try {
      if (!connection.getAutoCommit()) {
        connection.commit();
      }
    } catch (Throwable t) {
      throw new Exception("Could not commit transaction. Cause: " + t, t);
    }
  }

  private void rollbackConnection() {
    try {
      if (!connection.getAutoCommit()) {
        connection.rollback();
      }
    } catch (Throwable t) {
      // ignore
    }
  }

  private void handleLine(StringBuilder command, String line) throws SQLException {
    String trimmedLine = line.trim();
    if (lineIsComment(trimmedLine)) {
      Matcher matcher = DELIMITER_PATTERN.matcher(trimmedLine);
      if (matcher.find()) {
        delimiter = matcher.group(5);
      }
      println(trimmedLine);
    } else if (commandReadyToExecute(trimmedLine)) {
      command.append(line, 0, line.lastIndexOf(delimiter));
      command.append(LINE_SEPARATOR);
      println(command);
      executeStatement(command.toString());
      command.setLength(0);
    } else if (trimmedLine.length() > 0) {
      command.append(line);
      command.append(LINE_SEPARATOR);
    }
  }

  private boolean lineIsComment(String trimmedLine) {
    return trimmedLine.startsWith("//") || trimmedLine.startsWith("--");
  }

  private boolean commandReadyToExecute(String trimmedLine) {
    // issue #561 remove anything after the delimiter
    return !fullLineDelimiter && trimmedLine.contains(delimiter) || fullLineDelimiter && trimmedLine.equals(delimiter);
  }

  private void executeStatement(String command) throws SQLException {
    try (Statement statement = connection.createStatement()) {
      statement.setEscapeProcessing(escapeProcessing);
      String sql = command;
      if (removeCRs) {
        sql = sql.replace("\r\n", "\n");
      }
      try {
        boolean hasResults = statement.execute(sql);
        while (!(!hasResults && statement.getUpdateCount() == -1)) {
          checkWarnings(statement);
          printResults(statement, hasResults);
          hasResults = statement.getMoreResults();
        }
      } catch (SQLWarning e) {
        throw e;
      } catch (SQLException e) {
        if (stopOnError) {
          throw e;
        } else {
          String message = "Error executing: " + command + ".  Cause: " + e;
          printlnError(message);
        }
      }
    }
  }

  private void checkWarnings(Statement statement) throws SQLException {
    if (!throwWarning) {
      return;
    }
    // In Oracle, CREATE PROCEDURE, FUNCTION, etc. returns warning
    // instead of throwing exception if there is compilation error.
    SQLWarning warning = statement.getWarnings();
    if (warning != null) {
      throw warning;
    }
  }

  private void printResults(Statement statement, boolean hasResults) {
    if (!hasResults) {
      return;
    }
    try (ResultSet rs = statement.getResultSet()) {
      ResultSetMetaData md = rs.getMetaData();
      int cols = md.getColumnCount();
      for (int i = 0; i < cols; i++) {
        String name = md.getColumnLabel(i + 1);
        print(name + "\t");
      }
      println("");
      while (rs.next()) {
        for (int i = 0; i < cols; i++) {
          String value = rs.getString(i + 1);
          print(value + "\t");
        }
        println("");
      }
    } catch (SQLException e) {
      printlnError("Error printing results: " + e.getMessage());
    }
  }

  private void print(Object o) {
    if (logWriter != null) {
      logWriter.print(o);
      logWriter.flush();
    }
  }

  private void println(Object o) {
    if (logWriter != null) {
      logWriter.println(o);
      logWriter.flush();
    }
  }

  private void printlnError(Object o) {
    if (errorLogWriter != null) {
      errorLogWriter.println(o);
      errorLogWriter.flush();
    }
  }

}

基线表:

CREATE TABLE "sql_schema_validate" (
   "name" varchar(256),
   "hash" varchar(50) NOT NULL,
   PRIMARY KEY ("name")
);

数据库文件列表使用fly的命名柜子进行如下:

   方便进行数据库的切换,其中sqlLogPath为记录出错的sql的记录文件,这样基本上满足sql迁移的功能,后续有问题再调整

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/691963.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Java【String字符串不可变】

字符串不可变&练习 字符串不可变1. 字符串设置为不可变的原因2. 如何修改字符串内容3 StringBuilder类的具体使用4. 面试题 字符串不可变 1. 字符串设置为不可变的原因 方便实现字符串常量池&#xff0c;若String对象可变&#xff0c;常量池中的内容就会随时变化&#xf…

Linux SVN提交日志校验

#!/bin/bash export LANG"en_US.UTF-8" #确保中文日志显示正常&#xff0c;便于统计日志 REPOS"$1" TXN"$2" #限制日志长度 LENGTH20 #exit 0SVNLOOK"/usr/bin/svnlook" BLACKLIST".* *.o *.chw *.pck ~\$*"function error_…

五、菜单管理

云尚办公系统&#xff1a;菜单管理 B站直达【为尚硅谷点赞】: https://www.bilibili.com/video/BV1Ya411S7aT 本博文以课程相关为主发布&#xff0c;并且融入了自己的一些看法以及对学习过程中遇见的问题给出相关的解决方法。一起学习一起进步&#xff01;&#xff01;&#x…

【Linux系统编程】Linux基本指令详解(二)

文章目录 前言1. cp 指令&#xff08;重要&#xff09;2. mv 指令&#xff08;重要&#xff09;3. cat 指令echo 命令输出重定向追加重定向wc 命令输入重定向 4. more 指令5. less指令&#xff08;重要&#xff09;6. head指令7. tail指令8. 命令行管道&#xff08;了解&#x…

《AVL树》

文章目录 一、AVL树的基本概念二、AVL树的结点定义三、AVL树的插入四、AVL树的旋转1. 右单旋2. 左单旋3. 右左双旋4. 左右双旋 五、AVL树的验证六、AVL树的性能七、源代码 一、AVL树的基本概念 二叉搜索树虽可以缩短查找的效率&#xff0c;但如果数据有序或接近有序二叉搜索树将…

Mysql+ETLCloud CDC+Doris实时数仓同步实战

业务需求及其痛点 很多大型企业需要对各种销售及营销数据进行实时同步分析&#xff0c;例如销售订单信息&#xff0c;库存信息&#xff0c;会员信息&#xff0c;设备状态信息等等&#xff0c;这些统计分析信息可以实时同步到Doris中进行分析和统计&#xff0c;Doris作为分析型…

《深入浅出SSD:固态存储核心技术、原理与实战》----学习记录(一)

前言 传统数据存储介质有磁带、光盘等&#xff0c;但更多的是硬盘(HDD)。随着数据呈爆炸式增长&#xff0c;对数据存储介质在速度上、容量上有更高的要求。时势造英雄&#xff0c;固态硬盘(Solid State Disk&#xff0c;SSD)横空出世。SSD使用电子芯片存储数据&#xff0c;没有…

数学建模算法(基于matlab和python)之 改进的欧拉方法与四阶L-K方法(4/10)

实验目的及要求&#xff1a; 1、熟悉各种初值问题的算法&#xff0c;编出算法程序&#xff1b; 2、明确各种算法的精度寓所选步长有密切关系&#xff1b; 3、熟悉在Matlab平台上直接求解常微分方程初值问题。 实验内容&#xff1a; 1、编写改进的欧拉公式通用子程序&#xff0…

禽流感病毒防治VR模拟实训教学效率高-深圳华锐视点

对于临床兽医学实训而言&#xff0c;学生在实验教学中依托传统的教学方式已经无法满足学生的学习效率&#xff0c;理论知识和实验教学无法完美结合。 随着互联网数字化的飞速发展&#xff0c;数字化虚拟仿真教学兴起&#xff0c;有效的提升了传统教学的质量&#xff0c;学生在实…

Blender骨骼绑定

演示视频参考连接:Blender骨骼绑定教程3&#xff1a;清除绑定 & Deform & 权重修改_哔哩哔哩_bilibili 对给定人体Mesh建立骨骼的操作步骤&#xff1a; 在Blender中打开人体Mesh模型&#xff0c;并确保该模型处于object模式。或者使用快捷键“Shift A”并选择“骨骼…

【FFmpeg实战】视频容器

原文地址&#xff1a;https://alphahinex.github.io/2020/03/12/video-container/ 视频容器 我们常见的视频格式有 avi 或 mp4 等&#xff0c;这些所谓的视频格式&#xff0c;实际上指的只是视频容器的格式。就像 zip 格式的压缩包里面可以放置任意类型的文件一样&#xff0c;…

【初识 Docker | 中级篇】 Docker 中部署 Spring Boot 微服务详解

文章目录 前言一、生成 Docker 镜像1.编写Dockerfile2.构建镜像 二、启动容器1.运行服务2.测试 三、jar包映射部署1.更新Dockerfile文件2.构建镜像3.创建&启动容器 总结 前言 本文将为您详细介绍如何在Docker容器中部署Spring Boot服务。 首先&#xff0c;您需要为您的Sp…

Argo CD 实践教程 07

在本章中&#xff0c;我们将探讨如何设置用户访问Argo CD的权限&#xff0c;以及从终端或CI/CD管道连接CLI的选项&#xff0c;以及如何执行基于角色的访问控制。我们将查看单点登录&#xff08;SSO&#xff09;选项&#xff0c;通常这是一个需要付费的功能&#xff0c;但由于Ar…

Bean与@Bean注解

文章目录 一、背景与IoC二、注解Bean1、Bean注解用法2、Bean注解源码3、Bean注解演示 一、背景与IoC 之前的代码书写现状—耦合度偏高。如下图&#xff0c;业务层需要数据层实现类对象BookDaoImpl&#xff0c;于是自己new了一个&#xff0c;此时&#xff0c;当数据层类名改为B…

隐藏菜单之菜单和搜索

先看效果&#xff1a; 再看代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>菜单</title><style>/* css代码开始*/* {margin: 0;padding: 0;box-sizing: border-box;}b…

人工智能(pytorch)搭建模型15-手把手搭建MnasNet模型,并实现模型的训练与预测

大家好&#xff0c;我是微学AI&#xff0c;今天给大家介绍一下人工智能(pytorch)搭建模型15-手把手搭建MnasNet模型&#xff0c;并实现模型的训练与预测&#xff0c;本文将介绍MnasNet模型的原理&#xff0c;并使用PyTorch框架构建一个MnasNet模型用于图像分类任务&#xff0c;…

1、电商数仓(用户行为采集平台)数据仓库概念、用户行为日志、业务数据、模拟数据、用户行为数据采集模块、日志采集Flume

1、数据仓库概念 数据仓库&#xff08; Data Warehouse &#xff09;&#xff0c;是为企业制定决策&#xff0c;提供数据支持的。可以帮助企业&#xff0c;改进业务流程、提高产品质量等。 数据仓库的输入数据通常包括&#xff1a;业务数据、用户行为数据和爬虫数据等。 业务数…

Linux——使用第三方库链接的方式——动态式

回顾上文&#xff1a; (122条消息) Linux使用第三方库链接的使用方式——静态式_橙予清的zzz~的博客-CSDN博客https://blog.csdn.net/weixin_69283129/article/details/131414804?spm1001.2014.3001.5502 上篇文章中&#xff0c;我讲到了关于链接第三方库作为静态库的使…

股票技术分析方法综述

文章目录 K线均线MACDKDJ和RSIBOLL线趋势理论、支撑位和压力位形态理论量价关系理论道氏理论波浪理论江恩理论缠论自定义指标 K线 K线的组合形态是K线技术分析中的重要部分&#xff0c;包括早晨之星、黄昏之星、红三兵、黑三兵等。 早晨之星&#xff1a;由三根K线组成&#x…

OpenGL 抗锯齿

1.简介 你可以看到&#xff0c;我们只是绘制了一个简单的立方体&#xff0c;你就能注意到它存在锯齿边缘。 可能不是非常明显&#xff0c;但如果你离近仔细观察立方体的边缘&#xff0c;你就应该能够看到锯齿状的图案。如果放大的话&#xff0c;你会看到下面的图案&#xff1a…