0

Overview

I'm going to add flyway db migration to an existing project with the following configs:

  • Project type: Spring Boot
  • DB: MariaDB

application-local.yaml (Flyway and datasource configs):

    ...

    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
    ...

Issue:

When the application is stopped, db tables are not dropped. But as I used ddl-auto: create-drop, it's expected tables to be dropped.

Note: I know it seems a bit odd to use Flyway for volatile DB. As there are some configuration classes to insert initial data in start up. If the db is not dropped while the application is stopped, it'll come up with complain [e.g.: unique constraint violations ] in the next start up because the initial values have already inserted.

I'd be grateful if anyone could assist me with their genuine solution.

saeedj
  • 2,179
  • 9
  • 25
  • 38

1 Answers1

0

The documentation for create-drop states:

Drop the schema and recreate it on SessionFactory startup. Additionally, drop the schema on SessionFactory shutdown.

How are you stopping your application? I'm guessing that you aren't shutting down the SessionFactory when you stop the application

Perhaps you'll want to add the /shutdown endpoint to your spring-boot application as specified here to gracefully shutdown your application

lance-java
  • 25,497
  • 4
  • 59
  • 101