flyway和liquibase的使用樣例

2022-09-17 19:42:14 字數 3966 閱讀 6062

--** 

在**上我們有svn和git等諸多的版本控制方法。

但是在資料庫上卻沒有相應的工具。一度導致多環境見的資料庫同步難以維持。

flyway和liquibase都是常見的資料庫版本控制工具。

flyway社群版的功能相對完全免費的liquibase來說簡直毫無可比性。

因此免費使用者的話強烈liquibase。

樣例**已上傳至github:樣例基於spring boot。

flyway:

spring:

datasource:

platform: mysql

url: jdbc:mysql://***:3306/***?useunicode=true&characterencoding=utf8&usessl=false

username: ***

password: ***

driver-class-name: com.mysql.jdbc.driver

jpa:

database: mysql

show-sql: true

hibernate:

ddl-auto: none

flyway:

locations: classpath:db/migration

resources\db\migration\目錄下檔案:

v1_0__createbook.sql

create table `book`  (

`id` int(11) not null auto_increment,

`name` varchar(255) character set utf8mb4 collate utf8mb4_unicode_ci not null,

`create_time` datetime(0) not null,

primary key (`id`) using btree

) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic;

create table `test` (

`id` int(11) not null auto_increment,

`name` varchar(255) character set utf8mb4 collate utf8mb4_unicode_ci not null,

primary key (`id`) using btree

) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic;

create trigger book_trigger after insert on book for each row begin

insert into test (`name`) values ('1');

end;

v1_1__insertbook.sql

insert into book(name, create_time) values ('測試', '2018-04-21 16:53:48');
liquibase:

spring:

datasource:

platform: mysql

url: jdbc:mysql://***:3306/***?useunicode=true&characterencoding=utf8&usessl=false

username: ***

password: ***

driver-class-name: com.mysql.jdbc.driver

jpa:

database: mysql

show-sql: true

hibernate:

ddl-auto: none

liquibase:

change-log: classpath:db/changelog/db.changelog-master.xml

resources\db\changelog\目錄下檔案:

db.changelog-1.0.sql

--liquibase formatted sql

--changeset mahaoyu:1.0

create table `book` (

`id` int(11) not null auto_increment,

`name` varchar(255) character set utf8mb4 collate utf8mb4_unicode_ci not null,

`create_time` datetime(0) not null,

primary key (`id`) using btree

) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic;

--changeset mahaoyu:1.1

create table `test` (

`id` int(11) not null auto_increment,

`name` varchar(255) character set utf8mb4 collate utf8mb4_unicode_ci not null,

primary key (`id`) using btree

) engine = innodb character set = utf8mb4 collate = utf8mb4_unicode_ci row_format = dynamic;

db.changelog-1.1.sql

--liquibase formatted sql

--changeset mahaoyu:1.2 splitstatements:false

create trigger book_trigger after insert on book for each row begin

insert into test (`name`) values ('1');

end;

--changeset mahaoyu:1.3 splitstatements:false

create procedure `proc_adder` ( in a int, in b int, out sum int ) begin

declare

c int;

ifa is null then

set a = 0;

end if;

ifb is null then

set b = 0;

end if;

set sum = a + b;

end;

--changeset mahaoyu:1.4

create view oceania as select * from book;

--changeset mahaoyu:1.5 splitstatements:false

create function hello ( ) returns varchar ( 255 ) begin

return 'hello world,i am mysql';

end;

--changeset zhangsan:1.6

insert into book(name, create_time) values ('測試', '2018-04-21 16:53:48');

db.changelog-master.xml

<?xml version="1.0" encoding="utf-8"?>

這裡對liquibase的事務、觸發器等進行了示例。另外liquibase還支援xml語法,以增加對不同資料庫的支援。

Flyway配置釋義

flyway配置釋義 flyway.baseline description對執行遷移時基準版本的描述.flyway.baseline on migrate當遷移時發現目標schema非空,而且帶有沒有元資料的表時,是否自動執行基準遷移,預設false.flyway.baseline version...

flyway在gradle中的運用

修改了gradle中關於flyway應用的相關說明 1.satisfy.properties關於flyway 的相關配置 flyway configuration flyway.migrations.path filesystem sql db migration flyway.baselineon...

flyway的三種命名

一 v versioned migrations 每個檔案只會被執行一次 version必須唯一,可以用遞增的整型和點 組合 下面都是合法的 常用於建立 修改 刪除表 插入 修改資料等 如 create table car id int not null primary key,license pl...