ããã¯ããªã«ãããããŠæžãããã®ïŒ
以åããFlywayã1床詊ãããšæã£ãŠããã®ã§ãããããããããå æžã«ããšã
Flywayãšã¯
Flywayã¯ãJavaã§å®è£ ãããããŒã¿ããŒã¹ã®ãã€ã°ã¬ãŒã·ã§ã³ããŒã«ã§ãã
GitHub - flyway/flyway: Flyway by Redgate • Database Migrations Made Easy.
Documentation - Flyway by Redgate • Database Migrations Made Easy.
7ã€ã®ã³ãã³ãã«åºã¥ããŠæ§æãããŠããŸãã
ãã€ã°ã¬ãŒã·ã§ã³ã¯ãSQLãJavaã§èšè¿°ããããšãã§ããCLIãJava APIãMavenãGradleã®ãã©ã°ã€ã³ãå©çšããŠ
å®è¡ããããSpring BootãDropwizardã®ãã©ã°ã€ã³ãªã©ããããŸãã
ãµããŒããããŠããããŒã¿ããŒã¹ã¯ã以äžã®ããã§ãã
Flywayã«ã¯2ã€ã®EditionããããCommunity EditionãšTeam EditionããããŸãã
ããããæ©èœå·®ããã£ãŠã7ã€ã®ã³ãã³ãã®ãã¡undo
ã¯Team Editionã®ã¿ã§äœ¿ããŸãã
ããŒã¿ããŒã¹ãã€ã°ã¬ãŒã·ã§ã³ãšFlywayã®ä»çµã¿
Flywayã®ããã¥ã¡ã³ãã«ãã©ãããŠããŒã¿ããŒã¹ãã€ã°ã¬ãŒã·ã§ã³ãå¿ èŠãªã®ããæžãããŠããŸãã
Why database migrations - Flyway by Redgate • Database Migrations Made Easy.
ããŒã¿ããŒã¹ã¯ãæ¬çªç°å¢ã ãã§ã¯ãªããéçºè
å人ã®ç°å¢ãCIç°å¢ããã¹ãç°å¢ãªã©è€æ°ååšããããšãå€ãã§ãããã
ãããã®ããŒã¿ããŒã¹ã«å¯ŸããŠãå€æŽã管çã»åæ ããŠããå¿
èŠããããŸãã
ããããã®ããŒã¿ããŒã¹ãã©ã®ãããªç¶æ
ã«ãªã£ãŠãããïŒå€æŽãè¡ãã¹ã¯ãªããã¯é©çšæžã¿ãïŒãšãã£ãç¶æ
ã管çãããã
æ°ããããŒã¿ããŒã¹ã皌åããç°å¢ãå¢ããæã«èšå®ããæã¯ïŒãšãã£ã課é¡ã¯ãéçºã«ã€ããŸãšã課é¡ã§ãã
ãã€ã°ã¬ãŒã·ã§ã³ããŒã«ã¯ãããã解決ããããã®ãã®ã§ãã
Flywayãã©ã®ããã«åäœãããã¯ããã¡ãã«æžãããŠããŸãã
How Flyway works - Flyway by Redgate • Database Migrations Made Easy.
Flywayã¯ã¹ããŒãã管çããããŒãã«ïŒã¹ããŒãå±¥æŽããŒãã«ïŒãäœæãããã¡ãã§ããŒã¿ããŒã¹ã®ç¶æ
管çã
è¡ãããã§ãã
Flywayã®å®è¡ã®æµãããã£ããæžããšããããªæãã¿ããã§ãã
- ãã€ã°ã¬ãŒã·ã§ã³ã®æ€åº
- å¿ èŠã«å¿ããŠããŒã¿ããŒã¹ã«ãã€ã°ã¬ãŒã·ã§ã³ãåæ
- ã¹ããŒãå±¥æŽããŒãã«ã«èšé²
- 次ã®å®è¡ãž
Flywayã¯ãã¡ã€ã«ã·ã¹ãã ãŸãã¯ã¯ã©ã¹ãã¹ãã¹ãã£ã³ããŠããã€ã°ã¬ãŒã·ã§ã³ã確èªããŸãããã€ã°ã¬ãŒã·ã§ã³ã¯
SQLãŸãã¯Javaã§èšè¿°ãããŠããŸãã
ããã§ããã€ã°ã¬ãŒã·ã§ã³ããšããã®ã¯ãããŒã¿ããŒã¹ã«å¯Ÿããå€æŽã®ããšã§ãããFlywayã§ã¯ããŒã¿ããŒã¹ãžã®å€æŽã
ãã¹ãŠãã€ã°ã¬ãŒã·ã§ã³ãšåŒã¶ããã§ãã
With Flyway all changes to the database are called migrations.
Migrations - Migrations - Flyway by Redgate • Database Migrations Made Easy.
ãã€ã°ã¬ãŒã·ã§ã³ã¯ããŒãžã§ã³çªå·ã«åºã¥ããŠäžŠã³æ¿ããããé çªã«é©çšããŸãã
ãã®çµæã¯ãã¹ããŒãå±¥æŽããŒãã«ã«åæ ãããŸãã
次ã«Flywayãå®è¡ããæã«ã¯ãå床ãã¡ã€ã«ã·ã¹ãã ãã¯ã©ã¹ãã¹ãã¹ãã£ã³ãããã€ã°ã¬ãŒã·ã§ã³ã確èªããŸãã
æ€åºããããã€ã°ã¬ãŒã·ã§ã³ãšã¹ããŒãå±¥æŽããŒãã«ã確èªããçŸåšé©çšæžã¿ã®ããŒãžã§ã³ä»¥äžã®å Žåã¯
ãã®ãã€ã°ã¬ãŒã·ã§ã³ã¯ç¡èŠãããŸãã
ãã以å€ã®ãã€ã°ã¬ãŒã·ã§ã³ã¯ãã³ãã£ã³ã°ãããããŒãžã§ã³çªå·ã§ãœãŒããããåŸã«é©çšãããŸãã
ãããŠãã¹ããŒãå±¥æŽããŒãã«ã«çµæãåæ ãããŸãã
ãã®ç¹°ãè¿ãããšããæãã§ãããé©çšãããã€ã°ã¬ãŒã·ã§ã³ã¯ãDDLãDMLãšãã£ãçš®é¡ã¯åããŸããã
ãã€ã°ã¬ãŒã·ã§ã³ã®è©³çŽ°
ãã€ã°ã¬ãŒã·ã§ã³ã®è©³ããæ å ±ã¯ããã¡ãã®ããŒãžãåç §ã§ããã
Migrations - Migrations - Flyway by Redgate • Database Migrations Made Easy.
ãã€ã°ã¬ãŒã·ã§ã³ã«ã¯ã次ã®3çš®é¡ããããŸãã
- Migrations / Versioned Migrations
- Migrations / Undo Migrations
- Team Editionã®ã¿
- Migrations / Repeatable Migrations
Undo Migrationã¯åãæ¶ããè¡šããã€ã°ã¬ãŒã·ã§ã³ã§ãããTeam Editionã§ãªããšäœ¿ããŸããâŠã
Versioned Migrationãäžè¬çãªãã€ã°ã¬ãŒã·ã§ã³ã§ãããŒãžã§ã³ã説æããã§ãã¯ãµã ããæããŸãã
ããŒãžã§ã³ã¯äžæã§ããå¿
èŠãããã説æã¯ãã€ã°ã¬ãŒã·ã§ã³ãã©ã®ãããªãã®ãã瀺ãæ
å ±ã«ãªããŸãã
ãã§ãã¯ãµã ã¯ãã€ã°ã¬ãŒã·ã§ã³èªäœã®å€æŽæ€åºã«äœ¿ãããŸãã
Versioned Migrationã¯1床ã ãé©çšããããã®å
容ãšããŠã¯DDLãDMLãªã©ã«ãªããŸãã
ãã®é©çšé ã¯ãããŒãžã§ã³é ã§ããåºæ¬çã«ã¯å調å¢å ããæŽæ°ã§è¯ãã®ã§ããã.
ã_
ãå«ãã æè»ãªå€ã
æå®ããããšãã§ããŸãã
Repeatable Migrationã¯ã説æãšãã§ãã¯ãµã ã¯ãããã®ã®ãããŒãžã§ã³ã®ãªããã€ã°ã¬ãŒã·ã§ã³ã§ãã
å®è¡ã¯1床ã ãã§ã¯ãªãããã§ãã¯ãµã ãå€æŽããã床ã«å床å®è¡ãããŸãã
ããã¯ããã¥ãŒãã¹ãã¢ãããã·ãŒãžã£ãªã©ã®äœæãããŒã¿ã®äžæ¬ç»é²ãªã©ãå©çšçšéã®ããã§ãã
é©çšé ã¯ãVersioned Migrationããã¹ãŠå®è¡ãããåŸã«Repeatable Migrationãå®è¡ãããããã§ãã
Repeatable Migrationèªäœã®é©çšé ã¯ã説æã®é çªã¿ããã§ããã
ãããã®ãã€ã°ã¬ãŒã·ã§ã³ã¯ãSQLãŸãã¯Javaã§å®è£ ããããšã«ãªããŸãã
Migrations / SQL-based migrations
Migrations / Java-based migrations
ãã¡ãã«ãã€ã°ã¬ãŒã·ã§ã³ã®åœåèŠåããã€ã°ã¬ãŒã·ã§ã³ã®ãã£ã¹ã«ããªãŒããµããŒãããŠããæ§æãªã©ã
èšèŒãããŠããŸãã
説æã¯ãã£ãããããããã«ããŠãåãããŠã¿ãããšã«ããŸãããã
ç°å¢
ä»åã¯ãFlywayã®é©çšå
ããŒã¿ããŒã¹ãšããŠMySQLã䜿ãããšã«ããŸãã
ããŒãžã§ã³ã¯8.0.26ã䜿ãã172.17.0.2ã§åäœããŠãããã®ãšããŸãã
Flyway Command LineããŒã«ã䜿ã
ãŸãã¯ãFlywayã®Comamand LineããŒã«ã䜿ã£ãŠã¿ãŸãããã
SpawnãšããããŒã«ããããFirst stepsã¯ãã¡ãã䜿ã£ãããã¥ã¡ã³ãã«ãªã£ãŠããŸãããä»åã¯äœ¿ããªãããšã«ããŸãã
çŸæç¹ã®ææ°çã¯8.0.0ã®betaãªã®ã§ãä»åã¯7.15.0ã䜿ãããšã«ããŸãã
ããŠã³ããŒãããŠ
$ curl -OL https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/7.15.0/flyway-commandline-7.15.0-linux-x64.tar.gz
å±éã
$ tar xf flyway-commandline-7.15.0-linux-x64.tar.gz
ãããªæãã®ãã£ã¬ã¯ããªæ§æã®ããã§ãã
$ find flyway-7.15.0 -maxdepth 1 -type d flyway-7.15.0 flyway-7.15.0/sql flyway-7.15.0/drivers flyway-7.15.0/jre flyway-7.15.0/lib flyway-7.15.0/conf flyway-7.15.0/jars flyway-7.15.0/licenses
JREãå ¥ã£ãŠããã¿ããã§ããã
ãã£ã¬ã¯ããªå ã«ç§»åã
$ cd flyway-7.15.0
ãã«ãã
$ ./flyway --help
Javaã¯ãAdoptOpenJDKã®11ãå ¥ã£ãŠããããã§ãã
$ jre/bin/java --version openjdk 11.0.2 2019-01-15 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.2+9) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.2+9, mixed mode)
èšå®ãã¡ã€ã«ã確èªããŠã¿ãŸãã
conf/flyway.conf
$ grep -vE '#|^$' conf/flyway.conf flyway.locations=filesystem:sql
ããã©ã«ãã§ã¯ããã¡ã€ã«ã·ã¹ãã äžã®sql
ãã£ã¬ã¯ããªããã€ã°ã¬ãŒã·ã§ã³é
çœ®å Žæã«æå®ããŠããããã§ãã
èšå®é ç®ã¯ããã¡ããåç §ã
configuration - Configuration - Flyway by Redgate • Database Migrations Made Easy.
First stepsã®ããã¥ã¡ã³ãã«åãããŠãJDBCæ¥ç¶æã®URLãšãŠãŒã¶ãŒåããã¹ã¯ãŒããèšå®ã
$ grep -vE '#|^$' conf/flyway.conf flyway.url=jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&characterSetResults=utf-8 flyway.user=kazuhira flyway.password=password flyway.locations=filesystem:sql
JDBCãã©ã€ããŒã¯ãFlywayèªäœã«å«ãŸããŠããŸãã
$ ll drivers | perl -wp -e 's!kazuhira!xxxxx!g' åèš 58580 drwxrwxr-x 3 xxxxx xxxxx 4096 10æ 6 21:40 ./ drwxrwxr-x 9 xxxxx xxxxx 4096 10æ 6 21:40 ../ -rw-r--r-- 1 xxxxx xxxxx 19883 9æ 6 23:18 aws-secretsmanager-jdbc-1.0.6.jar -rw-r--r-- 1 xxxxx xxxxx 3683035 9æ 6 23:18 derby-10.15.2.0.jar -rw-r--r-- 1 xxxxx xxxxx 601103 9æ 6 23:18 derbyclient-10.15.2.0.jar -rw-r--r-- 1 xxxxx xxxxx 93688 9æ 6 23:18 derbyshared-10.15.2.0.jar -rw-r--r-- 1 xxxxx xxxxx 264445 9æ 6 23:18 derbytools-10.15.2.0.jar drwxrwxr-x 2 xxxxx xxxxx 4096 10æ 6 21:40 gcp/ -rw-r--r-- 1 xxxxx xxxxx 143459 9æ 6 23:18 google-cloud-spanner-jdbc-2.2.6.jar -rw-r--r-- 1 xxxxx xxxxx 346711 9æ 6 23:18 google-cloud-storage-1.113.13.jar -rw-r--r-- 1 xxxxx xxxxx 2303679 9æ 6 23:18 h2-1.4.200.jar -rw-r--r-- 1 xxxxx xxxxx 1623068 9æ 6 23:18 hsqldb-2.6.0.jar -rw-r--r-- 1 xxxxx xxxxx 1161511 9æ 6 23:18 jaybird-jdk18-3.0.10.jar -rw-r--r-- 1 xxxxx xxxxx 1484022 9æ 6 23:18 jna-4.5.2.jar -rw-r--r-- 1 xxxxx xxxxx 2327597 9æ 6 23:18 jna-platform-4.5.2.jar -rw-r--r-- 1 xxxxx xxxxx 317816 9æ 6 23:18 jtds-1.3.1.jar -rw-r--r-- 1 xxxxx xxxxx 632979 9æ 6 23:18 mariadb-java-client-2.7.2.jar -rw-r--r-- 1 xxxxx xxxxx 279641 9æ 6 23:18 msal4j-1.10.1.jar -rw-r--r-- 1 xxxxx xxxxx 1327642 9æ 6 23:18 mssql-jdbc-9.2.1.jre8.jar -rw-r--r-- 1 xxxxx xxxxx 2428323 9æ 6 23:18 mysql-connector-java-8.0.24.jar -rw-r--r-- 1 xxxxx xxxxx 4397918 9æ 6 23:18 ojdbc8-19.6.0.0.jar -rw-r--r-- 1 xxxxx xxxxx 1005078 9æ 6 23:18 postgresql-42.2.19.jar -rw-r--r-- 1 xxxxx xxxxx 0 9æ 6 23:12 put-your-jdbc-drivers-here.txt -rw-r--r-- 1 xxxxx xxxxx 28177186 9æ 6 23:18 snowflake-jdbc-3.13.1.jar -rw-r--r-- 1 xxxxx xxxxx 7296329 9æ 6 23:18 sqlite-jdbc-3.34.0.jar
ã§ã¯ã䜿ã£ãŠãããŸãã
ãŸãã¯ã察象ãšãªãMySQLã®ããŒã¿ããŒã¹ã«ãªã«ããªãããšã確èªã
mysql> show tables; Empty set (0.00 sec)
sql
ãã£ã¬ã¯ããªã«ããã€ã°ã¬ãŒã·ã§ã³ãäœæããŸããä»åã¯SQLãšããŠäœæããŸãããã
sql/V1__create_book_table.sql
create table book ( isbn varchar(14), title varchar(255), price int, primary key(isbn) );
ãã€ã°ã¬ãŒã·ã§ã³ã®åœåèŠåã¯ããã¡ããåç §ã
Migrations / SQL-based migrations / Naming
ãã€ã°ã¬ãŒã·ã§ã³ã®ååã¯ã以äžã®èŠçŽ ã§æ§æãããŸãã
- Prefix ⊠VãUãRïŒãã€ã°ã¬ãŒã·ã§ã³ã®çš®é¡ïŒ
- Version ⊠ããŒãžã§ã³
- Separator ⊠éåžžã
__
- Description ⊠説æïŒã¢ã³ããŒã¹ã³ã¢ãã¹ããŒã¹ã§åºåãïŒ
- Suffix âŠ
.sql
ä»åã¯V1__create_book_table.sql
ã§ãVersion MigrationãããŒãžã§ã³ã1ã説æã¯ãcreate book tableããšãªã£ãŠããŸãã
info
ã³ãã³ããè©ŠããŠã¿ãŸãã
Info - Info - Flyway by Redgate • Database Migrations Made Easy.
$ ./flyway info Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Schema version: << Empty Schema >> +-----------+---------+-------------------+------+--------------+---------+----------+ | Category | Version | Description | Type | Installed On | State | Undoable | +-----------+---------+-------------------+------+--------------+---------+----------+ | Versioned | 1 | create book table | SQL | | Pending | No | +-----------+---------+-------------------+------+--------------+---------+----------+
äœæãããã€ã°ã¬ãŒã·ã§ã³ãæ€åºããããŸã ãã³ãã£ã³ã°ã§ããããšã衚瀺ãããŸãã
ã§ã¯ãmigrate
ã³ãã³ããå®è¡ã
Migrate - Migrate - Flyway by Redgate • Database Migrations Made Easy.
$ ./flyway migrate Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Successfully validated 1 migration (execution time 00:00.020s) Creating Schema History table `practice`.`flyway_schema_history` ... Current version of schema `practice`: << Empty Schema >> Migrating schema `practice` to version "1 - create book table" Successfully applied 1 migration to schema `practice`, now at version v1 (execution time 00:00.211s)
ãã€ã°ã¬ãŒã·ã§ã³ãé©çšãããããŒãã«ãäœæãããŸãã
mysql> show tables; +-----------------------+ | Tables_in_practice | +-----------------------+ | book | | flyway_schema_history | +-----------------------+ 2 rows in set (0.00 sec)
ã¹ããŒãå±¥æŽããŒãã«ãã§ããŠããã®ã§ãäžèº«ãèŠãŠã¿ãŸãããã
mysql> select * from flyway_schema_history; +----------------+---------+-------------------+------+---------------------------+-----------+--------------+---------------------+----------------+---------+ | installed_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success | +----------------+---------+-------------------+------+---------------------------+-----------+--------------+---------------------+----------------+---------+ | 1 | 1 | create book table | SQL | V1__create_book_table.sql | 657681960 | kazuhira | 2021-10-06 13:12:37 | 147 | 1 | +----------------+---------+-------------------+------+---------------------------+-----------+--------------+---------------------+----------------+---------+ 1 row in set (0.00 sec)
é©çšãããã€ã°ã¬ãŒã·ã§ã³ã®æ å ±ã衚瀺ãããŸãã
ãã®ç¶æ
ã§ãvalidate
ãå®è¡ããŠããŒã«ã«ãšã®å·®ã確èªããŠã¿ãŸãã
Validate - Validate - Flyway by Redgate • Database Migrations Made Easy.
$ ./flyway validate Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Successfully validated 1 migration (execution time 00:00.022s)
å·®ã¯ãããŸããã
ããã§ããã€ã°ã¬ãŒã·ã§ã³ãè¿œå ããŠã¿ãŸããããŒãžã§ã³ã¯2ã«ããŸãã
sql/V2__create_account_table.sql
create table account ( id int, name varchar(50), registered datetime, about varchar(255), primary key(id) );
info
ã§èŠããšããã³ãã£ã³ã°ç¶æ
ã®ãã€ã°ã¬ãŒã·ã§ã³ãè¿œå ãããŠããŸãã
$ ./flyway info Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Schema version: 1 +-----------+---------+----------------------+------+---------------------+---------+----------+ | Category | Version | Description | Type | Installed On | State | Undoable | +-----------+---------+----------------------+------+---------------------+---------+----------+ | Versioned | 1 | create book table | SQL | 2021-10-06 13:12:37 | Success | No | | Versioned | 2 | create account table | SQL | | Pending | No | +-----------+---------+----------------------+------+---------------------+---------+----------+
ããã§validate
ãå®è¡ãããšããŒã«ã«ãšããŒã¿ããŒã¹ã§å·®ãããã®ã§ãNGã«ãªãããšã確èªã§ããŸãã
$ ./flyway validate Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- ERROR: Validate failed: Migrations have failed validation Detected resolved migration not applied to database: 2. To fix this error, either run migrate, or set -ignorePendingMigrations=true. Need more flexibility with validation rules? Learn more: https://rd.gt/3AbJUZE
å床migrate
ãå®è¡ã
$ ./flyway migrate Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Successfully validated 2 migrations (execution time 00:00.037s) Current version of schema `practice`: 1 Migrating schema `practice` to version "2 - create account table" Successfully applied 1 migration to schema `practice`, now at version v2 (execution time 00:00.233s)
ããŒãã«ãè¿œå ãããŸããã
mysql> show tables; +-----------------------+ | Tables_in_practice | +-----------------------+ | account | | book | | flyway_schema_history | +-----------------------+ 3 rows in set (0.00 sec)
ããã§ãè©Šãã«ããŒãžã§ã³1_1ãšã2ãããäœããã€ã°ã¬ãŒã·ã§ã³ãè¿œå ããŠã¿ãŸãã
ãããinfo
ã§ç¢ºèªã
$ cp sql/V1__create_book_table.sql sql/V1_1__create_book_table.sql $ ./flyway info Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 28 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Schema version: 2 +-----------+---------+----------------------+------+---------------------+---------+----------+ | Category | Version | Description | Type | Installed On | State | Undoable | +-----------+---------+----------------------+------+---------------------+---------+----------+ | Versioned | 1 | create book table | SQL | 2021-10-06 13:12:37 | Success | No | | Versioned | 2 | create account table | SQL | 2021-10-06 13:26:48 | Success | No | | Versioned | 1.1 | create book table | SQL | | Ignored | No | +-----------+---------+----------------------+------+---------------------+---------+----------+
ãããšãçŸåšã®ããŒãžã§ã³ã§ãã2ãããäœãã®ã§ãç¡èŠãããããšã確èªã§ããŸãã
FlywayãããŒãžã§ã³ãã©ã®ããã«è§£éããŠãããã¯ããã¡ããèŠããšããããã§ãã
ããã¥ã¡ã³ãã§ãååã€ã¡ãŒãžã¯ã€ãããŸããã©ãããã詳现ã«èŠããå Žåã¯ããšã
Migrations / SQL-based migrations / Naming
è¿œå ãããç¡èŠå¯Ÿè±¡ã®ãã€ã°ã¬ãŒã·ã§ã³ã¯åé€ããŠãããŸãã
$ rm sql/V1_1__create_book_table.sql
ä»åºŠã¯ããŒãžã§ã³3ãäœæããŸããããããŒã¿ç»é²ã«ããŸãã
sql/V3__insert_books.sql
insert into book(isbn, title, price) values('978-4798161488', 'MySQL培åºå ¥é 第4ç MySQL 8.0察å¿', 4180); insert into book(isbn, title, price) values('978-4798147406', '詳解MySQL 5.7 æ¢ãŸãã¬é²åã«ä¹ãé ããªãããã®ãã¯ãã«ã«ã¬ã€ã', 3960); insert into book(isbn, title, price) values('978-4873116389', 'å®è·µãã€ããã©ãŒãã³ã¹MySQL 第3ç', 5280);
migrate
ãå®è¡ã
$ ./flyway migrate Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Successfully validated 3 migrations (execution time 00:00.031s) Current version of schema `practice`: 2 Migrating schema `practice` to version "3 - insert books" Successfully applied 1 migration to schema `practice`, now at version v3 (execution time 00:00.108s)
ããŒã¿ãå ¥ããŸããã
mysql> select * from book; +----------------+------------------------------------------------------------------------------------------+-------+ | isbn | title | price | +----------------+------------------------------------------------------------------------------------------+-------+ | 978-4798147406 | 詳解MySQL 5.7 æ¢ãŸãã¬é²åã«ä¹ãé ããªãããã®ãã¯ãã«ã«ã¬ã€ã | 3960 | | 978-4798161488 | MySQL培åºå ¥é 第4ç MySQL 8.0å¯Ÿå¿ | 4180 | | 978-4873116389 | å®è·µãã€ããã©ãŒãã³ã¹MySQL 第3ç | 5280 | +----------------+------------------------------------------------------------------------------------------+-------+ 3 rows in set (0.00 sec)
clean
ãè©ŠããŠã¿ãŸããããããã¯ãã¹ããŒãå
ã®ãªããžã§ã¯ãïŒããŒãã«ããã¥ãŒãã¹ãã¢ãããã·ãŒãžã£ãªã©ïŒã
ç Žæ£ããŠã¯ãªãŒã³ã¢ããããã³ãã³ãã§ãããã¡ãããæ¬çªç°å¢ã®ããŒã¿ããŒã¹ã«å¯ŸããŠäœ¿ãã¹ãã³ãã³ãã§ã¯ãããŸããã
Clean - Clean - Flyway by Redgate • Database Migrations Made Easy.
$ ./flyway clean Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Successfully dropped pre-schema database level objects (execution time 00:00.002s) Successfully cleaned schema `practice` (execution time 00:00.295s) Successfully dropped post-schema database level objects (execution time 00:00.001s)
äœæããããŒãã«ããã¬ã€ã«ãªããªããŸããã
mysql> show tables; Empty set (0.00 sec)
Repeatable Migrationã䜿ã£ãŠã¿ãŸããããæåŸã«äœæãããã€ã°ã¬ãŒã·ã§ã³ã®PrefixãRã«ããŠããŒãžã§ã³ã¯ãªãããŠã¿ãŸãã
$ mv sql/V3__insert_books.sql sql/R__insert_books.sql
äžèº«ã¯ãåå®è¡å¯èœãªããã«truncate table
ãå
¥ããŠãããŸãã
sql/R__insert_books.sql
truncate table book; insert into book(isbn, title, price) values('978-4798161488', 'MySQL培åºå ¥é 第4ç MySQL 8.0察å¿', 4180); insert into book(isbn, title, price) values('978-4798147406', '詳解MySQL 5.7 æ¢ãŸãã¬é²åã«ä¹ãé ããªãããã®ãã¯ãã«ã«ã¬ã€ã', 3960); insert into book(isbn, title, price) values('978-4873116389', 'å®è·µãã€ããã©ãŒãã³ã¹MySQL 第3ç', 5280);
ãã®ç¶æ
ã§ãmigrate
ãå®è¡ã
$ ./flyway migrate Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Successfully validated 3 migrations (execution time 00:00.031s) Creating Schema History table `practice`.`flyway_schema_history` ... Current version of schema `practice`: << Empty Schema >> Migrating schema `practice` to version "1 - create book table" Migrating schema `practice` to version "2 - create account table" Migrating schema `practice` with repeatable migration "insert books" Successfully applied 3 migrations to schema `practice`, now at version v2 (execution time 00:01.017s)
ããŒã¿ãå ¥ããŸããã
mysql> select * from book; +----------------+------------------------------------------------------------------------------------------+-------+ | isbn | title | price | +----------------+------------------------------------------------------------------------------------------+-------+ | 978-4798147406 | 詳解MySQL 5.7 æ¢ãŸãã¬é²åã«ä¹ãé ããªãããã®ãã¯ãã«ã«ã¬ã€ã | 3960 | | 978-4798161488 | MySQL培åºå ¥é 第4ç MySQL 8.0å¯Ÿå¿ | 4180 | | 978-4873116389 | å®è·µãã€ããã©ãŒãã³ã¹MySQL 第3ç | 5280 | +----------------+------------------------------------------------------------------------------------------+-------+ 3 rows in set (0.00 sec)
info
ã§èŠãŠã¿ãŸãã
$ ./flyway info Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Schema version: 2 +------------+---------+----------------------+------+---------------------+---------+----------+ | Category | Version | Description | Type | Installed On | State | Undoable | +------------+---------+----------------------+------+---------------------+---------+----------+ | Versioned | 1 | create book table | SQL | 2021-10-06 13:45:23 | Success | No | | Versioned | 2 | create account table | SQL | 2021-10-06 13:45:24 | Success | No | | Repeatable | | insert books | SQL | 2021-10-06 13:45:24 | Success | | +------------+---------+----------------------+------+---------------------+---------+----------+
ããŒã¿ã確èªã
mysql> select * from flyway_schema_history; +----------------+---------+----------------------+------+------------------------------+------------+--------------+---------------------+----------------+---------+ | installed_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success | +----------------+---------+----------------------+------+------------------------------+------------+--------------+---------------------+----------------+---------+ | 1 | 1 | create book table | SQL | V1__create_book_table.sql | 657681960 | kazuhira | 2021-10-06 13:45:23 | 116 | 1 | | 2 | 2 | create account table | SQL | V2__create_account_table.sql | 1435889679 | kazuhira | 2021-10-06 13:45:24 | 275 | 1 | | 3 | NULL | insert books | SQL | R__insert_books.sql | -871738101 | kazuhira | 2021-10-06 13:45:24 | 428 | 1 | +----------------+---------+----------------------+------+------------------------------+------------+--------------+---------------------+----------------+---------+ 3 rows in set (0.00 sec)
ããã§ãããŒã¿ãè¿œå ããŠã¿ãŸãã
sql/R__insert_books.sql
truncate table book; insert into book(isbn, title, price) values('978-4798161488', 'MySQL培åºå ¥é 第4ç MySQL 8.0察å¿', 4180); insert into book(isbn, title, price) values('978-4798147406', '詳解MySQL 5.7 æ¢ãŸãã¬é²åã«ä¹ãé ããªãããã®ãã¯ãã«ã«ã¬ã€ã', 3960); insert into book(isbn, title, price) values('978-4873116389', 'å®è·µãã€ããã©ãŒãã³ã¹MySQL 第3ç', 5280); insert into book(isbn, title, price) values('978-4295000297', 'MySQL å³å¹ã¯ãšãªãã¥ãŒãã³ã°', 1980);
info
ã§ç¢ºèªããŠã¿ããšããã³ãã£ã³ã°ç¶æ
ã®ãã€ã°ã¬ãŒã·ã§ã³ãæ€åºãããŸãã
$ ./flyway info Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 28 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Schema version: 2 +------------+---------+----------------------+------+---------------------+----------+----------+ | Category | Version | Description | Type | Installed On | State | Undoable | +------------+---------+----------------------+------+---------------------+----------+----------+ | Versioned | 1 | create book table | SQL | 2021-10-06 13:45:23 | Success | No | | Versioned | 2 | create account table | SQL | 2021-10-06 13:45:24 | Success | No | | Repeatable | | insert books | SQL | 2021-10-06 13:45:24 | Outdated | | | Repeatable | | insert books | SQL | | Pending | | +------------+---------+----------------------+------+---------------------+----------+----------+
migrate
ãå®è¡ããŠã¿ãŸãã
$ ./flyway migrate
ãããšãRepeatable Migrationã1ã¬ã³ãŒãè¿œå ãããŸããåã«é©çšãããã®ã¯ãã眮ãæãããããã®ããšãã
æ±ãã«ãªãããã§ãã
$ ./flyway info Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Schema version: 2 +------------+---------+----------------------+------+---------------------+------------+----------+ | Category | Version | Description | Type | Installed On | State | Undoable | +------------+---------+----------------------+------+---------------------+------------+----------+ | Versioned | 1 | create book table | SQL | 2021-10-06 13:45:23 | Success | No | | Versioned | 2 | create account table | SQL | 2021-10-06 13:45:24 | Success | No | | Repeatable | | insert books | SQL | 2021-10-06 13:45:24 | Superseded | | | Repeatable | | insert books | SQL | 2021-10-06 13:51:32 | Success | | +------------+---------+----------------------+------+---------------------+------------+----------+
ã¹ããŒãå±¥æŽããŒãã«ã確èªã
mysql> select * from flyway_schema_history; +----------------+---------+----------------------+------+------------------------------+-------------+--------------+---------------------+----------------+---------+ | installed_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success | +----------------+---------+----------------------+------+------------------------------+-------------+--------------+---------------------+----------------+---------+ | 1 | 1 | create book table | SQL | V1__create_book_table.sql | 657681960 | kazuhira | 2021-10-06 13:45:23 | 116 | 1 | | 2 | 2 | create account table | SQL | V2__create_account_table.sql | 1435889679 | kazuhira | 2021-10-06 13:45:24 | 275 | 1 | | 3 | NULL | insert books | SQL | R__insert_books.sql | -871738101 | kazuhira | 2021-10-06 13:45:24 | 428 | 1 | | 4 | NULL | insert books | SQL | R__insert_books.sql | -1366018225 | kazuhira | 2021-10-06 13:51:32 | 175 | 1 | +----------------+---------+----------------------+------+------------------------------+-------------+--------------+---------------------+----------------+---------+ 4 rows in set (0.00 sec)
ããŒã¿ãå ¥ã£ãŠããããšã確èªã
mysql> select * from book; +----------------+------------------------------------------------------------------------------------------+-------+ | isbn | title | price | +----------------+------------------------------------------------------------------------------------------+-------+ | 978-4295000297 | MySQL å³å¹ã¯ãšãªãã¥ãŒãã³ã° | 1980 | | 978-4798147406 | 詳解MySQL 5.7 æ¢ãŸãã¬é²åã«ä¹ãé ããªãããã®ãã¯ãã«ã«ã¬ã€ã | 3960 | | 978-4798161488 | MySQL培åºå ¥é 第4ç MySQL 8.0å¯Ÿå¿ | 4180 | | 978-4873116389 | å®è·µãã€ããã©ãŒãã³ã¹MySQL 第3ç | 5280 | +----------------+------------------------------------------------------------------------------------------+-------+ 4 rows in set (0.00 sec)
æåŸã«clean
ã§ç Žæ£ããŠãããŸãããã
$ ./flyway clean Flyway Teams Edition 7.15.0 by Redgate Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) ---------------------------------------- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://rd.gt/3A4IWym ---------------------------------------- Successfully dropped pre-schema database level objects (execution time 00:00.001s) Successfully cleaned schema `practice` (execution time 00:00.279s) Successfully dropped post-schema database level objects (execution time 00:00.008s)
ããã§ãCommand LineããŒã«ã䜿ã£ãŠåºæ¬çãªäœ¿ãæ¹ã¯ç¢ºèªã§ããã®ã§ã¯ãªãã§ããããã
Java APIã䜿ã
æåŸã«ãJava APIã䜿ã£ãŠã¿ãŸãããã
å
ã»ã©ãŸã§Flywayã®Comnand LineããŒã«ã§å®è¡ããŠããããšããJavaã§è¡ãæãã§ããã
ããã¥ã¡ã³ãã¯ããã®ããããåèã«ã
API - First Steps - First Steps: API - Flyway by Redgate • Database Migrations Made Easy.
API - API - Flyway by Redgate • Database Migrations Made Easy.
ç°å¢ã
$ java --version openjdk 11.0.11 2021-04-20 OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04) OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing) $ mvn --version Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739) Maven home: $HOME/.sdkman/candidates/maven/current Java version: 11.0.11, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64 Default locale: ja_JP, platform encoding: UTF-8 OS name: "linux", version: "5.4.0-88-generic", arch: "amd64", family: "unix"
MavenäŸåé¢ä¿ãFlywayãšMySQLã®JDBCãã©ã€ããŒã§ãã
<dependencies> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>7.15.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> <scope>runtime</scope> </dependency> </dependencies>
äœæãããœãŒã¹ã³ãŒãã
src/main/java/org/littlewings/flyway/App.java
package org.littlewings.flyway; import org.flywaydb.core.Flyway; public class App { public static void main(String... args) { Flyway flyway = Flyway .configure() .dataSource( "jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&characterSetResults=utf-8", "kazuhira", "password" ) .load(); flyway.migrate(); } }
Flyway
ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãäœæããŠãmigrate
ã¡ãœãããªã©ã®Flywayã®ã³ãã³ãã«å¯Ÿå¿ããã¡ãœããã
åŒã³åºããŸãã
Flyway
ã¯ã©ã¹ã®Javadocã¯ãã¡ãã
API: Javadoc - Flyway by Redgate • Database Migrations Made Easy.
ä»åãèšå®ã¯ããŒã¿ãœãŒã¹ïŒJDBC URLããŠãŒã¶ãŒåããã¹ã¯ãŒãïŒã®ã¿ã«ãšã©ããŠããŸãã
ãã€ã°ã¬ãŒã·ã§ã³ã¯ãã©ãããæ€åºããããšãããš
flyway.locations - Locations - Flyway by Redgate • Database Migrations Made Easy.
ããã©ã«ãå€ã®classpath:db/migration
ãšãªããŸããã¯ã©ã¹ãã¹äžã§ããã
ãšããããã§ãä»åã¯src/main/resources/db/migration
ãã£ã¬ã¯ããªã«Command LineããŒã«ã§äœ¿ã£ãæãšåã
ãã€ã°ã¬ãŒã·ã§ã³ïŒSQLãã¡ã€ã«ïŒãé
眮ããŸãã
$ tree src/main/resources src/main/resources âââ db âââ migration âââ R__insert_books.sql âââ V1__create_book_table.sql âââ V2__create_account_table.sql 2 directories, 3 files
ã¡ãªã¿ã«ãèšå®ãããã¡ãã£ãšè©³çŽ°ã«è¡ãå Žåã¯ãFlyway
ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã«Configuration
ã€ã³ã¿ãŒãã§ãŒã¹ã®
å®è£
ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãæž¡ããŸãã
Configuration
ã€ã³ã¿ãŒãã§ãŒã¹ã®å®è£
ãšããŠã¯ãClassicConfiguration
ãFluentConfiguration
ã®2çš®é¡ããããŸãã
ã§ã¯ãå®è¡ã
$ mvn compile exec:java -Dexec.mainClass=org.littlewings.flyway.App
ãã°ãåºåãããFlywayãå®è¡ãããŠããæ§åãããããŸãã
[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ flyway-example --- 10æ 07, 2021 12:29:56 åå org.flywaydb.core.internal.license.VersionPrinter printVersionOnly æ å ±: Flyway Community Edition 7.15.0 by Redgate 10æ 07, 2021 12:29:56 åå org.flywaydb.core.internal.database.base.BaseDatabaseType createDatabase æ å ±: Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) 10æ 07, 2021 12:29:56 åå org.flywaydb.core.internal.command.DbValidate validate æ å ±: Successfully validated 3 migrations (execution time 00:00.022s) 10æ 07, 2021 12:29:56 åå org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory create æ å ±: Creating Schema History table `practice`.`flyway_schema_history` ... 10æ 07, 2021 12:29:57 åå org.flywaydb.core.internal.command.DbMigrate migrateGroup æ å ±: Current version of schema `practice`: << Empty Schema >> 10æ 07, 2021 12:29:57 åå org.flywaydb.core.internal.command.DbMigrate doMigrateGroup æ å ±: Migrating schema `practice` to version "1 - create book table" 10æ 07, 2021 12:29:57 åå org.flywaydb.core.internal.command.DbMigrate doMigrateGroup æ å ±: Migrating schema `practice` to version "2 - create account table" 10æ 07, 2021 12:29:57 åå org.flywaydb.core.internal.command.DbMigrate doMigrateGroup æ å ±: Migrating schema `practice` with repeatable migration "insert books" 10æ 07, 2021 12:29:57 åå org.flywaydb.core.internal.command.DbMigrate logSummary æ å ±: Successfully applied 3 migrations to schema `practice`, now at version v2 (execution time 00:00.630s)
ããŒã¿ããŒã¹åŽã確èªãããŒã¿ãå ¥ã£ãŠããŸããã
mysql> show tables; +-----------------------+ | Tables_in_practice | +-----------------------+ | account | | book | | flyway_schema_history | +-----------------------+ 3 rows in set (0.00 sec) mysql> select * from book; +----------------+------------------------------------------------------------------------------------------+-------+ | isbn | title | price | +----------------+------------------------------------------------------------------------------------------+-------+ | 978-4295000297 | MySQL å³å¹ã¯ãšãªãã¥ãŒãã³ã° | 1980 | | 978-4798147406 | 詳解MySQL 5.7 æ¢ãŸãã¬é²åã«ä¹ãé ããªãããã®ãã¯ãã«ã«ã¬ã€ã | 3960 | | 978-4798161488 | MySQL培åºå ¥é 第4ç MySQL 8.0å¯Ÿå¿ | 4180 | | 978-4873116389 | å®è·µãã€ããã©ãŒãã³ã¹MySQL 第3ç | 5280 | +----------------+------------------------------------------------------------------------------------------+-------+ 4 rows in set (0.00 sec)
ããã§ãJava APIã®æ¹ã確èªã§ããŸããããšã
ãŸãšã
Flywayã䜿ã£ãããšããªãã£ãã®ã§ãä»åã¯ã©ããããã®ãã確èªããæå³ãå«ããŠãããã¥ã¡ã³ãã®èšèŒå
容ãã
åºæ¬çãªäœ¿ãæ¹ãŸã§ã確èªããŠã¿ãŸããã
ãªããšãªãã€ã¡ãŒãžã¯ç¥ã£ãŠããã€ããã§ããããã¡ãããšäœ¿ã£ãŠã¿ãæ¹ããã£ã±ãç解ãé²ã¿ãŸããã
ãã®ãã¡ãSpring BootãQuarkusãããã«çµã¿èŸŒãã ãã®ãšåãããŠäœ¿ã£ãŠã¿ãããã®ã§ãã