为什么需要Flyway

日常开发常常会遇到一些这样的场景

  1. 小红开发一个模块在本地数据库增加了两个字段,并且改动了dao层的代码提交到git。这时候小黄拉取了代码Run很可能报错。
  2. 如果在上线正式环境的时候,忘记在正式数据库执行sql脚本可能造成严重的问题。
  3. 传统的解决方式是在一个固定的地方添加sql脚本,开发人员相互沟通执行哪个sql脚本

Flyway可以将这一类问题解决,在项目编译期就将改动写入数据库。只要启动成功就没有问题。


Flyway 导入

如果是Gradle,在build.gradle添加依赖

compile "org.flywaydb:flyway-core:4.1.2"

Maven 在pom.xml添加依赖

<dependency><groupId>org.flywaydb</groupId><artifactId>flyway-core</artifactId><version>4.0.3</version>
</dependency>


实际使用

使用Spring Boot,Gradle 构建项目,添加依赖。

如果是已经开发一段时间的项目需要开启 baselineOnMigrate 否则抛出异常

Found non-empty schema(s) `reshelf2` without metadata table! Use baseline() or set baselineOnMigrate to true to initialize the metadata table. 

application.properties 添加配置

flyway.baselineOnMigrate=true# 是否开启flyway,默认true
flyway.enable=true

当然也可以实现接口

@Component
public class BaselineOnMigrateMigrationStrategy implements FlywayMigrationStrategy {@Overridepublic void migrate(Flyway flyway) {flyway.setBaselineOnMigrate(true);flyway.migrate();}
}

数据库中的schema_version为存储对比脚本版本的表

sql脚本默认放置在 classpath:db/migration

文件以.sql结尾,命名V字开头,后面数字为版本号 例如 V1__init.sql

实例:

摘自 org.flywaydb.core.Flyway

 /*** The locations to scan recursively for migrations.* <p/>* <p>The location type is determined by its prefix.* Unprefixed locations or locations starting with {@code classpath:} point to a package on the classpath and may* contain both sql and java-based migrations.* Locations starting with {@code filesystem:} point to a directory on the filesystem and may only contain sql* migrations.</p>* <p/>* (default: db/migration)*/private Locations locations = new Locations("db/migration");/*** The encoding of Sql migrations. (default: UTF-8)*/private String encoding = "UTF-8";/*** The schemas managed by Flyway.  These schema names are case-sensitive. (default: The default schema for the datasource connection)* <p>Consequences:</p>* <ul>* <li>The first schema in the list will be automatically set as the default one during the migration.</li>* <li>The first schema in the list will also be the one containing the metadata table.</li>* <li>The schemas will be cleaned in the order of this list.</li>* </ul>*/private String[] schemaNames = new String[0];/*** <p>The name of the schema metadata table that will be used by Flyway. (default: schema_version)</p><p> By default* (single-schema mode) the metadata table is placed in the default schema for the connection provided by the* datasource. </p> <p> When the <i>flyway.schemas</i> property is set (multi-schema mode), the metadata table is* placed in the first schema of the list. </p>*/private String table = "schema_version";/*** The target version up to which Flyway should consider migrations. Migrations with a higher version number will* be ignored. The special value {@code current} designates the current version of the schema (default: the latest version)*/private MigrationVersion target = MigrationVersion.LATEST;/*** Whether placeholders should be replaced. (default: true)*/private boolean placeholderReplacement = true;/*** The map of &lt;placeholder, replacementValue&gt; to apply to sql migration scripts.*/private Map<String, String> placeholders = new HashMap<String, String>();/*** The prefix of every placeholder. (default: ${ )*/private String placeholderPrefix = "${";/*** The suffix of every placeholder. (default: } )*/private String placeholderSuffix = "}";/*** The file name prefix for sql migrations. (default: V)* <p/>* <p>Sql migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix ,* which using the defaults translates to V1_1__My_description.sql</p>*/private String sqlMigrationPrefix = "V";/*** The file name prefix for repeatable sql migrations. (default: R)* <p/>* <p>Repeatable sql migrations have the following file name structure: prefixSeparatorDESCRIPTIONsuffix ,* which using the defaults translates to R__My_description.sql</p>*/private String repeatableSqlMigrationPrefix = "R";/*** The file name separator for sql migrations. (default: __)* <p/>* <p>Sql migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix ,* which using the defaults translates to V1_1__My_description.sql</p>*/private String sqlMigrationSeparator = "__";/*** The file name suffix for sql migrations. (default: .sql)* <p/>* <p>Sql migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix ,* which using the defaults translates to V1_1__My_description.sql</p>*/private String sqlMigrationSuffix = ".sql";

可以自定义配置,但是建议不要。

注意:sql脚本需要有相应的版本号,例如如果想让 V2__init.sql执行 需要有V1__init.sql作为一个基准对比,然后flyway才会执行相应的sql脚本。


一些链接

https://flywaydb.org/documentation/gradle/

http://stackoverflow.com/questions/33029311/setting-flyway-baselineonmigrate-and-baselineversion-using-spring-boot-prope

http://stackoverflow.com/questions/30013953/how-to-use-jdbc-authentication-of-spring-boot-spring-security-with-flyway

