一流的网站建设哪家好,网页源代码在线查看,网站建设用处,网络公司最怕怎么投诉假如我们有一个叫shiny的项目#xff0c;它是由一个程序Shiny-Server 和一个数据库 Shiny-DB组成的;简单结构图如下#xff1a;image.png但是很多时候#xff0c;现实开发团队是这样的#xff1a;image.png我们的项目shiny项目的运行环境是有多套的#xff0c;我们擅长解决…假如我们有一个叫shiny的项目它是由一个程序Shiny-Server 和一个数据库 Shiny-DB组成的;简单结构图如下image.png但是很多时候现实开发团队是这样的image.png我们的项目shiny项目的运行环境是有多套的我们擅长解决代码层面的问题。版本控制工具git非常普遍而且好用我们有持续集成和持续构建的工具我们很好的定义了测试和生产环境的发布流程image.png但是我们的数据库的版本如何控制呢image.png当前现状非常不幸的是我们还不能很好的处理数据库的版本管理问题很多的项目依赖运维人员手动的执行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等同时在分布式环境下能够安全可靠地升级数据库同时也支持失败恢复等。Flyway工作流程.png每次不管是数据库的表结构或者表数据的变更你只需要把问题当成一次数据库的升级简单的创建一个比当前版本更高的版本的迁移SQL文件或者Java文件下次Flyway启动的时候他会找到这些脚本并把它更新到数据库。脚本或者Java迁移脚本的命名规则其中的文件名由以下部分组成除了使用默认配置外某些部分还可自定义规则。prefix: 可配置前缀标识默认值V表示VersionedR表示Repeatableversion: 标识版本号由一个或多个数字构成数字之间的分隔符可用点.或下划线_separator: 可配置用于分隔版本标识与描述信息默认为两个下划线__description: 描述信息文字之间可以用下划线或空格分隔suffix: 可配置后续标识默认为.sql实现路径真实项目版本更新场景中我们不可能再基于人力去做这件事情我们选择的是API的方式。使用步骤如下API方式使用Flyway.png一般大家都是写SQL脚本也支持通过写Java代码的方式来实现。Java方式写迁移功能使用步骤Java代码方式写迁移代码.png目前的集成方式使用的是springboot的方式集成了Flyway;COLA引入Flyway的流程.png配置参数可自行翻译和参考选择去配置flyway.baseline-description # The description to tag an existing schema with when executing baseline.flyway.baseline-version1 # Version to start migration.flyway.baseline-on-migratefalse # Whether to execute migration against a non-empty schema with no metadata tableflyway.check-locationfalse # Check that migration scripts location exists.flyway.clean-on-validation-errorfalse # will clean all objects. Warning! Do NOT enable in production!flyway.enabledtrue # Enable flyway.flyway.encodingUTF-8 # The encoding of migrations.flyway.ignore-failed-future-migrationtrue # Ignore future migrations when reading the metadata table.flyway.init-sqls # SQL statements to execute to initialize a connection immediately after obtaining it.flyway.locationsclasspath:db/migration # locations of migrations scripts.flyway.out-of-orderfalse # Allows migrations to be run out of order.flyway.placeholder-prefix # The prefix of every placeholder.flyway.placeholder-replacementtrue # 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-prefixV # 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.tableschema_version # The name of Flyways 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-migratetrue # 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 {Overridepublic 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);}}}}}资料来源如有问题请留言。原创不易关注诚可贵转发价更高转载请注明出处让我们互通有无共同进步欢迎沟通交流。我会持续分享Java软件编程知识和程序员发展职业之路欢迎关注