日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

flyway版本号_Flyway版本化管理数据库脚本

發布時間:2023/12/2 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 flyway版本号_Flyway版本化管理数据库脚本 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

假如我們有一個叫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表示Versioned,R表示Repeatable

version: 標識版本號,由一個或多個數字構成,數字之間的分隔符可用點.或下劃線_

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-version=1 # Version to start migration.

flyway.baseline-on-migrate=false # Whether to execute migration against a non-empty schema with no metadata table

flyway.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 updating

flyway.sql-migration-prefix=V # The file name prefix for Sql migrations

flyway.sql-migration-separator=__ # The file name separator for Sql migrations

flyway.sql-migration-suffix=.sql # The file name suffix for Sql migrations

flyway.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;

@Slf4j

public 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);

}

}

}

}

}

資料來源

如有問題,請留言。

原創不易,關注誠可貴,轉發價更高!轉載請注明出處,讓我們互通有無,共同進步,歡迎溝通交流。

我會持續分享Java軟件編程知識和程序員發展職業之路,歡迎關注!

總結

以上是生活随笔為你收集整理的flyway版本号_Flyway版本化管理数据库脚本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。