ããã¯ããªã«ããããã¦æ¸ãããã®ï¼
JUnitãã¹ãå®è¡æã«ãFlywayãçµã¿åããã¦ä½¿ããããªã¨æãã¾ãã¦ã
ããDatabase Riderã使ãã®ã§ãä»åã¯JUnit 5 Extensionã¨ãã¦Database Riderã®å®è¡åã«Flywayãã¤ã°ã¬ã¼ã·ã§ã³ãåããããã«çµã¿è¾¼ãã§
ã¿ããã¨æãã¾ãã
Flywayã¨JUnitãã¹ã
Spring Bootã®ããã«Flywayããµãã¼ããã¦ãã¦ãã³ã³ããã®èµ·åæã«å®è¡ãã¦ãã¾ããããªãã®ã使ã£ãããããã§ã¯è©±ãçµãã£ã¦ãã¾ãã®ã§
ä»åã¯ãã®ãããªæ§æã¯å¯¾è±¡å¤ã«ãã¾ãã
Flywayãµãã¼ãããªãç¶æ ã§ããã¹ãæã«å®è¡ãããå ´åã¯ï¼ã¨ãã話ããã¼ãã«ãã¦ãã¾ãã
ã¾ãã¯Flywayèªä½ãè¦ãã¨ãFlyway Test Extensionsã¨ãããã®ãããããã§ãã
GitHub - flyway/flyway-test-extensions
ãªã®ã§ãããããã¯Spring Frameworkã¨çµã¿åããã¦ä½¿ããã¨ãåæã«ãªã£ã¦ããããã§ããã¨ãªãã¨ãSpring Bootã使ã£ããããã®ã§ã¯ã¨
ãã£ã±ã話ãçµãã£ã¦ãã¾ãã®ã§ãããã¯é¤å¤ã¨ãããã¨ã§ã
ã¨ãªãã¨ãFlywayã®ãã¤ã°ã¬ã¼ã·ã§ã³ããã¹ãã®å®è¡åã«åãããã«ããããªãã®ã§ãããããã«ã¯JUnit 5 Extensionã使ããããªã¨
æãã¾ãã@BeforeEach
ã@BeforeAll
ã§é½åº¦æ¸ãã¦ãããã®ã§ãããåãã¹ãã§åããããªãã¨ããããªãJUnit 5 Extensionã¨ãã¦
æ¸ãã¦ãããããªã¨ã
JUnit 5のExtension Modelを試す - CLOVER🍀
ä¸æ¹ã§ããã¹ããã¼ã¿ã®ç»é²ãã¢ãµã¼ã·ã§ã³ã«ã¯Database Riderã使ãããã®ã§ãDatabase Riderã®å®è¡åã«Flywayãã¤ã°ã¬ã¼ã·ã§ã³ã
è¡ããããªãµã³ãã«ããããã©ããè¦ã¦ã¿ã¾ãã
Database Riderã®ãµã³ãã«ã«ã¯ã@BeforeAll
ã®ã¿ã¤ãã³ã°ã§Flywayãã¤ã°ã¬ã¼ã·ã§ã³ãå®è¡ãããã®ãããã¾ããã
ã¨ããããã§ãFlywayãã¤ã°ã¬ã¼ã·ã§ã³ãJUnit 5 Extensionã¨ãã¦çµã¿è¾¼ã¿ã@BeforeAll
ã®ã¿ã¤ãã³ã°ã§å®è¡ããããã«ãã¦ã¿ã¾ãããã
FlywayãJavaã³ã¼ãå ã«çµã¿è¾¼ã
ããããã°ãèªåã§Flywayãã¤ã°ã¬ã¼ã·ã§ã³ãJavaã³ã¼ãããå®è¡ãããã¨ãããã¾ãããFlywayã®ããã¥ã¡ã³ããè¦ã¦ã¿ã¾ãããã
ãã¡ãã§ãããflyway-coreã使ãã¾ãã
API (Java) - Flyway - Product Documentation
ã¾ãããã¼ã¿ãã¼ã¹ã®ç¨®é¡ãã¨ã«è¿½å ã®ã©ã¤ãã©ãªã¼ãå¿ è¦ãªããã§ããä»åã¯MySQLã対象ã«ãã¾ãããã
MySQL - Flyway - Product Documentation
ãã®ä»ã®ãã¼ã¿ãã¼ã¹ã«ã¤ãã¦ã¯ããã¡ããããã©ã£ã¦ãã ããã
Supported Databases - Flyway - Product Documentation
ãªããflyway-coreã«ã¯ããã©ã«ãã§H2 DatabaseãSQLite 3ãTestcontainersã®ãµãã¼ããå
¥ã£ã¦ããããã§ãããã以å¤ã®ãã¼ã¿ãã¼ã¹ã«
ã¤ãã¦ã¯è¿½å ã®ã©ã¤ãã©ãªã¼ãå¿
è¦ã§ãã
ã§ã¯ã試ãã¦ãã£ã¦ã¿ã¾ãããã
ç°å¢
ä»åã®ç°å¢ã¯ãã¡ãã
$ java --version openjdk 21.0.3 2024-04-16 OpenJDK Runtime Environment (build 21.0.3+9-Ubuntu-1ubuntu122.04.1) OpenJDK 64-Bit Server VM (build 21.0.3+9-Ubuntu-1ubuntu122.04.1, mixed mode, sharing) $ mvn --version Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cffd34256) Maven home: $HOME/.sdkman/candidates/maven/current Java version: 21.0.3, 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-117-generic", arch: "amd64", family: "unix"
MySQLã¯172.17.0.2ã§åä½ãã¦ãããã®ã¨ãã¾ãã
MySQL localhost:3306 ssl practice SQL > select version(); +-----------+ | version() | +-----------+ | 8.0.39 | +-----------+ 1 row in set (0.0007 sec)
æºå
Mavenä¾åé¢ä¿ãªã©ã
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>org.seasar.doma</groupId> <artifactId>doma-core</artifactId> <version>2.61.0</version> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.0.33</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>3.26.3</version> <scope>test</scope> </dependency> <dependency> <groupId>com.github.database-rider</groupId> <artifactId>rider-junit5</artifactId> <version>1.44.0</version> <scope>test</scope> </dependency> <!-- <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>10.16.0</version> <scope>test</scope> </dependency> --> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-mysql</artifactId> <version>10.16.0</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> <configuration> <source>21</source> <target>21</target> <annotationProcessorPaths> <path> <groupId>org.seasar.doma</groupId> <artifactId>doma-processor</artifactId> <version>2.61.0</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
ãã¼ã¿ãã¼ã¹ã¢ã¯ã»ã¹ã«ã¯Doma 2ã使ããã¨ã«ãã¾ããMaven Compiler Pluginã¯Doma 2ã®è¨å®ã§ããã
Flywayã¯flyway-mysqlãä¾åé¢ä¿ã«è¿½å ãã¾ããflyway-coreã¯flyway-mysqlããæ¨ç§»çã«è¿½å ããã¾ãã
<!-- <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>10.16.0</version> <scope>test</scope> </dependency> --> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-mysql</artifactId> <version>10.16.0</version> <scope>test</scope> </dependency>
Database RiderãJUnit 5ã®ãµãã¼ãæ©è½ã使ãã¾ãã
<dependency> <groupId>com.github.database-rider</groupId> <artifactId>rider-junit5</artifactId> <version>1.44.0</version> <scope>test</scope> </dependency>
ã¡ãªã¿ã«ãrider-junit5ãä¾åãã¦ããJUnit 5ãç¾æç¹ã§5.7.2ã¨å¤ãã£ãã®ã§ãããæ°ããããã¨ä¾å¤ã«ãªã£ãã®ã§Database Riderãä¾åãã¦ãã
ãã¼ã¸ã§ã³ã®JUnit 5ã使ããã¨ã«ãã¾ãâ¦ã
https://github.com/database-rider/database-rider/blob/1.44.0/rider-junit5/pom.xml#L13
ãã¹ã対象ã®ã³ã¼ããæ¸ã
ããã§ã¯ãã¾ãã¯ãã¹ã対象ã®ã³ã¼ããæ¸ãã¾ãããã
è¤æ°ã®ãã¹ãã³ã¼ãããã£ãæ¹ãããããªã¨æã£ãã®ã§ã対象ãè¤æ°ã«ãã¾ãã
ãé¡ãæ¸ç±ã«ãã¨ã³ãã£ãã£ã
src/main/java/org/littlewings/databaserider/Book.java
package org.littlewings.databaserider; import org.seasar.doma.Entity; import org.seasar.doma.Id; @Entity public record Book( @Id String isbn, String title, Integer price ) { }
Doma 2ã§ã¯ã¨ã³ãã£ãã£ã«Recordsã使ããããã§ãã
Daoã
src/main/java/org/littlewings/databaserider/BookDao.java
package org.littlewings.databaserider; import org.seasar.doma.*; import org.seasar.doma.jdbc.Result; import java.util.List; @Dao public interface BookDao { @Insert Result<Book> insert(Book book); @Sql("select /*%expand*/* from book where isbn = /* isbn */'dummy'") @Select Book selectByIsbn(String isbn); @Sql("select /*%expand*/* from book order by price desc") @Select List<Book> selectAllOrderByPriceDesc(); @Delete Result<Book> delete(Book book); }
人ç©ããé¡ã«ãã¨ã³ãã£ãã£ã
src/main/java/org/littlewings/databaserider/Person.java
package org.littlewings.databaserider; import org.seasar.doma.Entity; import org.seasar.doma.Id; @Entity public record Person( @Id Integer id, String firstName, String lastName, Integer age ) { }
Daoã
src/main/java/org/littlewings/databaserider/PersonDao.java
package org.littlewings.databaserider; import org.seasar.doma.*; import org.seasar.doma.jdbc.Result; import java.util.List; @Dao public interface PersonDao { @Insert Result<Person> insert(Person person); @Sql("select /*%expand*/* from person where id = /* id */0") @Select Person selectById(Integer id); @Sql("select /*%expand*/* from person order by id asc") @Select List<Person> selectAllOrderByIdAsc(); @Delete Result<Person> delete(Person person); }
Doma 2ã®Config
ã¯ã©ã¹ããã¼ã¿ãã¼ã¹ã¸ã®æ¥ç¶æ
å ±ã¯ãå¤é¨ããåãåããã¨ã«ãã¾ããã
src/main/java/org/littlewings/databaserider/DomaConfig.java
package org.littlewings.databaserider; import org.seasar.doma.jdbc.Config; import org.seasar.doma.jdbc.Naming; import org.seasar.doma.jdbc.dialect.Dialect; import org.seasar.doma.jdbc.dialect.MysqlDialect; import org.seasar.doma.jdbc.tx.LocalTransactionDataSource; import org.seasar.doma.jdbc.tx.LocalTransactionManager; import org.seasar.doma.jdbc.tx.TransactionManager; import javax.sql.DataSource; public class DomaConfig implements Config { private Dialect dialect; private LocalTransactionDataSource dataSource; private TransactionManager transactionManager; public static DomaConfig from(String url, String username, String password) { return new DomaConfig(url, username, password); } private DomaConfig(String url, String username, String password) { dialect = new MysqlDialect(); dataSource = new LocalTransactionDataSource(url, username, password); transactionManager = new LocalTransactionManager(dataSource.getLocalTransaction(getJdbcLogger())); } @Override public DataSource getDataSource() { return dataSource; } @Override public Dialect getDialect() { return dialect; } @Override public TransactionManager getTransactionManager() { return transactionManager; } @Override public Naming getNaming() { return Naming.SNAKE_LOWER_CASE; } }
Flywayã®ãã¤ã°ã¬ã¼ã·ã§ã³ã¯src/main/resources
å´ã«ç½®ããã¨ã«ãã¾ãã
src/main/resources/db/migration/V1__create_book_table.sql
create table book ( isbn varchar(14), title varchar(255), price int, primary key(isbn) );
src/main/resources/db/migration/V2__create_person_table.sql
create table person ( id int, first_name varchar(255), last_name varchar(255), age int, primary key(id) );
Flywayã®ãã¤ã°ã¬ã¼ã·ã§ã³ã®é
ç½®å
ã¯ãããã©ã«ãã§ã¯ã©ã¹ãã¹ä¸ã®db/migration
ã®ãããªã®ã§ããã«åããããã¨æãã¾ãã
Configuration - flyway-core 10.16.0 javadoc
ãã¹ãã³ã¼ããæ¸ã
ã§ã¯ããã¹ãã³ã¼ããæ¸ãã¦ããã¾ãã
ã¾ãã¯Database Riderã®è¨å®ãã¡ã¤ã«ã
src/test/resources/dbunit.yml
cacheConnection: false properties: caseSensitiveTableNames: true connectionConfig: url: "jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin" user: "kazuhira" password: "password"
æ¸ç±ã®ãã¹ããã¼ã¿ã»ããã¢ããç¨ã®ãã¼ã¿ã»ããã
src/test/resources/dataset/bookSetup.yml
book: - isbn: "978-4798161488" title: "MySQLå¾¹åºå ¥é 第4ç MySQL 8.0対å¿" price: 4180 - isbn: "978-4297141844" title: "MySQLéç¨ã»ç®¡çï¼»å®è·µï¼½å ¥é ãå®å ¨ãã¤é«éã«ãã¼ã¿ãæ±ãå é¨æ§é ã»åä½åçãå¦ã¶" price: 3080 - isbn: "978-4621303252" title: "Effective Java 第3ç" price: 4400
æ¸ç±ã®ã¢ãµã¼ã·ã§ã³ç¨ã®ãã¼ã¿ã»ããã
src/test/resources/dataset/bookExpected.yml
book: - isbn: "978-4798161488" title: "MySQLå¾¹åºå ¥é 第4ç MySQL 8.0対å¿" price: 4180 - isbn: "978-4297141844" title: "MySQLéç¨ã»ç®¡çï¼»å®è·µï¼½å ¥é ãå®å ¨ãã¤é«éã«ãã¼ã¿ãæ±ãå é¨æ§é ã»åä½åçãå¦ã¶" price: 3080 - isbn: "978-4621303252" title: "Effective Java 第3ç" price: 4400 - isbn: "978-4839981723" title: "åä½ãã¹ãã®èãæ¹/使ãæ¹" price: 4488
ãã¡ãã使ã£ããã¹ãã³ã¼ãã
src/test/java/org/littlewings/databaserider/BookDaoTest.java
package org.littlewings.databaserider; import com.github.database.rider.core.api.dataset.DataSet; import com.github.database.rider.core.api.dataset.ExpectedDataSet; import com.github.database.rider.junit5.api.DBRider; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @DBRider class BookDaoTest { private DomaConfig config = DomaConfig.from( "jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin", "kazuhira", "password" ); private BookDao bookDao = new BookDaoImpl(config); @Test @DataSet("dataset/bookSetup.yml") void select() { config .getTransactionManager() .required(() -> { Book book = bookDao.selectByIsbn("978-4798161488"); assertThat(book.title()).isEqualTo("MySQLå¾¹åºå ¥é 第4ç MySQL 8.0対å¿"); assertThat(bookDao.selectAllOrderByPriceDesc()).hasSize(3); }); } @Test @DataSet("dataset/bookSetup.yml") @ExpectedDataSet(value = "dataset/bookExpected.yml", orderBy = "isbn") void insert() { config .getTransactionManager() .required(() -> { Book book = new Book( "978-4839981723", "åä½ãã¹ãã®èãæ¹/使ãæ¹", 4488 ); bookDao.insert(book); }); } }
è¤æ°ã®ãã¹ãã¡ã½ããããã£ãæ¹ãããããªã¨æã£ããããã§ãå®è£ èªä½ã¯ã¨ã¦ãåç´ã§ãã
ãã¼ã¿ãã¼ã¹ã®æ¥ç¶æ å ±ããã¼ãã³ã¼ããã¦ããã®ã¯ãä»åã¯ç½®ãã¦ããã¾ãâ¦ã
private DomaConfig config = DomaConfig.from( "jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin", "kazuhira", "password" );
ã§ããã®ãã¹ãå®è¡åã«Flywayãã¤ã°ã¬ã¼ã·ã§ã³ãå®è¡ãããã¨ããã§ããã¾ãDatabase Riderã¨åãæ¥ç¶æ å ±ã使ãããã¨ããã§ãã
ã¨ããããã§ãä»åã¯ãããªJUnit 5 Extensionãä½æã@BeforeAll
ã®ã¿ã¤ãã³ã°ã§Flywayãã¤ã°ã¬ã¼ã·ã§ã³ãå®è¡ãã¾ãã
src/test/java/org/littlewings/databaserider/FlywayExtension.java
package org.littlewings.databaserider; import com.github.database.rider.core.configuration.ConnectionConfig; import com.github.database.rider.core.configuration.DBUnitConfig; import org.flywaydb.core.Flyway; import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.ExtensionContext; public class FlywayExtension implements BeforeAllCallback { @Override public void beforeAll(ExtensionContext context) throws Exception { DBUnitConfig dbUnitConfig = DBUnitConfig.fromGlobalConfig(); ConnectionConfig connectionConfig = dbUnitConfig.getConnectionConfig(); Flyway flyway = Flyway .configure() .dataSource(connectionConfig.getUrl(), connectionConfig.getUser(), connectionConfig.getPassword()) .load(); flyway.migrate(); } }
Database Riderã®è¨å®æ
å ±ã¯ãDBUnitConfig
ããåå¾ãã¦ãã¾ãã
ãã£ãããªã®ã§ã¡ã¿ã¢ããã¼ã·ã§ã³ãä½ã£ã¦ããã¾ãããã
src/test/java/org/littlewings/databaserider/FlywayMigration.java
package org.littlewings.databaserider; import org.junit.jupiter.api.extension.ExtendWith; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @ExtendWith(FlywayExtension.class) public @interface FlywayMigration { }
ãããå ã»ã©ã®ã¯ã©ã¹ã«ä»ä¸ãã¾ãã
@FlywayMigration @DBRider class BookDaoTest {
人ç©ã®æ¹ããã¹ããç¨æãã¾ãããã
ãã¹ããã¼ã¿ã»ããã¢ããç¨ã®ãã¼ã¿ã»ããã
src/test/resources/dataset/personSetup.yml
person: - id: 1 first_name: "ãµã¶ã¨" last_name: "ãã°ç°" age: 24 - id: 2 first_name: "ãã¹ãª" last_name: "ãã°ç°" age: 28
ã¢ãµã¼ã·ã§ã³ç¨ã®ãã¼ã¿ã»ããã
src/test/resources/dataset/personExpected.yml
person: - id: 1 first_name: "ãµã¶ã¨" last_name: "ãã°ç°" age: 24 - id: 2 first_name: "ãã¹ãª" last_name: "ãã°ç°" age: 28 - id: 3 first_name: "ã«ããª" last_name: "磯é" age: 11
ãã¹ãã³ã¼ãã
src/test/java/org/littlewings/databaserider/PersonDaoTest.java
package org.littlewings.databaserider; import com.github.database.rider.core.api.dataset.DataSet; import com.github.database.rider.core.api.dataset.ExpectedDataSet; import com.github.database.rider.junit5.api.DBRider; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @FlywayMigration @DBRider class PersonDaoTest { private DomaConfig config = DomaConfig.from( "jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin", "kazuhira", "password" ); private PersonDao personDao = new PersonDaoImpl(config); @Test @DataSet("dataset/personSetup.yml") void select() { config .getTransactionManager() .required(() -> { Person person = personDao.selectById(1); assertThat(person.firstName()).isEqualTo("ãµã¶ã¨"); assertThat(person.lastName()).isEqualTo("ãã°ç°"); assertThat(personDao.selectAllOrderByIdAsc()).hasSize(2); }); } @Test @DataSet("dataset/personSetup.yml") @ExpectedDataSet(value = "dataset/personExpected.yml", orderBy = "id") void insert() { config .getTransactionManager() .required(() -> { Person person = new Person(3, "ã«ããª", "磯é", 11); personDao.insert(person); }); } }
æºåãã§ããã®ã§ããã¹ããå®è¡ãã¾ãããã
$ mvn test
æåã«å®è¡ããããã¹ãã¯ã©ã¹ã®æã«Flywayãã¤ã°ã¬ã¼ã·ã§ã³ãå®è¡ããã¾ããã
[INFO] Running org.littlewings.databaserider.BookDaoTest 7æ 28, 2024 10:23:11 åå¾ org.flywaydb.core.FlywayExecutor execute æ å ±: Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) 7æ 28, 2024 10:23:11 åå¾ org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory allAppliedMigrations æ å ±: Schema history table `practice`.`flyway_schema_history` does not exist yet 7æ 28, 2024 10:23:11 åå¾ org.flywaydb.core.internal.command.DbValidate validate æ å ±: Successfully validated 2 migrations (execution time 00:00.028s) 7æ 28, 2024 10:23:11 åå¾ org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory create æ å ±: Creating Schema History table `practice`.`flyway_schema_history` ... 7æ 28, 2024 10:23:12 åå¾ org.flywaydb.core.internal.command.DbMigrate migrateGroup æ å ±: Current version of schema `practice`: << Empty Schema >> 7æ 28, 2024 10:23:12 åå¾ org.flywaydb.core.internal.command.DbMigrate doMigrateGroup æ å ±: Migrating schema `practice` to version "1 - create book table" 7æ 28, 2024 10:23:12 åå¾ org.flywaydb.core.internal.command.DbMigrate doMigrateGroup æ å ±: Migrating schema `practice` to version "2 - create person table" 7æ 28, 2024 10:23:12 åå¾ org.flywaydb.core.internal.command.DbMigrate logSummary æ å ±: Successfully applied 2 migrations to schema `practice`, now at version v2 (execution time 00:00.264s)
次ã®ãã¹ãã¯ã©ã¹ã®å®è¡æã«ã¯ããã§ã«Flywayãã¤ã°ã¬ã¼ã·ã§ã³ã¯å®è¡æ¸ã¿ãªãã¨ã表示ããã¾ãã
[INFO] Running org.littlewings.databaserider.PersonDaoTest 7æ 28, 2024 10:23:13 åå¾ org.flywaydb.core.FlywayExecutor execute æ å ±: Database: jdbc:mysql://172.17.0.2:3306/practice (MySQL 8.0) 7æ 28, 2024 10:23:13 åå¾ org.flywaydb.core.internal.command.DbValidate validate æ å ±: Successfully validated 2 migrations (execution time 00:00.010s) 7æ 28, 2024 10:23:13 åå¾ org.flywaydb.core.internal.command.DbMigrate migrateGroup æ å ±: Current version of schema `practice`: 2 7æ 28, 2024 10:23:13 åå¾ org.flywaydb.core.internal.command.DbMigrate logSummary æ å ±: Schema `practice` is up to date. No migration necessary.
ã¨ããããã§ãDatabase Riderã®å®è¡åã«Flywayãã¤ã°ã¬ã¼ã·ã§ã³ãå®è¡ãããããªJUnit 5 Extensionãæ¸ãã¾ãããOKã§ããã
ãªãã±ï¼ Database Riderã®ãã¼ã¿ãã¼ã¹ã¢ã¯ã»ã¹å ããã¹ãã³ã¼ãããè¨å®ãã
ä»åã¯Flywayã®ãã¼ã¿ãã¼ã¹ã¢ã¯ã»ã¹å
ãDatabase Riderã®è¨å®ã«å¯ãã¾ãããããã¹ãã³ã¼ãã§ä½¿ãæ¥ç¶æ
å ±ãDatabase Riderã«
æå®ãããæããããããªæ°ããã¾ãã
ããããæã¯ConnectionHolder
ã¨ãããã®ã使ãã°ããããã§ãã
ãã¹ãã¯ã©ã¹ã®ãã£ã¼ã«ãã«å®ç¾©ãã¦ããã¨ã親ã¯ã©ã¹ã¾ã§å«ãã¦æ¢ãã¦ãããã¿ããã§ããã
Database Riderã®è¨å®ãã¡ã¤ã«ããã¯ããã¼ã¿ãã¼ã¹æ¥ç¶å ã®æ å ±ãã³ã¡ã³ãã¢ã¦ããã¾ãã
src/test/resources/dbunit.yml
cacheConnection: false properties: caseSensitiveTableNames: true # connectionConfig: # url: "jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin" # user: "kazuhira" # password: "password"
ãã¹ãã¯ã©ã¹ã«ConnectionHolder
åã®ãã£ã¼ã«ããå®ç¾©ããããã¯Doma 2ã®Config
ããJDBCã®Connection
ãåå¾ããããã«ãã¾ãã
src/test/java/org/littlewings/databaserider/BookDaoTest.java
package org.littlewings.databaserider; import com.github.database.rider.core.api.connection.ConnectionHolder; import com.github.database.rider.core.api.dataset.DataSet; import com.github.database.rider.core.api.dataset.ExpectedDataSet; import com.github.database.rider.junit5.api.DBRider; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @FlywayMigration @DBRider class BookDaoTest { private DomaConfig config = DomaConfig.from( "jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin", "kazuhira", "password" ); private ConnectionHolder connectionHolder = () -> config.getDataSource().getConnection(); private BookDao bookDao = new BookDaoImpl(config); ããã¹ãã¡ã½ããã¯çç¥ã }
ããã²ã¨ã¤ã®ãã¹ãã¯ã©ã¹ãåæ§ã«ã
src/test/java/org/littlewings/databaserider/PersonDaoTest.java
package org.littlewings.databaserider; import com.github.database.rider.core.api.connection.ConnectionHolder; import com.github.database.rider.core.api.dataset.DataSet; import com.github.database.rider.core.api.dataset.ExpectedDataSet; import com.github.database.rider.junit5.api.DBRider; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @FlywayMigration @DBRider class PersonDaoTest { private DomaConfig config = DomaConfig.from( "jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin", "kazuhira", "password" ); private ConnectionHolder connectionHolder = () -> config.getDataSource().getConnection(); private PersonDao personDao = new PersonDaoImpl(config); ããã¹ãã¡ã½ããã¯çç¥ã }
Flywayç¨ã®JUnit 5 Extensionã®æ¹ã¯â¦ä»æ¹ããªãã®ã§ä»åã¯ãã¼ãã³ã¼ãã«ãã¾ããâ¦ã
src/test/java/org/littlewings/databaserider/FlywayExtension.java
package org.littlewings.databaserider; import com.github.database.rider.core.configuration.ConnectionConfig; import com.github.database.rider.core.configuration.DBUnitConfig; import org.flywaydb.core.Flyway; import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.ExtensionContext; public class FlywayExtension implements BeforeAllCallback { @Override public void beforeAll(ExtensionContext context) throws Exception { /* DBUnitConfig dbUnitConfig = DBUnitConfig.fromGlobalConfig(); ConnectionConfig connectionConfig = dbUnitConfig.getConnectionConfig(); */ Flyway flyway = Flyway .configure() //.dataSource(connectionConfig.getUrl(), connectionConfig.getUser(), connectionConfig.getPassword()) .dataSource( "jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin", "kazuhira", "password" ) .load(); flyway.migrate(); } }
ã¢ããªã±ã¼ã·ã§ã³ããã¹ãã³ã¼ãã®è¨å®ããåãããã«ãããã§ããããã®ãããã¯ã
ãã¹ãå®è¡æã«Flywayãã¤ã°ã¬ã¼ã·ã§ã³ãå®è¡ãããæ§åã¯å ã»ã©ã¨åããªã®ã§ãçç¥ãã¾ãã
ãããã«
Database Riderã®å®è¡åã«Flywayãã¤ã°ã¬ã¼ã·ã§ã³ãè¡ãJUnit 5 Extensionãæ¸ãã¦ã¿ã¾ããã
Flywayèªä½ããµãã¼ããã¦ãããã¬ã¼ã ã¯ã¼ã¯ãªã©ã使ã£ã¦ãã¡ãã«ä»»ãããããªãã¨ãå¤ããããªæ°ã¯ãã¾ãããã¡ãã£ã¨ããå°ãã¿çã«
æ¼ããã¦ããã¦ãããã®ããªãã¨æãã¾ãã
ãããã£ãç°å¢ä»¥å¤ã§Flywayã使ãæã®ã¢ã¤ãã¢ã®ã²ã¨ã¤ã¨ãã¦ã