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

相关文章

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

《SpringBoot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)》:本文主要介绍SpringBoot拦截器Interceptor与过滤器Filter深度解析... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实

基于C#实现MQTT通信实战

《基于C#实现MQTT通信实战》MQTT消息队列遥测传输,在物联网领域应用的很广泛,它是基于Publish/Subscribe模式,具有简单易用,支持QoS,传输效率高的特点,下面我们就来看看C#实现... 目录1、连接主机2、订阅消息3、发布消息MQTT(Message Queueing Telemetr

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

Python日期和时间完全指南与实战

《Python日期和时间完全指南与实战》在软件开发领域,‌日期时间处理‌是贯穿系统设计全生命周期的重要基础能力,本文将深入解析Python日期时间的‌七大核心模块‌,通过‌企业级代码案例‌揭示最佳实践... 目录一、背景与核心价值二、核心模块详解与实战2.1 datetime模块四剑客2.2 时区处理黄金法

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

Spring Boot集成Logback终极指南之从基础到高级配置实战指南

《SpringBoot集成Logback终极指南之从基础到高级配置实战指南》Logback是一个可靠、通用且快速的Java日志框架,作为Log4j的继承者,由Log4j创始人设计,:本文主要介绍... 目录一、Logback简介与Spring Boot集成基础1.1 Logback是什么?1.2 Sprin

Linux高并发场景下的网络参数调优实战指南

《Linux高并发场景下的网络参数调优实战指南》在高并发网络服务场景中,Linux内核的默认网络参数往往无法满足需求,导致性能瓶颈、连接超时甚至服务崩溃,本文基于真实案例分析,从参数解读、问题诊断到优... 目录一、问题背景:当并发连接遇上性能瓶颈1.1 案例环境1.2 初始参数分析二、深度诊断:连接状态与

C#实现高性能Excel百万数据导出优化实战指南

《C#实现高性能Excel百万数据导出优化实战指南》在日常工作中,Excel数据导出是一个常见的需求,然而,当数据量较大时,性能和内存问题往往会成为限制导出效率的瓶颈,下面我们看看C#如何结合EPPl... 目录一、技术方案核心对比二、各方案选型建议三、性能对比数据四、核心代码实现1. MiniExcel

POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能

《POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能》ApachePOI是一个流行的Java库,用于处理MicrosoftOffice格式文件,提供丰富API来创建、读取和修改O... 目录前言:Apache POIEasyPoiEasyExcel一、EasyExcel1.1、核心特性

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与