Flyway 数据库版本控制

1、pom.xml

   <dependencies>
        <!--springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>
        <!--mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!--jdbc驱动-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>
        <!--flyway-->
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>6.1.0</version>
        </dependency>
    </dependencies>

2、配置 jdbc 和 flyway

# ***************** 数据源配置 开始 ******************************** #
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/flyway?characterEncoding=utf-8&&serverTimezone=GMT%2B8&useUnicode=true&useSSL=false&allowMultiQueries=true
    username: root
    password: 123456
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      minimum-idle: 20
      maximum-pool-size: 50
      auto-commit: true
      idle-timeout: 30000
      pool-name: DatebookHikariCP
      max-lifetime: 1800000
      connection-timeout: 30000
      connection-test-query: SELECT 1
  # ***************** nacos、seata配置 开始 ******************************** #
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

  # ***************** FlayWay配置 开始 ******************************** #
  flyway:
    # 数据库名称
    #    schemas: test
    # flyway自动配置 true 开启
    enabled: false
    #设定 SQL 脚本的目录,多个路径使用逗号分隔, 比如取值为 classpath:db/migration,filesystem:/sql-migrations
    locations:
      - classpath:db/migration
    # 如果数据库不是空表,需要设置成 true,否则启动报错
    baseline-on-migrate: true
    # 与 baseline-on-migrate: true 搭配使用
    baseline-version: 0
    encoding: UTF-8
    # 开发环境最好开启 outOfOrder, 生产环境关闭 outOfOrder .
    out-of-order: true
    # 禁止清理数据库表
    clean-disabled: true
    # 校验
    validate-on-migrate: true
    # 版本控制日志表,默认flyway_schema_history,不同系统建议修改改数据
    table: flyway_schema_history

  # ***************** mybatis配置 开始 ******************************** #
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
    auto-mapping-behavior: full
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    cache-enabled: false
    local-cache-scope: statement
  mapper-locations: classpath:/mapper/**/*Mapper.xml
  type-aliases-package: nxcloud.member.domain.entity.po

#***************** 日志 配置 ********************************#
logging:
  level:
    root: info
    nxcloud: debug
    coupon: debug
  file:
    name: /service.log
  pattern:
    level: "%X{RequestId} %5p"
#***************** Server、Swagger 配置 ********************************#
server:
  port: 8083
swagger:
  enable: true
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: ALWAYS
#***************** Feign超时 配置 ********************************#
feign:
  hystrix:
    enabled: false
  client:
    config:
      default:
        connectTimeout: 900000
        readTimeout: 900000


3、编写sql文件


配置的sql文件路径和默认的一致,都在src\main\resources\db\migration下,创建文件名称要符合flyway规范,当然具体的命名规则也都可以通过配置修改,这个使用默认的就可以。命名规则如下:

flyway命名规则


其中Versioned 用来进行常规版本升级;Undo 用来进行回滚,不建议使用;Repeatable是可重复执行的sql,每次初始化都会运行。
例如编写sql文件命名为:V4.5.1__version_update.sql


4、错误处理

sql文件出错,删除 flyway_schema_history表错误的一行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值