Introducing Flyway with SpringBoot + Maven + PostgreSQL

Ruby on Rails has a DB migration function by default, but Spring Boot has no choice but to install it yourself. I had a hard time introducing Flyway, a migration tool, so I'll try to put it all together.

Preparation

pom.xml


<dependency>
	<groupId>org.flywaydb</groupId>
	<artifactId>flyway-core</artifactId>
</dependency>

I will write it like this in the dependency hierarchy. Since I want to use the mvn command together, I also specify the following in the plugin.

pom.xml


<build>
         <plugins>
           <plugin>
               <groupId>org.flywaydb</groupId>
               <artifactId>flyway-maven-plugin</artifactId>
               <version>6.0.8</version>
               <configuration>
                   <url>Please specify the URL of the DB?currentSchema=If the schema is specified on the DB side, specify it.</url>
                 <user>Please specify USER of DB.</user>
                 <password></password>
                 <schemas>
                     <schema>ssp_engine</schema>
                 </schemas>
                 <baselineOnMigrate>true</baselineOnMigrate>
                 <baselineVersion>1.0</baselineVersion>
                     <baselineDescription>Initial</baselineDescription>
                 <locations>classpath:/db/migration</locations>
             </configuration>
           </plugin>
         </plugins>
</build>

After that, update the project with Maven and import the jar file.

DB instruction file creation

The file name is strictly determined, so please refer to this. ・ Flyway usage memo

In my case, it looks like this. The hierarchy is

-src/
  -main/
    |-java/
    |  -com/
    |    -example/
    |      -main.java
    -resources/
      -db/
        -migration/
          -V1_0__create_sequence.sql
          -V1_1__create_table.sql
          -V1_2__create_data.sql

For the contents of the file, create a sequence object, create a table, and write the contents.

Command execution

In your terminal, go to the hierarchy where the target file is located and execute the following command to execute it.

Perform migration

mvn flyway:migrate

Delete all objects in the database

mvn flyway:clean

Display information about to what version the migration is running

mvn flyway:info

Check if there is a difference between the version of the database and the prepared SQL file

mvn flyway:validate

Make database version 1

mvn flyway:init

Delete the version of metadata that is in the Failed state

mvn flyway:repair

Impressions introduced

During development in local, after confirming CRUD with a form etc., mvn flyway: clean and mvn flyway: migrate can only be used for reset purposes. I would like to ask if there is a more effective way to use it. ..

Recommended Posts

Introducing Flyway with SpringBoot + Maven + PostgreSQL
Distributed transaction with SpringBoot + PostgreSql + mybatis + NarayanaJTA
Supports multi-port with SpringBoot
Change the port with SpringBoot
Try gRPC with Java, Maven
I tried UPSERT with PostgreSQL.
Easy library introduction with Maven!
Hello World with SpringBoot / Gradle
UnitTest with SpringBoot + JUnit + Mockito
Create a flyway jar with maven and docker build (migrate) with docker-maven-plugin