flyway实战

2024-03-02 22:52
文章标签 实战 flyway

本文主要是介绍flyway实战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

flyway是一款用来管理数据库版本的工具框架

一, 添加依赖

<dependency><groupId>org.flywaydb</groupId><artifactId>flyway-core</artifactId>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId>
</dependency>

二, 编写配置类

MySQLDatabase

/** Copyright (C) Red Gate Software Ltd 2010-2021** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**         http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package org.flywaydb.core.internal.database.mysql;import lombok.CustomLog;
import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.api.FlywayException;
import org.flywaydb.core.api.MigrationType;
import org.flywaydb.core.api.MigrationVersion;
import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.internal.database.base.Database;
import org.flywaydb.core.internal.database.base.BaseDatabaseType;
import org.flywaydb.core.internal.database.base.Table;
import org.flywaydb.core.internal.database.mysql.mariadb.MariaDBDatabaseType;
import org.flywaydb.core.internal.jdbc.JdbcConnectionFactory;
import org.flywaydb.core.internal.jdbc.JdbcTemplate;
import org.flywaydb.core.internal.jdbc.StatementInterceptor;import java.sql.Connection;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;@Slf4j
public class MySQLDatabase extends Database<MySQLConnection> {// See https://mariadb.com/kb/en/version/private static final Pattern MARIADB_VERSION_PATTERN = Pattern.compile("(\\d+\\.\\d+)\\.\\d+(-\\d+)*-MariaDB(-\\w+)*");private static final Pattern MARIADB_WITH_MAXSCALE_VERSION_PATTERN = Pattern.compile("(\\d+\\.\\d+)\\.\\d+(-\\d+)* (\\d+\\.\\d+)\\.\\d+(-\\d+)*-maxscale(-\\w+)*");private static final Pattern MYSQL_VERSION_PATTERN = Pattern.compile("(\\d+\\.\\d+)\\.\\d+\\w*");/*** Whether this is a Percona XtraDB Cluster in strict mode.*/private final boolean pxcStrict;/*** Whether this database is enforcing GTID consistency.*/private final boolean gtidConsistencyEnforced;/*** Whether the event scheduler table is queryable.*/final boolean eventSchedulerQueryable;public MySQLDatabase(Configuration configuration, JdbcConnectionFactory jdbcConnectionFactory, StatementInterceptor statementInterceptor) {super(configuration, jdbcConnectionFactory, statementInterceptor);JdbcTemplate jdbcTemplate = new JdbcTemplate(rawMainJdbcConnection, databaseType);pxcStrict = isMySQL() && isRunningInPerconaXtraDBClusterWithStrictMode(jdbcTemplate);gtidConsistencyEnforced = isMySQL() && isRunningInGTIDConsistencyMode(jdbcTemplate);eventSchedulerQueryable = false;}private static boolean isEventSchedulerQueryable(JdbcTemplate jdbcTemplate) {try {// Attempt queryjdbcTemplate.queryForString("SELECT event_name FROM information_schema.events LIMIT 1");return true;} catch (SQLException e) {log.debug("Detected unqueryable MariaDB event scheduler, most likely due to it being OFF or DISABLED.");return false;}}static boolean isRunningInPerconaXtraDBClusterWithStrictMode(JdbcTemplate jdbcTemplate) {try {String pcx_strict_mode = jdbcTemplate.queryForString("select VARIABLE_VALUE from performance_schema.global_variables"+ " where variable_name = 'pxc_strict_mode'");if ("ENFORCING".equals(pcx_strict_mode) || "MASTER".equals(pcx_strict_mode)) {log.debug("Detected Percona XtraDB Cluster in strict mode");return true;}} catch (SQLException e) {log.debug("Unable to detect whether we are running in a Percona XtraDB Cluster. Assuming not to be.");}return false;}static boolean isRunningInGTIDConsistencyMode(JdbcTemplate jdbcTemplate) {try {String gtidConsistency = jdbcTemplate.queryForString("SELECT @@GLOBAL.ENFORCE_GTID_CONSISTENCY");if ("ON".equals(gtidConsistency)) {log.debug("Detected GTID consistency being enforced");return true;}} catch (SQLException e) {log.debug("Unable to detect whether database enforces GTID consistency. Assuming not.");}return false;}boolean isMySQL() {return databaseType instanceof MySQLDatabaseType;}boolean isMariaDB() {return databaseType instanceof MariaDBDatabaseType;}boolean isPxcStrict() {return pxcStrict;}/** CREATE TABLE ... AS SELECT ... cannot be used in three scenarios:* - Percona XtraDB Cluster in strict mode doesn't support it* - TiDB doesn't support it (overridden elsewhere)* - When GTID consistency is being enforced. Note that if GTID_MODE is ON, then ENFORCE_GTID_CONSISTENCY is* necessarily ON as well.*/protected boolean isCreateTableAsSelectAllowed() {return !pxcStrict && !gtidConsistencyEnforced;}@Overridepublic String getRawCreateScript(Table table, boolean baseline) {String tablespace =configuration.getTablespace() == null? "": " TABLESPACE \"" + configuration.getTablespace() + "\"";String baselineMarker = "";if (baseline) {if (isCreateTableAsSelectAllowed()) {baselineMarker = " AS SELECT" +"     1 as \"installed_rank\"," +"     '" + configuration.getBaselineVersion() + "' as \"version\"," +"     '" + configuration.getBaselineDescription() + "' as \"description\"," +"     '" + MigrationType.BASELINE + "' as \"type\"," +"     '" + configuration.getBaselineDescription() + "' as \"script\"," +"     NULL as \"checksum\"," +"     '" + getInstalledBy() + "' as \"installed_by\"," +"     CURRENT_TIMESTAMP as \"installed_on\"," +"     0 as \"execution_time\"," +"     TRUE as \"success\"\n";} else {// Revert to regular insert, which unfortunately is not safe in concurrent scenarios// due to MySQL implicit commits after DDL statements.baselineMarker = ";\n" + getBaselineStatement(table);}}return "CREATE TABLE " + table + " (\n" +"    `installed_rank` INT NOT NULL,\n" +"    &

这篇关于flyway实战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/767675

相关文章

从原理到实战深入理解Java 断言assert

《从原理到实战深入理解Java断言assert》本文深入解析Java断言机制,涵盖语法、工作原理、启用方式及与异常的区别,推荐用于开发阶段的条件检查与状态验证,并强调生产环境应使用参数验证工具类替代... 目录深入理解 Java 断言(assert):从原理到实战引言:为什么需要断言?一、断言基础1.1 语

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

Redis 配置文件使用建议redis.conf 从入门到实战

《Redis配置文件使用建议redis.conf从入门到实战》Redis配置方式包括配置文件、命令行参数、运行时CONFIG命令,支持动态修改参数及持久化,常用项涉及端口、绑定、内存策略等,版本8... 目录一、Redis.conf 是什么?二、命令行方式传参(适用于测试)三、运行时动态修改配置(不重启服务

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.