ããã¯ããªã«ãããããŠæžãããã®ïŒ
Flywayã®ãã€ã°ã¬ãŒã·ã§ã³ãã©ã管çããã®ãããã®ããªãšæ©ãã ããšããã£ãŠãã¡ãã£ãšãŸãšããŠãããããªãšã
æ¹éïŒ
Gitã䜿ãããã©ã³ãã§éçº â ããŒãžããããšãã£ããããŒãçµãã§ããããŒã éçºãèæ¯ã«ããŠããŸãã
ããããæãã§èããŸããã
- ãã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ã®ããŒãžã§ã³ã¯æ¥ä»ãããŒã¹ã«ãã
- ç°å¢ããšã«ãã£ã¬ã¯ããªãåãã
- DDLïŒDCLãïŒïŒãšããŒã¿ïŒDMLïŒã§ãã£ã¬ã¯ããªãåãã
- ïŒæ°ã«ãªããªãïŒOut Of Orderãèš±å¯ãã
ã¡ãã£ãšãã€æžããŠãããŸãã
åŒçšããŠããFlywayã®ããã¥ã¡ã³ãã¯ãFlyway 10.17.2æç¹ã®æ å ±ã§åç §ããŠããŸãã
ãã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ã®ããŒãžã§ã³ã¯æ¥ä»ãããŒã¹ã«ãã
Flywayã®ããã¥ã¡ã³ãã§ããã€ã°ã¬ãŒã·ã§ã³ã®ããŒãžãèŠããšããŒãžã§ãã³ã°ããããã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«åã®äŸã§æåã«
V2__Add_new_table.sql
ãšãã£ãäŸãç®ã«å
¥ãã®ã§ãåçŽå¢å ãªæŽæ°çãªãã®ãæ³åããŠããŸããŸãã
Migrations / Versioned Migrations
ã§ãããå®éã«ã¯ãã£ãšæè»ã«ããŒãžã§ã³ãä»äžã§ããŸããäŸãæžãããŠããŸãã
- 1
- 001
- 5.2
- 1.2.3.4.5.6.7.8.9
- 205.68
- 20130115113556
- 2013.1.15.11.35.56
- 2013.01.15.11.35.56
åçŽãªå¢å ãªæŽæ°ïŒãããã¯Vx.y.zã®ãããªãã®ïŒãšããããŒãžã§ãã³ã°ãæ¡çšãããšãè€æ°äººã§éçºããŠããæã«ã¯ããŒãžã§ã³ã®æ¡çªã§
æ©ã¿ãŸããããã©ã³ãã®ããŒãžã®ã¿ã€ãã³ã°ã§ããŒãžã§ã³ã®é åºãå
¥ãæ¿ãã£ããããŠåŸ®åŠãªããšã«ãªãã®ã§ããããªãæ¥ä»ãããŒã¹ã«ãã
ããŒãžã§ãã³ã°ã«ããæ¹ãããã®ããªãšæããŸãã
date
ã³ãã³ãã§æ¡çªãããããã§ãããã®ã§ã¯ãªãã§ããããïŒ
$ date '+%Y%m%d.%H%M%S' 20240828.115208
ãã¡ãããããŒãžã§ã³ã®åŸã«æå³ãããããããååãã€ããããšã¯å¿ èŠã§ããã
æ¥ä»ãããŒã¹ã«ããããŒãžã§ãã³ã°ã䜿ã£ãŠããçµå±ããŒãžããã¿ã€ãã³ã°ã§é çªãå
¥ãæ¿ããåŸããããŒãžããããã€ã°ã¬ãŒã·ã§ã³ã®
ããŒãžã§ã³ãå€ãããšã©ãŒã«ãªãã®ã§ã¯ïŒãšãã話ã«ã€ããŠã¯Out Of Orderã§å¯Ÿå¿ããããšãã§ããŸãã
ç°å¢ããšã«ãã£ã¬ã¯ããªãåãã
Flywayã¯ã¯ã©ã€ã¢ã³ãã«ãããŸãããããã©ã«ãã§classpath:db/migration
ïŒJava APIãMavenãGradleã®å ŽåïŒãŸãã¯
filesystem:sql
ïŒCLIã®å ŽåïŒé
äžã«ãããã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ãåç
§ããŸãã
Locations - Flyway - Product Documentation
ããããã€ãé©çšãããã€ã°ã¬ãŒã·ã§ã³ãå«ãããã£ã¬ã¯ããªãšãç°å¢å¥ã®ãã£ã¬ã¯ããªã«åããŠç®¡çãããšããã®ããªãšæããŸãã
ããšãã°ãããªæãã§ããã
db âââ migration âââ common â  âââ ....sql â  âââ ....sql âââ development â  âââ ....sql â  âââ ....sql âââ production â  âââ ....sql â  âââ ....sql âââ staging   âââ ....sql   âââ ....sql
ãã€ãé©çšãããã€ã°ã¬ãŒã·ã§ã³ãå«ãã ãã®ãcommon
ãšãããšããããã¯ã·ã§ã³ç°å¢åãã«ã¯ä»¥äžã®2ã€ããã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ã®
é
眮å
ãšããŠæå®ããã€ã¡ãŒãžã§ãã
classpath:db/migration/common
classpath:db/migration/production
ãã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ã®çœ®ãå Žæã¯åãããŸããããã£ã¬ã¯ããªã®å
容ããŸãšããŠé©çšé ã決ãŸãã®ã§ãµã€ãã«ããŒãžã§ã³ãã€ããŠããã°
OKã§ãã
DDLïŒDCLãïŒïŒãšããŒã¿ïŒDMLïŒã§ãã£ã¬ã¯ããªãåãã
ç°å¢å¥ã«ãã£ã¬ã¯ããªãåããå Žåããã€ã°ã¬ãŒã·ã§ã³ã®å
容ãDDLãªã®ãDMLãªã®ãã§ãã£ã¬ã¯ããªãåããæ¹ãåŸã
ã«ããããããã®ããªãš
æããŸããŠã
ã€ã¡ãŒãžçã«ã¯ãããªæãã§ãã
âââ db âââ migration âââ data â  âââ common â  â  âââ ....sql â  â  âââ ....sql â  âââ development â  â  âââ ....sql â  â  âââ ....sql â  âââ production â  â  âââ ....sql â  â  âââ ....sql â  âââ staging â  âââ ....sql â  âââ ....sql âââ ddl âââ common â  âââ ....sql â  âââ ....sql âââ development â  âââ ....sql â  âââ ....sql âââ production â  âââ ....sql â  âââ ....sql âââ staging âââ xxx.sql âââ xxx.sql
DDLãããŸãç°å¢å¥ã«å€ããããšã¯ãªããããªæ°ã¯ããŸãããDCLïŒDDLã§ã¯ãªãã§ããïŒã®ãããªã±ãŒã¹ãèãããšåããŠãããæ¹ã
ããã®ãããããªãããããã§ãã
DMLã¯data
ã«çœ®ããŸãããã¡ãã¯ç»é²ããããŒã¿ãªã©ã«ãªãã®ã§ãç°å¢å¥ã«åããããªããšæããŸãã
䜿ãæ¹ã¯ç°å¢å¥ã«ãã£ã¬ã¯ããªãåããå Žåã®æ¡åŒµã®ãããªãã®ãªã®ã§ããããã¯ã·ã§ã³ç°å¢åãã®å Žåã¯ä»¥äžã®4ã€ã
ãã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ã®é
眮å
ãšããŠæå®ããã€ã¡ãŒãžã§ãã
classpath:db/migration/ddl/common
classpath:db/migration/ddl/production
classpath:db/migration/data/common
classpath:db/migration/data/production
DDLã¯å šç°å¢ã§åããªããããããã®ã§ãããããã§ããã
âââ db âââ migration âââ data â  âââ common â  â  âââ ....sql â  â  âââ ....sql â  âââ development â  â  âââ ....sql â  â  âââ ....sql â  âââ production â  â  âââ ....sql â  â  âââ ....sql â  âââ staging â  âââ ....sql â  âââ ....sql âââ ddl   âââ ....sql   âââ ....sql
æå®ã¯ãã¡ãã
classpath:db/migration/ddl
classpath:db/migration/data/common
classpath:db/migration/data/production
ãããã¯ã·ã§ã³çšã®ããŒã¿ã¯GitãªããžããªãŒåŽã«ã¯å«ããããªããšããå Žåã¯ãå®è¡æã«ããŠã³ãããåæã«ããŠãã®ãã£ã¬ã¯ããªãæå®ã
ã§ããããã
ïŒæ°ã«ãªããªãïŒOut Of Orderãèš±å¯ãã
ããŒãžã§ã³ã®æã«å°ã觊ããŸããããFlywayã¯ãã€ã°ã¬ãŒã·ã§ã³ãé©çšããåŸã«ææ°ã®ããŒãžã§ã³ãããå€ãããŒãžã§ã³ã®ãã€ã°ã¬ãŒã·ã§ã³
ãã¡ã€ã«ãè¿œå ãããšããã©ã«ãã§ã¯ãšã©ãŒã«ãªããŸãã
ããã¯ããŒã éçºãããŠããæã«ããã€ã°ã¬ãŒã·ã§ã³ãå«ãä¿®æ£ãããŒãžããé çªãå ¥ãæ¿ãã£ããããæãªã©ã«çºçããŸãã
ããšãã°20240820æ¥ä»ã®ãã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ãå«ãä¿®æ£ãšã20240821æ¥ä»ã®ãã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ãå«ãä¿®æ£ã䞊è¡ããŠ
è¡ã£ãŠãããšããŸãããã®æãããŒãžã§ã³ãæ°ãã20240821æ¥ä»ã®ãã€ã°ã¬ãŒã·ã§ã³ãå
ã«ããŒãžã»é©çšãããŠããŸããšãåŸããåã蟌ãŸãã
20240820æ¥ä»ã®ãã€ã°ã¬ãŒã·ã§ã³ã¯é©çšæã«ãšã©ãŒã«ãªããŸãã
ãã®æåãå€æŽããã®ãOut Of Orderã§ãã
Out Of Order - Flyway - Product Documentation
䞊è¡ããŠéçºããŠããæã®ãã€ã°ã¬ãŒã·ã§ã³ã®å
容ã«äŸåé¢ä¿ããªãããšãåæã§ãããOut Of Orderãtrue
ã«ãããšå€ãããŒãžã§ã³ã®
ãã€ã°ã¬ãŒã·ã§ã³ãè¿œå ãããŠãé©çšããŠãããŸãã
ãšããããã§
ãããŸã§ãããã䞊ã¹ãŠã¿ãŸããããç°¡åã«ãµã³ãã«ãäœã£ãŠããããšæããŸãã
ãµã³ãã«ã¯Spring Bootã§äœãããšã«ããŠããã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ã®çœ®ãå Žæã¯ãã®æ§é ã«ããããšã«ããŸãã
âââ src âââ main âââ resources âââ db âââ migration âââ data â  âââ common â  â  âââ ....sql â  â  âââ ....sql â  âââ development â  â  âââ ....sql â  â  âââ ....sql â  âââ production â  â  âââ ....sql â  â  âââ ....sql â  âââ staging â  âââ ....sql â  âââ ....sql âââ ddl âââ ....sql âââ ....sql
ç°å¢
ä»åã®ç°å¢ã¯ãã¡ãã
$ java --version openjdk 21.0.4 2024-07-16 OpenJDK Runtime Environment (build 21.0.4+7-Ubuntu-1ubuntu222.04) OpenJDK 64-Bit Server VM (build 21.0.4+7-Ubuntu-1ubuntu222.04, mixed mode, sharing) $ mvn --version Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937) Maven home: $HOME/.sdkman/candidates/maven/current Java version: 21.0.4, vendor: Ubuntu, runtime: /usr/lib/jvm/java-21-openjdk-amd64 Default locale: ja_JP, platform encoding: UTF-8 OS name: "linux", version: "5.15.0-119-generic", arch: "amd64", family: "unix"
ããŒããŒã¹ã«ã¯MySQLã䜿çšããŸããMySQLãµãŒããŒã¯172.17.0.2ã§åäœããŠãããã®ãšããŸãã
MySQL localhost:3306 ssl practice SQL > select version(); +-----------+ | version() | +-----------+ | 8.4.2 | +-----------+ 1 row in set (0.0003 sec)
è€æ°ç°å¢ãæ±ããµã³ãã«ãäœãããšæãã®ã§ãããããã«ã€ããŠã¯è€æ°ã®ããŒã¿ããŒã¹ãŸã§ã¯çšæãããããŒãã«ãdropããŠç°å¢ã
ãªã»ããããªããè©ŠããŠããããšæããŸãã
Spring Bootãããžã§ã¯ããäœæãã
ããã§ã¯ãFlywayã䜿ãSpring Bootãããžã§ã¯ããäœæããŸãã
Flywayãå®è¡ã§ããã°ããã®ã§ãäŸåé¢ä¿ã«ã¯jdbc
ãšflyway
ãmysql
ãå«ããŸãã
$ curl -s https://start.spring.io/starter.tgz \ -d bootVersion=3.3.3 \ -d javaVersion=21 \ -d type=maven-project \ -d name=flyway-migrations \ -d groupId=org.littlewings \ -d artifactId=flyway-migrations \ -d version=0.0.1-SNAPSHOT \ -d packageName=org.littlewings.spring.flyway \ -d dependencies=jdbc,flyway,mysql \ -d baseDir=flyway-migrations | tar zxvf -
çæããããããžã§ã¯ãå ã«ç§»åã
$ cd flyway-migrations
äŸåé¢ä¿ãªã©ã¯ãã®ããã«ãªã£ãŠããŸãã
<properties> <java.version>21</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-mysql</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
çæããããã£ã¬ã¯ããªããã³ãã¡ã€ã«ãFlywayçšã®ãã£ã¬ã¯ããªãæåãããããŸããã
$ tree -a . âââ .gitignore âââ .mvn â  âââ wrapper â  âââ maven-wrapper.properties âââ HELP.md âââ mvnw âââ mvnw.cmd âââ pom.xml âââ src âââ main â  âââ java â  â  âââ org â  â  âââ littlewings â  â  âââ spring â  â  âââ flyway â  â  âââ FlywayMigrationsApplication.java â  âââ resources â  âââ application.properties â  âââ db â  âââ migration âââ test âââ java âââ org âââ littlewings âââ spring âââ flyway âââ FlywayMigrationsApplicationTests.java 18 directories, 9 files
èªåçæãããJavaã®ãœãŒã¹ã³ãŒãã¯åé€ããŠãããŸãã
$ rm src/main/java/org/littlewings/spring/flyway/FlywayMigrationsApplication.java src/test/java/org/littlewings/spring/flyway/FlywayMigrationsApplicationTests.java
main
ã¡ãœãããæã£ãã¯ã©ã¹ã®äœæã
src/main/java/org/littlewings/spring/flyway/App.java
package org.littlewings.spring.flyway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String... args) { SpringApplication.run(App.class, args); } }
application.properties
ã«ã¯ãã²ãšãŸãããŒã¿ããŒã¹æ¥ç¶ã«é¢ããæ
å ±ãå®çŸ©ããŠãããŸãã
src/main/resources/application.properties
spring.application.name=flyway-migrations spring.datasource.url=jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin spring.datasource.username=kazuhira spring.datasource.password=password
Flywayã®èšå®ãšãã€ã°ã¬ãŒã·ã§ã³ã®äœæ
ã§ã¯ãFlywayã«é¢ããèšå®ãšãã€ã°ã¬ãŒã·ã§ã³ã®äœæãè¡ã£ãŠãããŸãããã
ãŸãã¯ãã£ã¬ã¯ããªã®äœæã
$ mkdir src/main/resources/db/migration/{ddl,data} $ mkdir src/main/resources/db/migration/data/{common,development,production}
ç°å¢æ°ã¯2ã€ã«ããŠãããŸãããã
ãŸãã¯ããçšæããŸããã
$ tree src/main/resources/db/migration src/main/resources/db/migration âââ data â  âââ common â  â  âââ V20240825.150000__add_people_data.sql â  âââ development â  â  âââ V20240825.180000__add_people_data.sql â  âââ production â  âââ V20240825.210000__add_people_data.sql âââ ddl âââ V20240825.120000__create_person_table.sql âââ V20240827.150000__create_book_table.sql 5 directories, 5 files
DDLã¯äººãšæžç±ã
src/main/resources/db/migration/ddl/V20240825.120000__create_person_table.sql
create table person( id int, last_name varchar(20), first_name varchar(20), age int primary key(id) );
src/main/resources/db/migration/ddl/V20240827.150000__create_book_table.sql
create table book ( isbn varchar(14), title varchar(200), price int, primary key(isbn) );
ããŒãžã§ã³ã¯å°ãé£ã°ããŠãããŸãã
ããŒã¿ã¯ãå ±éã«ç£¯é家ã®äººãã¡ïŒèŠªé€ãïŒã
src/main/resources/db/migration/data/common/V20240825.150000__add_people_data.sql
insert into person(id, last_name, first_name, age) values(1, 'ãã°ç°', 'ãµã¶ãš', 24); insert into person(id, last_name, first_name, age) values(2, 'ãã°ç°', 'ãã¹ãª', 28); insert into person(id, last_name, first_name, age) values(3, '磯é', 'ã«ããª', 11); insert into person(id, last_name, first_name, age) values(4, '磯é', 'ã¯ã«ã¡', 9); insert into person(id, last_name, first_name, age) values(5, 'ãã°ç°', 'ã¿ã©ãª', 3);
development
ã«æ³¢é家ã
src/main/resources/db/migration/data/development/V20240825.180000__add_people_data.sql
insert into person(id, last_name, first_name, age) values(6, 'æ³¢é', 'ããªã¹ã±', 24); insert into person(id, last_name, first_name, age) values(7, 'æ³¢é', 'ã¿ã€ã³', 22); insert into person(id, last_name, first_name, age) values(8, 'æ³¢é', 'ã€ã¯ã©', 1);
production
ã ãšç£¯é家ãå
šå¡æããŸãã
src/main/resources/db/migration/data/production/V20240825.210000__add_people_data.sql
insert into person(id, last_name, first_name, age) values(6, '磯é', '波平', 54); insert into person(id, last_name, first_name, age) values(7, '磯é', 'ãã', 50);
ããŒã¿ã®è¯ãæªãã«ã€ããŠã¯çœ®ããŠãããŸãâŠã
ããã«åãããŠãFlywayã®èšå®ãè¡ããŸãã
Common Application Properties / Data Migration Properties
ãããªæãã«ãªããŸããã
src/main/resources/application.properties
spring.application.name=flyway-migrations spring.datasource.url=jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin spring.datasource.username=kazuhira spring.datasource.password=password ## Flyway spring.flyway.fail-on-missing-locations=true ### ãã€ã°ã¬ãŒã·ã§ã³ã®é 眮å spring.flyway.locations=classpath:db/migration/ddl,classpath:db/migration/data/common,classpath:db/migration/data/development ### Out Of Order spring.flyway.out-of-order=false ### ãã€ã°ã¬ãŒã·ã§ã³ã®åœåèŠåã®ç¢ºèª spring.flyway.validate-migration-naming=true
ã²ãšãŸããã€ã³ãã¯ããã ãã§ããã
### ãã€ã°ã¬ãŒã·ã§ã³ã®é 眮å spring.flyway.locations=classpath:db/migration/ddl,classpath:db/migration/data/common,classpath:db/migration/data/development
以äžã®3ã€ããã€ã°ã¬ãŒã·ã§ã³ã®é 眮å ã«æå®ããŠããŸãã
classpath:db/migration/ddl
classpath:db/migration/data/common
classpath:db/migration/data/development
ã§ã¯ãããã±ãŒãžã³ã°ããŠå®è¡ã
$ mvn package $ java -jar target/flyway-migrations-0.0.1-SNAPSHOT.jar
Flywayãå®è¡ãããŸããã
2024-08-28T15:45:12.997+09:00 INFO 26843 --- [flyway-migrations] [ main] org.flywaydb.core.FlywayExecutor : Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.4) 2024-08-28T15:45:13.145+09:00 WARN 26843 --- [flyway-migrations] [ main] o.f.c.internal.database.base.Database : Flyway upgrade recommended: MySQL 8.4 is newer than this version of Flyway and support has not been tested. The latest supported version of MySQL is 8.1. 2024-08-28T15:45:13.199+09:00 INFO 26843 --- [flyway-migrations] [ main] o.f.c.i.s.JdbcTableSchemaHistory : Schema history table `practice`.`flyway_schema_history` does not exist yet 2024-08-28T15:45:13.206+09:00 INFO 26843 --- [flyway-migrations] [ main] o.f.core.internal.command.DbValidate : Successfully validated 4 migrations (execution time 00:00.044s) 2024-08-28T15:45:13.265+09:00 INFO 26843 --- [flyway-migrations] [ main] o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table `practice`.`flyway_schema_history` ... 2024-08-28T15:45:13.540+09:00 INFO 26843 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Current version of schema `practice`: << Empty Schema >> 2024-08-28T15:45:13.552+09:00 INFO 26843 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Migrating schema `practice` to version "20240825.120000 - create person table" 2024-08-28T15:45:13.738+09:00 INFO 26843 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Migrating schema `practice` to version "20240825.150000 - add people data" 2024-08-28T15:45:13.812+09:00 INFO 26843 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Migrating schema `practice` to version "20240825.180000 - add people data" 2024-08-28T15:45:13.855+09:00 INFO 26843 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Migrating schema `practice` to version "20240827.150000 - create book table" 2024-08-28T15:45:14.012+09:00 INFO 26843 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Successfully applied 4 migrations to schema `practice`, now at version v20240827.150000 (execution time 00:00.254s)
ããŒãã«ã®ç¶æ ã確èªããŠã¿ãŸãã
MySQL localhost:3306 ssl practice SQL > show tables; +-----------------------+ | Tables_in_practice | +-----------------------+ | book | | flyway_schema_history | | person | +-----------------------+ 3 rows in set (0.0014 sec)
é©çšããããã€ã°ã¬ãŒã·ã§ã³ã®äžèŠ§ã
MySQL localhost:3306 ssl practice SQL > select * from flyway_schema_history; +----------------+-----------------+---------------------+------+-------------------------------------------+------------+--------------+---------------------+----------------+---------+ | installed_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success | +----------------+-----------------+---------------------+------+-------------------------------------------+------------+--------------+---------------------+----------------+---------+ | 1 | 20240825.120000 | create person table | SQL | V20240825.120000__create_person_table.sql | -123870336 | kazuhira | 2024-08-28 06:45:13 | 114 | 1 | | 2 | 20240825.150000 | add people data | SQL | V20240825.150000__add_people_data.sql | -181951584 | kazuhira | 2024-08-28 06:45:13 | 11 | 1 | | 3 | 20240825.180000 | add people data | SQL | V20240825.180000__add_people_data.sql | -745607530 | kazuhira | 2024-08-28 06:45:13 | 32 | 1 | | 4 | 20240827.150000 | create book table | SQL | V20240827.150000__create_book_table.sql | 1689297802 | kazuhira | 2024-08-28 06:45:13 | 97 | 1 | +----------------+-----------------+---------------------+------+-------------------------------------------+------------+--------------+---------------------+----------------+---------+ 4 rows in set (0.0005 sec)
DDLãšcommonãšdevelopmentã®ããŒã¿ã®ã¿ãé©çšãããŠããã¯ãã§ãã
ããŒã¿ã§ç¢ºèªããŠã¿ãŸãããã
MySQL localhost:3306 ssl practice SQL > select * from person; +----+-----------+------------+-----+ | id | last_name | first_name | age | +----+-----------+------------+-----+ | 1 | ãã°ç° | ãµã¶ãš | 24 | | 2 | ãã°ç° | ãã¹ãª | 28 | | 3 | 磯é | ã«ã㪠| 11 | | 4 | 磯é | ã¯ã«ã¡ | 9 | | 5 | ãã°ç° | ã¿ã©ãª | 3 | | 6 | æ³¢é | ããªã¹ã± | 24 | | 7 | æ³¢é | ã¿ã€ã³ | 22 | | 8 | æ³¢é | ã€ã¯ã© | 1 | +----+-----------+------------+-----+ 8 rows in set (0.0005 sec)
OKã§ããã
ããã²ãšã€ã®æ¹ã«ã¯ããŒã¿ã¯å ¥ããŠããŸããã
MySQL localhost:3306 ssl practice SQL > select * from book; Empty set (0.0010 sec)
ããã§ããã¹ãŠã®ããŒãã«ãdropããŸãã
MySQL localhost:3306 ssl practice SQL > drop table flyway_schema_history;drop table person;drop table book;
ãã€ã°ã¬ãŒã·ã§ã³ã®é 眮å ããç°å¢å€æ°ã§åãæ¿ããŠã¿ãŸãããã
$ export SPRING_FLYWAY_LOCATIONS=classpath:db/migration/ddl,classpath:db/migration/data/common,classpath:db/migration/data/production
ã€ãŸãããã€ã°ã¬ãŒã·ã§ã³ã®é 眮å ã¯ãããªã£ãŠããŸãã
classpath:db/migration/ddl
classpath:db/migration/data/common
classpath:db/migration/data/production
development
ãproduction
ã«å€ãã£ãããã§ããã
å®è¡ããŸãã
$ java -jar target/flyway-migrations-0.0.1-SNAPSHOT.jar
å®è¡æã®ãã°ã
2024-08-28T15:49:09.550+09:00 INFO 27033 --- [flyway-migrations] [ main] org.flywaydb.core.FlywayExecutor : Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.4) 2024-08-28T15:49:09.653+09:00 WARN 27033 --- [flyway-migrations] [ main] o.f.c.internal.database.base.Database : Flyway upgrade recommended: MySQL 8.4 is newer than this version of Flyway and support has not been tested. The latest supported version of MySQL is 8.1. 2024-08-28T15:49:09.696+09:00 INFO 27033 --- [flyway-migrations] [ main] o.f.c.i.s.JdbcTableSchemaHistory : Schema history table `practice`.`flyway_schema_history` does not exist yet 2024-08-28T15:49:09.702+09:00 INFO 27033 --- [flyway-migrations] [ main] o.f.core.internal.command.DbValidate : Successfully validated 4 migrations (execution time 00:00.032s) 2024-08-28T15:49:09.731+09:00 INFO 27033 --- [flyway-migrations] [ main] o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table `practice`.`flyway_schema_history` ... 2024-08-28T15:49:10.004+09:00 INFO 27033 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Current version of schema `practice`: << Empty Schema >> 2024-08-28T15:49:10.018+09:00 INFO 27033 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Migrating schema `practice` to version "20240825.120000 - create person table" 2024-08-28T15:49:10.173+09:00 INFO 27033 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Migrating schema `practice` to version "20240825.150000 - add people data" 2024-08-28T15:49:10.224+09:00 INFO 27033 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Migrating schema `practice` to version "20240825.210000 - add people data" 2024-08-28T15:49:10.341+09:00 INFO 27033 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Migrating schema `practice` to version "20240827.150000 - create book table" 2024-08-28T15:49:10.542+09:00 INFO 27033 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Successfully applied 4 migrations to schema `practice`, now at version v20240827.150000 (execution time 00:00.253s)
é©çšããããã€ã°ã¬ãŒã·ã§ã³ã®äžèŠ§ã
MySQL localhost:3306 ssl practice SQL > select * from flyway_schema_history; +----------------+-----------------+---------------------+------+-------------------------------------------+-------------+--------------+---------------------+----------------+---------+ | installed_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success | +----------------+-----------------+---------------------+------+-------------------------------------------+-------------+--------------+---------------------+----------------+---------+ | 1 | 20240825.120000 | create person table | SQL | V20240825.120000__create_person_table.sql | -123870336 | kazuhira | 2024-08-28 06:49:10 | 106 | 1 | | 2 | 20240825.150000 | add people data | SQL | V20240825.150000__add_people_data.sql | -181951584 | kazuhira | 2024-08-28 06:49:10 | 11 | 1 | | 3 | 20240825.210000 | add people data | SQL | V20240825.210000__add_people_data.sql | -1450894877 | kazuhira | 2024-08-28 06:49:10 | 7 | 1 | | 4 | 20240827.150000 | create book table | SQL | V20240827.150000__create_book_table.sql | 1689297802 | kazuhira | 2024-08-28 06:49:10 | 129 | 1 | +----------------+-----------------+---------------------+------+-------------------------------------------+-------------+--------------+---------------------+----------------+---------+ 4 rows in set (0.0005 sec)
ããŒã¿ã確èªããŠã¿ãŸãã
MySQL localhost:3306 ssl practice SQL > select * from person; +----+-----------+------------+-----+ | id | last_name | first_name | age | +----+-----------+------------+-----+ | 1 | ãã°ç° | ãµã¶ãš | 24 | | 2 | ãã°ç° | ãã¹ãª | 28 | | 3 | 磯é | ã«ã㪠| 11 | | 4 | 磯é | ã¯ã«ã¡ | 9 | | 5 | ãã°ç° | ã¿ã©ãª | 3 | | 6 | 磯é | 波平 | 54 | | 7 | 磯é | ãã | 50 | +----+-----------+------------+-----+ 7 rows in set (0.0004 sec)
development
çšã®ããŒã¿ã¯ãªããªããproduction
ã®ããŒã¿ãè¿œå ãããŸããã
OKã§ããã
ãšããã§ãããèŠããšFlywayã¯MySQL 8.4ããŸã ãµããŒãããŠããªãã¿ããã§ããâŠã
2024-08-28T15:45:13.145+09:00 WARN 26843 --- [flyway-migrations] [ main] o.f.c.internal.database.base.Database : Flyway upgrade recommended: MySQL 8.4 is newer than this version of Flyway and support has not been tested. The latest supported version of MySQL is 8.1.
確èªæç¹ã ãšã確ãã«ããã§ããã
Verified Versions: 5.7, 8.0, 8.1
MySQL - Flyway - Product Documentation
Out Of Orderãè©Šã
æåŸã«Out Of Orderãè©ŠããŠã¿ãŸãããã
ãã€ã°ã¬ãŒã·ã§ã³ã¯ãdevelopment
ã§é©çšæžã¿ãšããŸãã
ã€ãŸãããã®ç¶æ ã§ããã
MySQL localhost:3306 ssl practice SQL > select * from flyway_schema_history; +----------------+-----------------+---------------------+------+-------------------------------------------+------------+--------------+---------------------+----------------+---------+ | installed_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success | +----------------+-----------------+---------------------+------+-------------------------------------------+------------+--------------+---------------------+----------------+---------+ | 1 | 20240825.120000 | create person table | SQL | V20240825.120000__create_person_table.sql | -123870336 | kazuhira | 2024-08-28 06:53:31 | 112 | 1 | | 2 | 20240825.150000 | add people data | SQL | V20240825.150000__add_people_data.sql | -181951584 | kazuhira | 2024-08-28 06:53:31 | 9 | 1 | | 3 | 20240825.180000 | add people data | SQL | V20240825.180000__add_people_data.sql | -745607530 | kazuhira | 2024-08-28 06:53:31 | 6 | 1 | | 4 | 20240827.150000 | create book table | SQL | V20240827.150000__create_book_table.sql | 1689297802 | kazuhira | 2024-08-28 06:53:31 | 82 | 1 | +----------------+-----------------+---------------------+------+-------------------------------------------+------------+--------------+---------------------+----------------+---------+ 4 rows in set (0.0004 sec) MySQL localhost:3306 ssl practice SQL > select * from person; +----+-----------+------------+-----+ | id | last_name | first_name | age | +----+-----------+------------+-----+ | 1 | ãã°ç° | ãµã¶ãš | 24 | | 2 | ãã°ç° | ãã¹ãª | 28 | | 3 | 磯é | ã«ã㪠| 11 | | 4 | 磯é | ã¯ã«ã¡ | 9 | | 5 | ãã°ç° | ã¿ã©ãª | 3 | | 6 | æ³¢é | ããªã¹ã± | 24 | | 7 | æ³¢é | ã¿ã€ã³ | 22 | | 8 | æ³¢é | ã€ã¯ã© | 1 | +----+-----------+------------+-----+ 8 rows in set (0.0005 sec)
ããã§æåŸã®ãã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ã®æ¥ä»ã¯20240827ã§ãããã20240826ã®ãã®ãå²ã蟌ãŸããŠã¿ãŸãã
src/main/resources/db/migration/ddl/V20240826.180000__create_category_table.sql
create table category ( id varchar(3), name varchar(20), primary key(id) );
ããã±ãŒãžã³ã°ããŠå®è¡ã
$ mvn package $ java -jar target/flyway-migrations-0.0.1-SNAPSHOT.jar
ãããšãæªé©çšã®ãã€ã°ã¬ãŒã·ã§ã³ãæ€åºããããšããããšã§å€±æããŸãã
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Validate failed: Migrations have failed validation Detected resolved migration not applied to database: 20240826.180000. To ignore this migration, set -ignoreMigrationPatterns='*:ignored'. To allow executing this migration, set -outOfOrder=true. Need more flexibility with validation rules? Learn more: https://rd.gt/3AbJUZE at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1806) ~[spring-beans-6.1.12.jar!/:6.1.12] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600) ~[spring-beans-6.1.12.jar!/:6.1.12] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) ~[spring-beans-6.1.12.jar!/:6.1.12] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337) ~[spring-beans-6.1.12.jar!/:6.1.12] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.1.12.jar!/:6.1.12] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335) ~[spring-beans-6.1.12.jar!/:6.1.12] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.1.12.jar!/:6.1.12] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313) ~[spring-beans-6.1.12.jar!/:6.1.12] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.1.12.jar!/:6.1.12] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975) ~[spring-beans-6.1.12.jar!/:6.1.12] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:971) ~[spring-context-6.1.12.jar!/:6.1.12] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:625) ~[spring-context-6.1.12.jar!/:6.1.12] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.3.3.jar!/:3.3.3] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.3.3.jar!/:3.3.3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) ~[spring-boot-3.3.3.jar!/:3.3.3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363) ~[spring-boot-3.3.3.jar!/:3.3.3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352) ~[spring-boot-3.3.3.jar!/:3.3.3] at org.littlewings.spring.flyway.App.main(App.java:9) ~[!/:0.0.1-SNAPSHOT] at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na] at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:102) ~[flyway-migrations-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:64) ~[flyway-migrations-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.launch.JarLauncher.main(JarLauncher.java:40) ~[flyway-migrations-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] Caused by: org.flywaydb.core.api.exception.FlywayValidateException: Validate failed: Migrations have failed validation Detected resolved migration not applied to database: 20240826.180000. To ignore this migration, set -ignoreMigrationPatterns='*:ignored'. To allow executing this migration, set -outOfOrder=true. Need more flexibility with validation rules? Learn more: https://rd.gt/3AbJUZE at org.flywaydb.core.Flyway.lambda$migrate$0(Flyway.java:160) ~[flyway-core-10.10.0.jar!/:na] at org.flywaydb.core.FlywayExecutor.execute(FlywayExecutor.java:205) ~[flyway-core-10.10.0.jar!/:na] at org.flywaydb.core.Flyway.migrate(Flyway.java:147) ~[flyway-core-10.10.0.jar!/:na] at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) ~[spring-boot-autoconfigure-3.3.3.jar!/:3.3.3] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-6.1.12.jar!/:6.1.12] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1802) ~[spring-beans-6.1.12.jar!/:6.1.12] ... 22 common frames omitted
ãã®éšåã§ããã
Validate failed: Migrations have failed validation
Detected resolved migration not applied to database: 20240826.180000.
ããããã®ãŸãŸé©çšããŠãããããšããå Žåã¯ãã¡ãã»ãŒãžã«æžãããŠããããã«ç¡èŠãããOut Of Orderãtrue
ã«ããŸãã
To ignore this migration, set -ignoreMigrationPatterns='*:ignored'. To allow executing this migration, set -outOfOrder=true.
ä»åã¯Out Of Orderãtrue
ã«ããŸããããapplication.properties
ãããå€æŽããŸãã
### Out Of Order spring.flyway.out-of-order=true
ç°å¢å€æ°ã§æå®ãããããŠãäžæçã«åãæ¿ããŠãããã§ãããã
$ export SPRING_FLYWAY_OUTOFORDER=true
ïŒapplication.properties
ãå€æŽããå Žåã¯ãããã±ãŒãžã³ã°ããŠïŒå®è¡ã
$ mvn package $ java -jar target/flyway-migrations-0.0.1-SNAPSHOT.jar
ãoutOfOrder mode is active.ããšè¡šç€ºããããã€ã°ã¬ãŒã·ã§ã³ãé©çšããŠãããŸãã
2024-08-28T15:59:20.540+09:00 INFO 27655 --- [flyway-migrations] [ main] org.flywaydb.core.FlywayExecutor : Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.4) 2024-08-28T15:59:20.633+09:00 WARN 27655 --- [flyway-migrations] [ main] o.f.c.internal.database.base.Database : Flyway upgrade recommended: MySQL 8.4 is newer than this version of Flyway and support has not been tested. The latest supported version of MySQL is 8.1. 2024-08-28T15:59:20.682+09:00 INFO 27655 --- [flyway-migrations] [ main] o.f.core.internal.command.DbValidate : Successfully validated 5 migrations (execution time 00:00.037s) 2024-08-28T15:59:20.702+09:00 INFO 27655 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Current version of schema `practice`: 20240827.150000 2024-08-28T15:59:20.702+09:00 WARN 27655 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : outOfOrder mode is active. Migration of schema `practice` may not be reproducible. 2024-08-28T15:59:20.724+09:00 INFO 27655 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Migrating schema `practice` to version "20240826.180000 - create category table" [out of order] 2024-08-28T15:59:21.088+09:00 INFO 27655 --- [flyway-migrations] [ main] o.f.core.internal.command.DbMigrate : Successfully applied 1 migration to schema `practice`, now at version v20240826.180000 (execution time 00:00.219s)
確èªããŠã¿ãŸãããã
MySQL localhost:3306 ssl practice SQL > show tables; +-----------------------+ | Tables_in_practice | +-----------------------+ | book | | category | | flyway_schema_history | | person | +-----------------------+ 4 rows in set (0.0013 sec)
ããŒãã«ã¯ã§ããŠããŸãã
é©çšããããã€ã°ã¬ãŒã·ã§ã³ã®äžèŠ§ã
MySQL localhost:3306 ssl practice SQL > select * from flyway_schema_history; +----------------+-----------------+-----------------------+------+---------------------------------------------+------------+--------------+---------------------+----------------+---------+ | installed_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success | +----------------+-----------------+-----------------------+------+---------------------------------------------+------------+--------------+---------------------+----------------+---------+ | 1 | 20240825.120000 | create person table | SQL | V20240825.120000__create_person_table.sql | -123870336 | kazuhira | 2024-08-28 06:53:31 | 112 | 1 | | 2 | 20240825.150000 | add people data | SQL | V20240825.150000__add_people_data.sql | -181951584 | kazuhira | 2024-08-28 06:53:31 | 9 | 1 | | 3 | 20240825.180000 | add people data | SQL | V20240825.180000__add_people_data.sql | -745607530 | kazuhira | 2024-08-28 06:53:31 | 6 | 1 | | 4 | 20240827.150000 | create book table | SQL | V20240827.150000__create_book_table.sql | 1689297802 | kazuhira | 2024-08-28 06:53:31 | 82 | 1 | | 5 | 20240826.180000 | create category table | SQL | V20240826.180000__create_category_table.sql | 1347572117 | kazuhira | 2024-08-28 06:59:20 | 219 | 1 | +----------------+-----------------+-----------------------+------+---------------------------------------------+------------+--------------+---------------------+----------------+---------+ 5 rows in set (0.0005 sec)
å€ãæ¥ä»ã®ãã€ã°ã¬ãŒã·ã§ã³ã®installed_rankãææ°ã«ãªã£ãŠãããã®ã®ãé©çšãããŠããããã§ãã
ããŒãã«ã«ãã¢ã¯ã»ã¹ã§ããŸãã
MySQL localhost:3306 ssl practice SQL > select * from category; Empty set (0.0018 sec)
ãšããããã§ãOut Of Orderã®å¹æã確èªã§ããŸããã
ãããã«
Flywayã®ãã€ã°ã¬ãŒã·ã§ã³ã®ç®¡çãšããããšã§ãããŒãžã§ã³ã®ä»ãæ¹ããã€ã°ã¬ãŒã·ã§ã³ã®é
眮å
ãªã©ãããããæžããŠã¿ãŸããã
ãã®ããæ¹ã§åŸ®åŠã«æããšããã¯ãã£ãããããã®ã§ãããããããèããŠãã®ä»è¿ã劥åç¹ããªãšãæã£ããã
åèã«ãªãã°ãšããããšããããŸãããèªåã¯ãã®ããããããŒã¹ã«èããããšæããŸãã