如果咱们有一个叫shiny的我的项目,它是由一个程序Shiny-Server 和一个数据库 Shiny-DB组成的;
简略结构图如下:
然而很多时候,事实开发团队是这样的:
咱们的我的项目shiny我的项目的运行环境是有多套的,
咱们善于解决代码层面的问题。
版本控制工具git十分广泛而且好用
咱们有继续集成和继续构建的工具
咱们很好的定义了测试和生产环境的公布流程
然而咱们的数据库的版本如何管制呢?
以后现状
十分可怜的是咱们还不能很好的解决数据库的版本治理问题,
很多的我的项目依赖运维人员手动的执行SQL脚本,
有的时候甚至为了疾速解决bug去疾速的在命令行上执行SQL脚本,那么问题来了。
通常这些问题的答案是:鬼晓得。
引入目标
flyway解决了下面的这些问题。
目前Flyway反对的数据库还是挺多的,包含:
Oracle, SQL Server, SQL Azure, DB2, DB2 z/OS,
MySQL(including Amazon RDS), MariaDB,
Google Cloud SQL, PostgreSQL(including Amazon RDS and Heroku),
Redshift, Vertica, H2, Hsql, Derby, SQLite, SAP HANA,
solidDB, Sybase ASE and Phoenix。
Flyway的执行流程
Flyway是一款开源的数据库版本管理工具,
它更偏向于规约优于配置的形式。
Flyway能够独立于利用实现治理并跟踪数据库变更,反对数据库版本主动降级,
并且有一套默认的规约,不须要简单的配置,
Migrations能够写成SQL脚本,也能够写在Java代码中,
不仅反对Command Line和Java API,还反对Build构建工具和Spring Boot等,
同时在分布式环境下可能安全可靠地降级数据库,同时也反对失败复原等。
每次不论是数据库的表构造或者表数据的变更,
你只须要把问题当成一次数据库的降级,
简略的创立一个比以后版本更高的版本的迁徙SQL文件或者Java文件,
下次Flyway启动的时候,他会找到这些脚本并把它更新到数据库。
脚本或者Java迁徙脚本的命名规定:
其中的文件名由以下局部组成,除了应用默认配置外,某些局部还可自定义规定。
- prefix: 可配置,前缀标识,默认值
V
示意Versioned,R
示意Repeatable - version: 标识版本号,由一个或多个数字形成,数字之间的分隔符可用点
.
或下划线_
- separator: 可配置,用于分隔版本标识与形容信息,默认为两个下划线
__
- description: 形容信息,文字之间能够用下划线或空格分隔
- suffix: 可配置,后续标识,默认为
.sql
实现门路
实在我的项目版本更新场景中,咱们不可能再基于人力去做这件事件,咱们抉择的是API的形式。
应用步骤如下:
个别大家都是写SQL脚本,也反对通过写Java代码的形式来实现。
Java形式写迁徙性能
应用步骤:
目前的集成形式
应用的是springboot的形式集成了Flyway;
配置参数:可自行翻译和参考抉择去配置
flyway.baseline-description= # The description to tag an existing schema with when executing baseline.flyway.baseline-version=1 # Version to start migration.flyway.baseline-on-migrate=false # Whether to execute migration against a non-empty schema with no metadata tableflyway.check-location=false # Check that migration scripts location exists.flyway.clean-on-validation-error=false # will clean all objects. Warning! Do NOT enable in production!flyway.enabled=true # Enable flyway.flyway.encoding=UTF-8 # The encoding of migrations.flyway.ignore-failed-future-migration=true # Ignore future migrations when reading the metadata table.flyway.init-sqls= # SQL statements to execute to initialize a connection immediately after obtaining it.flyway.locations=classpath:db/migration # locations of migrations scripts.flyway.out-of-order=false # Allows migrations to be run "out of order".flyway.placeholder-prefix= # The prefix of every placeholder.flyway.placeholder-replacement=true # Whether placeholders should be replaced.flyway.placeholder-suffix=} # The suffix of every placeholder.flyway.placeholders.*= # Placeholders to replace in Sql migrations.flyway.schemas= # Default schema of the connection and updatingflyway.sql-migration-prefix=V # The file name prefix for Sql migrationsflyway.sql-migration-separator=__ # The file name separator for Sql migrationsflyway.sql-migration-suffix=.sql # The file name suffix for Sql migrationsflyway.table=schema_version # The name of Flyway's metadata table.flyway.url= # JDBC url of the database to migrate. If not set, the primary configured data source is used.flyway.user= # Login user of the database to migrate. If not set, use spring.datasource.username value.flyway.password= # JDBC password if you want Flyway to create its own DataSource.flyway.validate-on-migrate=true # Validate sql migration CRC32 checksum in classpath.
package db.migration;/** * @author carter * create_date 2020/8/13 17:39 * description java数据库变更模板代码 */import lombok.extern.slf4j.Slf4j;import org.flywaydb.core.api.migration.BaseJavaMigration;import org.flywaydb.core.api.migration.Context;import java.sql.ResultSet;import java.sql.Statement;@Slf4jpublic class V2__test extends BaseJavaMigration { @Override public void migrate(Context context) throws Exception { try (Statement select = context.getConnection().createStatement()) { try (ResultSet rows = select.executeQuery("SELECT 1")) { while (rows.next()) { int id = rows.getInt(1); String anonymizedName = "Anonymous" + id; log.info("执行sql脚本:{}",anonymizedName); } } } }}
材料起源
官网: https://flywaydb.org/
实战: https://blog.waterstrong.me/flyway-in-practice/
如有问题,请留言。
原创不易,关注诚可贵,转发价更高!转载请注明出处,让咱们互通有无,共同进步,欢送沟通交流。
我会继续分享Java软件编程常识和程序员倒退职业之路,欢送关注,我整顿了这些年编程学习的各种资源,关注公众号‘李福春继续输入’,发送'学习材料'分享给你!