Flyway 简单入门

1,052 阅读3分钟

链接

官方文档:flywaydb.org/documentati…

简介

flyway 用来做数据库版本控制,也可以说是数据迁移。

场景

以前公司里面有关于数据库表或者数据更新的情况时,使用手动执行某段SQL,或者某段脚本来进行数据库变更,现在我们可以用过flyway自动执行,省去中间一些容易出现问题的环节。

使用

导入flyway包

// gradle 方式
// https://mvnrepository.com/artifact/org.flywaydb/flyway-core
implementation group: 'org.flywaydb', name: 'flyway-core', version: '8.5.12'
// maven 方式
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>8.5.12</version>
</dependency>

spring boot 配置文件

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:postgresql://127.0.0.1:54322/postgres
    username: postgres
    password: 123456

  flyway:
    # 开启flyway,也可以关闭
    enabled: true
    # 默认位置 /db/migrations, 可以自行设置文件夹, 多个路径用逗号分割
    locations: classpath:/db/migrations
    # 开发开启,生产关闭
    out-of-order: true
    # 设置flyway在数据库中迁移的表名, 默认flyway_schema_history
    table: flyway_schema_history
    # 设置受到flyway 管理的schema,可以是多个,但是只会在datasrouce url连接上的schema执行迁移脚本
    schemas: public
    

添加sql脚本

除了SQL脚本,还支持Java文件方式,不做讨论

image.png

迁移文件版本号规则,前缀还支持 U、R 不做讨论

image.png

我们来看一下SQL脚本

create table users (
   id bigint not null,
   name varchar(50) not null,
   email varchar(50) not null,
   mobile varchar(20) not null,
   age int2 not null,
   address varchar(50) not null,
   create_time timestamp not null,
   last_update_time timestamp not null,
   constraint users_pkey primary key (id)
);
create unique index idx_users_01 on users(name);
insert into users values (1, 'test', 'test@163.com', '18000001111', 18, 'Beijing', current_timestamp, current_timestamp);

启动服务

2022-06-13 14:23:40.059  INFO 28640 --- [           main] com.lv.testmp.TestMpApplication          : The following 1 profile is active: "local"
2022-06-13 14:23:41.408  WARN 28640 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.lv.testmp.dao.mapper]' package. Please check your configuration.
2022-06-13 14:23:42.193  INFO 28640 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9001 (http)
2022-06-13 14:23:42.209  INFO 28640 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-06-13 14:23:42.209  INFO 28640 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.63]
2022-06-13 14:23:42.476  INFO 28640 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-06-13 14:23:42.476  INFO 28640 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2291 ms
 _ _   |_  _ _|_. ___ _ |    _ 
| | |\/|_)(_| | |_\  |_)||_|_\ 
     /               |         
                        3.5.2 
2022-06-13 14:23:45.270  INFO 28640 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 8.5.12 by Redgate
2022-06-13 14:23:45.270  INFO 28640 --- [           main] o.f.c.internal.license.VersionPrinter    : See what's new here: https://flywaydb.org/documentation/learnmore/releaseNotes#8.5.12
2022-06-13 14:23:45.270  INFO 28640 --- [           main] o.f.c.internal.license.VersionPrinter    : 
2022-06-13 14:23:45.270  INFO 28640 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-06-13 14:23:45.478  INFO 28640 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-06-13 14:23:45.501  INFO 28640 --- [           main] o.f.c.i.database.base.BaseDatabaseType   : Database: jdbc:postgresql://127.0.0.1:54322/postgres (PostgreSQL 14.3)
2022-06-13 14:23:45.562  INFO 28640 --- [           main] o.f.core.internal.command.DbValidate     : Successfully validated 1 migration (execution time 00:00.028s)
2022-06-13 14:23:45.581  INFO 28640 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema "public": << Empty Schema >>
2022-06-13 14:23:45.582  WARN 28640 --- [           main] o.f.core.internal.command.DbMigrate      : outOfOrder mode is active. Migration of schema "public" may not be reproducible.
2022-06-13 14:23:45.607  INFO 28640 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema "public" to version "1.0 - create table user"
2022-06-13 14:23:45.673  INFO 28640 --- [           main] o.f.core.internal.command.DbMigrate      : Successfully applied 1 migration to schema "public", now at version v1.0 (execution time 00:00.103s)
2022-06-13 14:23:45.831  INFO 28640 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9001 (http) with context path ''
2022-06-13 14:23:45.852  INFO 28640 --- [           main] com.lv.testmp.TestMpApplication          : Started TestMpApplication in 6.828 seconds (JVM running for 9.341)

可以看到这行日志:Successfully applied 1 migration to schema "public", now at version v1.0 (execution time 00:00.103s)

注意

迁移脚本命名一定要遵循命名规则,大部分人倒在了这一步

转载需注明出处,抄袭必究