【问题标题】:Flyway issue in dropping tables when the application is stopped应用程序停止时删除表的 Flyway 问题
【发布时间】:2017-12-21 01:31:05
【问题描述】:

概览

我将使用以下配置将 flyway db 迁移添加到现有项目:

  • 项目类型:Spring Boot
  • 数据库:MariaDB

application-local.yaml(Flyway 和数据源配置):

    ...

    flyway:
      enabled: true
      locations: classpath:db.migration
      baselineOnMigrate: true

    spring:
        jackson:
            serialization:
                indent_output: true
        devtools:
            restart:
                enabled: false
            livereload:
                enabled: false
        datasource:
            type: com.zaxxer.hikari.HikariDataSource
            url: jdbc:mariadb://localhost:3306/migration
            username: root
            password: testing1
            hikari:
                data-source-properties:
                    leakDetectionThreshold: 2000
                    cachePrepStmts: true
                    prepStmtCacheSize: 250
                    prepStmtCacheSqlLimit: 2048
                    useServerPrepStmts: true
        jpa:
            database-platform: org.hibernate.dialect.MySQL5Dialect
            database: MYSQL
            show-sql: false
            hibernate.id.new_generator_mappings: true
            properties:
                hibernate.cache.use_second_level_cache: false
                hibernate.cache.use_query_cache: false
                hibernate.generate_statistics: false
                hibernate.hbm2ddl.import_files: schema-mysql.sql
                hibernate.hbm2ddl.import_files_sql_extractor: org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor
            open-in-view: false
            hibernate:
                ddl-auto: create-drop
                naming:
                    physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
                    implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
    ...

问题:

当应用程序停止时,数据库表不会被删除。但是当我使用ddl-auto: create-drop 时,预计表会被删除。

注意: 我知道将 Flyway 用于 volatile DB 似乎有点奇怪。因为有一些配置类可以在启动时插入初始数据。如果应用程序停止时db没有被删除,它会在下次启动时出现抱怨[例如:违反唯一约束],因为初始值已经插入。

如果有人能帮助我提供真正的解决方案,我将不胜感激。

【问题讨论】:

    标签: java spring-boot gradle mariadb flyway


    【解决方案1】:

    documentation for create-drop 声明:

    删除架构并在 SessionFactory 启动时重新创建它。此外,在 SessionFactory 关闭时删除架构。

    您如何停止您的应用程序?我猜你在停止应用程序时并没有关闭SessionFactory

    也许您希望将 /shutdown 端点添加到您指定的 spring-boot 应用程序 here 以正常关闭您的应用程序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 2010-10-31
      • 2013-08-06
      • 2018-11-10
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多