已有項目改造——Spring boot集成flyway(粗暴法)

背景

目前項目是spring boot+mysql+maven,由於須要數據庫版本管理,於是集成flywayhtml

目標

  1. 集成flyway
  2. 對於已存在的數據庫結構不影響

步驟

1.在pom.xml中加入依賴

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

2.在項目中,建立放置sql的文件夾

cd src/main/resources
mkdir db/migration

3.導出當前數據庫的DDL和數據文件清空數據庫

導出數據庫java

mysqldump -uroot -p Mydb >Mydb.sql

清空數據庫mysql

drop database Mydb;
create schema Mydb default character set utf8 collate utf8_general_ci;

4.將導出文件放置到db/migration

mv Mydb.sql db/migration/V1__init.sql

5.編譯&&運行項目

#編譯打包
mvn package
#運行項目
java -jar target/myproject-0.0.1-SNAPSHOT.jar

6.運行結果

數據庫和未集成前一致,且多一個schema_version表,該表是flyway進行版本管理的主表spring

注意

這個方法簡單粗暴,用於數據量不大的狀況,若是對於現有的數據庫並不想動能夠用Baseline方案解決,🚩之後有時間寫一下這個方案sql

一些能夠配置的flyway參數

方法一:在application.properties 或者 application.yml 中配置數據庫

# FLYWAY (FlywayProperties)
spring.flyway.baseline-description=<< Flyway Baseline >> # Description to tag an existing schema with when applying a baseline.
spring.flyway.baseline-on-migrate=false # Whether to automatically call baseline when migrating a non-empty schema.
spring.flyway.baseline-version=1 # Version to tag an existing schema with when executing baseline.
spring.flyway.check-location=true # Whether to check that migration scripts location exists.
spring.flyway.clean-disabled=false # Whether to disable cleaning of the database.
spring.flyway.clean-on-validation-error=false # Whether to automatically call clean when a validation error occurs.
spring.flyway.connect-retries=0 # Maximum number of retries when attempting to connect to the database.
spring.flyway.enabled=true # Whether to enable flyway.
spring.flyway.encoding=UTF-8 # Encoding of SQL migrations.
spring.flyway.group=false # Whether to group all pending migrations together in the same transaction when applying them.
spring.flyway.ignore-future-migrations=true # Whether to ignore future migrations when reading the schema history table.
spring.flyway.ignore-ignored-migrations=false # Whether to ignore ignored migrations when reading the schema history table.
spring.flyway.ignore-missing-migrations=false # Whether to ignore missing migrations when reading the schema history table.
spring.flyway.ignore-pending-migrations=false # Whether to ignore pending migrations when reading the schema history table.
spring.flyway.init-sqls= # SQL statements to execute to initialize a connection immediately after obtaining it.
spring.flyway.installed-by= # Username recorded in the schema history table as having applied the migration.
spring.flyway.locations=classpath:db/migration # Locations of migrations scripts. Can contain the special "{vendor}" placeholder to use vendor-specific locations.
spring.flyway.mixed=false # Whether to allow mixing transactional and non-transactional statements within the same migration.
spring.flyway.out-of-order=false # Whether to allow migrations to be run out of order.
spring.flyway.password= # Login password of the database to migrate.
spring.flyway.placeholder-prefix=${ # Prefix of placeholders in migration scripts.
spring.flyway.placeholder-replacement=true # Perform placeholder replacement in migration scripts.
spring.flyway.placeholder-suffix=} # Suffix of placeholders in migration scripts.
spring.flyway.placeholders= # Placeholders and their replacements to apply to sql migration scripts.
spring.flyway.repeatable-sql-migration-prefix=R # File name prefix for repeatable SQL migrations.
spring.flyway.schemas= # Scheme names managed by Flyway (case-sensitive).
spring.flyway.skip-default-callbacks=false # Whether to skip default callbacks. If true, only custom callbacks are used.
spring.flyway.skip-default-resolvers=false # Whether to skip default resolvers. If true, only custom resolvers are used.
spring.flyway.sql-migration-prefix=V # File name prefix for SQL migrations.
spring.flyway.sql-migration-separator=__ # File name separator for SQL migrations.
spring.flyway.sql-migration-suffixes=.sql # File name suffix for SQL migrations.
spring.flyway.table=flyway_schema_history # Name of the schema schema history table that will be used by Flyway.
spring.flyway.target= # Target version up to which migrations should be considered.
spring.flyway.url= # JDBC url of the database to migrate. If not set, the primary configured data source is used.
spring.flyway.user= # Login user of the database to migrate.
spring.flyway.validate-on-migrate=true # Whether to automatically call validate when performing a migration.

方法二:用環境變量配置
springboot

參考資料

Execute Flyway Database Migrations on Startup
spring boot 開箱集成flyway
Existing database setupapp

相關文章
相關標籤/搜索