背景:基于上篇springboot整合postgres进行
一、pom.xml添加flyway依赖
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>6.0.7</version>
</dependency>
二、application.yml增加flyway配置
spring:
flyway:
# 是否启用flyway
enabled: true
## 编码格式,默认UTF-8
encoding: UTF-8
## 迁移sql脚本文件存放路径,默认db/migration
locations: classpath:db/migration
## 迁移sql脚本文件名称的前缀,默认V
sqlMigrationPrefix: V
## 迁移sql脚本文件名称的分隔符,默认2个下划线__
sqlMigrationSeparator: __
# 迁移sql脚本文件名称的后缀
sqlMigrationSuffixes: .sql
# 迁移时是否进行校验,默认true
validateOnMigrate: true
# 设置为true,当迁移发现数据库非空且存在没有元数据的表时,自动执行基准迁移,新建schema_version表
baselineOnMigrate: true
# postgres10没有引入这个不提示错误,postgres12没有引入会报错
schemas: myschema, site_issue
三、在指定目录下classpath:db/migration创建V1__INIT_DATABASE.sql文件,内容如下:
-- account 表 id 使用uuid 类型,需要先添加uuid-ossp模块。
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE site_issue.issue_detail (
ID INT PRIMARY KEY NOT NULL,
site_id INTEGER,
issue_key TEXT COLLATE pg_catalog."default",
alert_type TEXT COLLATE pg_catalog."default",
"alert_resolution " TEXT COLLATE pg_catalog."default",
"duration_start " DATE,
"duration_end " DATE,
org_id INTEGER
);
CREATE TABLE myschema.COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL,
JOIN_DATE DATE
);
INSERT INTO myschema.COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (1, 'Paul', 32, 'California', 20000.00,'2001-07-13');
INSERT INTO myschema.COMPANY (ID,NAME,AGE,ADDRESS,JOIN_DATE) VALUES (2, 'Allen', 25, 'Texas', '2007-12-13');
INSERT INTO myschema.COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (3, 'Teddy', 23, 'Norway', 20000.00, DEFAULT );
INSERT INTO myschema.COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00, '2007-12-13' ),
(5, 'David', 27, 'Texas', 85000.00, '2007-12-13');
CREATE TABLE "myschema"."test_jsonb"(
"testid" varchar(64),
"testjson" jsonb
)
WITH (OIDS=FALSE);
INSERT INTO myschema.test_jsonb VALUES (uuid_generate_v1(), '{"personId":"123","name":"李四","age":"18","createTime":"2019-10-31 17:30:30"}');
INSERT INTO myschema.test_jsonb VALUES (uuid_generate_v1(), '{"personId":"125","name":"王五","age":"20","createTime":"2019-10-31 17:31:33"}');
四、运行主程序MySpringbootTemplateApplication
H:\JDK\jdk-13.0.1\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true &