https://github.com/spark-jobserver/spark-jobserver/issues/503

作者:YoRuo_
链接:https://www.jianshu.com/p/7f7279376349

转载于:https://www.cnblogs.com/diandianquanquan/p/10606976.html

数据库迁移Flyway相关推荐

  1. flyway数据迁移_使用Flyway在Java EE中进行数据库迁移

    flyway数据迁移 任何Java EE应用程序的数据库模式都会随着业务逻辑一起发展. 这使得数据库迁移对于任何Java EE应用程序都很重要. 您是否还在执行应用程序时手动执行它们? 它仍然是一个锁 ...

  2. 使用Flyway在Java EE中进行数据库迁移

    任何Java EE应用程序的数据库模式都会随着业务逻辑一起发展. 这使得数据库迁移对于任何Java EE应用程序都非常重要. 您是否还在执行应用程序时手动执行它们? 它仍然是一个锁定步骤过程,还是作为 ...

  3. jpa mysql脚本迁移_Spring Boot 数据库迁移:概述

    原标题:Spring Boot 数据库迁移:概述 前言: 在这里的数据库迁移主要是对数据库结构版本管理和迁移. 一.为什么需要数据库迁移工具? 那在没有使用迁移工具的时候,我们会碰到什么呢? (1)多 ...

  4. flyway版本号_各个互联网公司都在用的开源数据库控制器Flyway

    开源的数据库控制器 在开发中,我们经常会遇到上线数据库表的情况,代码上我们有git,svn这样优秀的版本控制软件,但是数据库的迭代我们不能使用手工的方式迭代吧?或者说每次上线前手工去数据库执行.这样带 ...

  5. 数据库版本控制Flyway

    摘要 在频繁发布版本的情况下,数据库版本难以控制一直是一个头疼的问题,本文主要介绍如何利用Flyway工具实现数据库版本控制. 概述 数据库的版本控制一般都是通过代码管理工具统一管理SQL脚本,但是仅 ...

  6. MySQL基础之 恢复数据和数据库迁移

    1.mysql命令或者source命令恢复数据 这两个命令在进行恢复数据的时候要检查是否创建数据库.如果数据库不存在,则恢复失败. 数据库迁移 1.相同版本的mysql数据库之间的迁移. mysqld ...

  7. 简单分析Flask 数据库迁移详情

    本文给大家分享的是 Flask 数据库迁移详情,db.create_all()不会重新创建表或是更新表,需要先使用db.drop_all()删除数据库中所有的表之后再调用db.create_all() ...

  8. 数据库迁移_【干货分享】DM数据库迁移方法(物理迁移)

    在数据库的维护过程中,可能涉及换服务器,或者需要现网数据库环境测试的情况,这时,最简单快速的办法就是将源数据库相关的文件拷贝到目标主机,然后注册数据库实例服务.这就是数据库的物理迁移过程,可以是从wi ...

  9. 数据库迁移用到的命令

    //在程序包管理器控制台中执行以下语句,安装EntityFramework. PM> Install-Package EntityFramework //执行成功后,控制台应用程序代码结构中,添 ...

最新文章

  1. 继续不务正业,今天来弄弄R
  2. 介绍一个.Net资源站点
  3. 程序员面试题精选100题(45)-Singleton(C/C++/C#)
  4. 云计算机创意名,有创意的道路名字推荐,分享一些好听有内涵的路名
  5. 定期定量采购_采购的四种方法
  6. centos eclipse php,centos打不开eclipse怎么办?
  7. “两步路·户外助手”谷歌类图源
  8. 在线式极限学习机OS-ELM
  9. Java中tostring重写_使用ToStringBuilder重写toString方法
  10. huffman编码的程序流程图_哈夫曼编码原理详解及应用实例,哈夫曼编码算法流程图 - 全文...
  11. POJ 1179 Polygon
  12. 以太网未识别的网络win10_win10系统遇到以太网无法识别网络如何解决
  13. ava程序员职业生涯规划范文
  14. JavaScript面试题
  15. 谜底是计算机的谜语英语,有关英语谜语大全及答案
  16. MySQL数据库面试题(超详细)
  17. ML.net 3-情绪预测
  18. Unity3D之调用WinRT组件
  19. linux添加硬盘分区设置柱面,Linux添加硬盘并分区格式化
  20. 男人的快乐不就来了?

热门文章

  1. 收缩sqlserver事务日志
  2. openstack VM可以ping外部网络,但是外部网络ping不通VM
  3. 请问:如何在C#简单分布式程序的数据层中为其它层留出很好的接口?????...
  4. AndroidStudio_从Eclipse到AndroidStudio开发工具_两者使用的区别_通过向导新建项目和引入module---Android原生开发工作笔记68
  5. 大数据_Flink_流式处理_简介_认识一下什么是BI中的ETL---Flink工作笔记0005
  6. OAuth2.0_授权服务配置_令牌服务和令牌端点配置_Spring Security OAuth2.0认证授权---springcloud工作笔记143
  7. 关于vc++调用 exe文件的问题
  8. js中substr与substring的区别
  9. 使用setsockopt TCP_NODELAY禁用 Nagle算法
  10. html 如何 创建目录,html - javascript:如何自动生成一篇文章的目录