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 Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Pandas使用SQLite3实战

《Pandas使用SQLite3实战》本文主要介绍了Pandas使用SQLite3实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1 环境准备2 从 SQLite3VlfrWQzgt 读取数据到 DataFrame基础用法:读

Python实战之屏幕录制功能的实现

《Python实战之屏幕录制功能的实现》屏幕录制,即屏幕捕获,是指将计算机屏幕上的活动记录下来,生成视频文件,本文主要为大家介绍了如何使用Python实现这一功能,希望对大家有所帮助... 目录屏幕录制原理图像捕获音频捕获编码压缩输出保存完整的屏幕录制工具高级功能实时预览增加水印多平台支持屏幕录制原理屏幕

最新Spring Security实战教程之Spring Security安全框架指南

《最新SpringSecurity实战教程之SpringSecurity安全框架指南》SpringSecurity是Spring生态系统中的核心组件,提供认证、授权和防护机制,以保护应用免受各种安... 目录前言什么是Spring Security?同类框架对比Spring Security典型应用场景传统

最新Spring Security实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)

《最新SpringSecurity实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)》本章节介绍了如何通过SpringSecurity实现从配置自定义登录页面、表单登录处理逻辑的配置,并简单模拟... 目录前言改造准备开始登录页改造自定义用户名密码登陆成功失败跳转问题自定义登出前后端分离适配方案结语前言

OpenManus本地部署实战亲测有效完全免费(最新推荐)

《OpenManus本地部署实战亲测有效完全免费(最新推荐)》文章介绍了如何在本地部署OpenManus大语言模型,包括环境搭建、LLM编程接口配置和测试步骤,本文给大家讲解的非常详细,感兴趣的朋友一... 目录1.概况2.环境搭建2.1安装miniconda或者anaconda2.2 LLM编程接口配置2

基于Canvas的Html5多时区动态时钟实战代码

《基于Canvas的Html5多时区动态时钟实战代码》:本文主要介绍了如何使用Canvas在HTML5上实现一个多时区动态时钟的web展示,通过Canvas的API,可以绘制出6个不同城市的时钟,并且这些时钟可以动态转动,每个时钟上都会标注出对应的24小时制时间,详细内容请阅读本文,希望能对你有所帮助...

Spring AI与DeepSeek实战一之快速打造智能对话应用

《SpringAI与DeepSeek实战一之快速打造智能对话应用》本文详细介绍了如何通过SpringAI框架集成DeepSeek大模型,实现普通对话和流式对话功能,步骤包括申请API-KEY、项目搭... 目录一、概述二、申请DeepSeek的API-KEY三、项目搭建3.1. 开发环境要求3.2. mav