Spring Boot ã® Webã¢ããªã§ãFlyway ã使ã£ã¦ãã¼ã¿ãã¼ã¹ã®ãã¤ã°ã¬ã¼ã·ã§ã³ãããæ¹æ³ãæ¸ãã¦ããã¾ãã
ãã¼ã¸ã§ã³
åä½ç¢ºèªã§ä½¿ç¨ãã製åã®ãã¼ã¸ã§ã³ã¯ä»¥ä¸ã®éãã§ãã
- SpringBoot 3.0.1
- Java 17
- Maven 3.8.6
- PostgreSQL 15.1
- pgJDBC 42.5.1ï¼JDBC Driverï¼
1. ãã«ããã¡ã¤ã«ã®ä½æ
ããã¸ã§ã¯ãã®ãã©ã«ã spring-flyway
ãä½æãã¦ããã®ä¸ã« Maven ã®ãã«ããã¡ã¤ã«ãä½æãã¾ãã
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.example</groupId> <artifactId>spring-flyway</artifactId> <version>1.0.0</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
以ä¸ã®ã©ã¤ãã©ãªãä¾åæ§ã«è¿½å ããã¨ãã¢ããªã®èµ·åæã«ãã¤ã°ã¬ã¼ã·ã§ã³ãå®è¡ããã¾ãã
2. SQLãã¡ã¤ã«ã®ä½æ
ãã¤ã°ã¬ã¼ã·ã§ã³ç¨ã® SQLãã¡ã¤ã«ãä½æãã¾ãã
src/main/resources/db/migration/V1__Create_student_table.sql
CREATE TABLE student ( id bigserial PRIMARY KEY, name varchar(60) NOT NULL, update_time timestamp NOT NULL DEFAULT current_timestamp, create_time timestamp NOT NULL DEFAULT current_timestamp );
2.1. ãã¡ã¤ã«ã®é ç½®å ´æ
SQLãã¡ã¤ã«ã¯ãã¯ã©ã¹ãã¹é
ä¸ã® db/migration
ãã£ã¬ã¯ããªã«é
ç½®ãã¾ãã
2.2. ãã¡ã¤ã«ã®å½åè¦å
SQLãã¡ã¤ã«ã®å½åè¦åã¯ä»¥ä¸ã®éãã§ãã
V{ãã¼ã¸ã§ã³çªå·}__{ä»»æã®æåå}.sql
ãã¼ã¸ã§ã³çªå·ã¨æååã®éã¯ãåè§ã®ã¢ã³ãã¼ã¹ã³ã¢ï¼ã¤ã§ãã
3. è¨å®ãã¡ã¤ã«ã®ä½æ
PostgreSQL ã«æ¥ç¶ããããã®è¨å®ãã¡ã¤ã«ãä½æãã¾ãã
src/main/resources/application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/flyway spring.datasource.username=usr spring.datasource.password=pass
4. èµ·åã¯ã©ã¹
Spring Boot ã®ã¢ããªãèµ·åããã¯ã©ã¹ãä½æãã¾ãã
src/main/java/org/example/SpringApp.java
package org.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringApp { public static void main(String[] args) { SpringApplication.run(SpringApp.class, args); } }
5. ã¢ããªã®èµ·å
èµ·åã¯ã©ã¹ãå®è¡ãããã以ä¸ã®ã³ãã³ãã§ã¢ããªãèµ·åãã¾ãã
mvn spring-boot:run
6. DBã®ç¢ºèª
PostgreSQL ã®ã³ãã³ã \d
ã§ããã¼ãã«ãä½æããã¦ãããã¨ã確èªãã¾ããã