flyway Usage Guide

Keywords: PHP Spring Database SQL Attribute

Explain

During this period of time, the project changes a lot, often modify the table structure, and then all the environment databases are modified, accidentally forgot, and found that the table structure has not been updated until there is a problem; then look for the database version control tool; finally determine flyway.

flyway description

Official address: https://flywaydb.org

According to the instructions on the official website:

Version control for your database. Robust schema evolution across all your environments.With ease, pleasure and plain SQL.

The schematic diagram of the official website: https://flywaydb.org/getstarted/how

Script file name definition

$PREFIX$VERSION__$REMARK.$SUBFIX

Common formats are as follows: $preifx denotes a prefix, which can be specified in the configuration by default to V
$version represents the version, which can be separated by. or separated in the middle sheet, and converted to. saved in the version field of the schema_history table when parsing.
$remark represents a comment, which is parsed and written into the description field.
subfix represents the suffix and can be specified in the configuration, defaulting to. sql.
Use to separate version from description.


Integration with spring boot

  • Find the plug-in section in the official website document:
  • Find spring boot in the link
  • Select the configuration properties in the spring boot plug-in and integration page
  • Configurable properties

At this point, the step of integration with spring boot is to add a pom dependency and configure the corresponding attributes.

Introducing maven dependencies

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

Increase the appropriate configuration

# Explain that in spring boot 1.x, the attribute prefix is flyway, and in spring boot 2.x, spring.flyway. It is necessary to distinguish different versions.
flyway:
  # When there is data in the database in the new environment and there is no flyway_schema_history table, whether to perform migration operation or not, if set to false, it will report an error at startup and stop migration; if true, it will generate history table and complete all migration, according to the actual situation;
  baseline-on-migrate: false
  #  The default tag for execution-time tags is Flyway Baseline >.
  baseline-description: <<Flyway Baseline>>
  #  Whether Flway is enabled or not
  enabled: true
#  Check if the path of the migration script exists and throw an exception if it does not exist
  check-location: true
#  Script location
  locations: classpath:db/migration
#  When migrating, if the script is checked, assuming that V1.0_ initial. sql has been migrated, and if the script is checked for changes at the next startup, an exception is thrown.
  validate-on-migrate: true

Specifically, if the non-empty database is migrated, the Flway_schema_history table is manually constructed in the target database and the initialized script record is manually written, so that the Flway can skip the initial verification, and the subsequent version can be guaranteed to be unified.

Verification

The following log information indicates the success of the validation.


flyway automatic configuration principle in spring boot


spring boot automation configuration, which has built-in flyway function;


The core of spring boot automation configuration is several annotations of Conditional. According to the annotations, the following requirements need to be met:

  1. There are Flyway classes;
  2. There are classes of Datasource;
  3. There are configuration attributes starting from flyway and the attribute value of enable is true.
//Examples of generating Flyway
        @Bean
        @ConfigurationProperties(prefix = "flyway")
        public Flyway flyway() {
            Flyway flyway = new SpringBootFlyway();
            if (this.properties.isCreateDataSource()) {
                flyway.setDataSource(this.properties.getUrl(), this.properties.getUser(),
                        this.properties.getPassword(),
                        this.properties.getInitSqls().toArray(new String[0]));
            }
            else if (this.flywayDataSource != null) {
                flyway.setDataSource(this.flywayDataSource);
            }
            else {
                flyway.setDataSource(this.dataSource);
            }
            flyway.setLocations(this.properties.getLocations().toArray(new String[0]));
            return flyway;
        }
//Examples of generating Flyway Migration Initializer
        @Bean
        @ConditionalOnMissingBean
        public FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
            return new FlywayMigrationInitializer(flyway, this.migrationStrategy);
        }

//This class implements the Initalizing Bean interface and can perform some operations after dependency injection is completed.
public class FlywayMigrationInitializer implements InitializingBean, Ordered

//Delegate the flyway object to complete the database migration command. Here, the flyway operation in spring boot is completed.
@Override
    public void afterPropertiesSet() throws Exception {
        if (this.migrationStrategy != null) {
            this.migrationStrategy.migrate(this.flyway);
        }
        else {
            this.flyway.migrate();
        }
    }

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

Posted by spetstnelis on Tue, 02 Jul 2019 10:47:59 -0700