Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Spring Security
- tasklet
- MFA
- SWAGGER
- preventdefault
- 리액트
- openapi3
- Spring Batch
- stopPropogation
- AuthenticatoinProvide
- Filter
- JavaScript
- UsernamePasswordAuthenticationFilter
- Spring REST Docs
- SpringBoot
- vue
- cloud native
- Crawling
- Flyway
- T-OTP
- REACT
- axios
- gradle
- Reduxpender
- vuejs
- MSA
- SpringRESTDocs
- OpenStack
- cheerio
- Pender
Archives
- Today
- Total
Miracle Morning, LHWN
14-3. Flyway 사용해보기 - Java-based Mirgrations 본문
# 파일명을 정의할 때 역시 Flyway 가 인식할 수 있도록 명명규칙을 따라주어야 하며, 여기선 'V3__bulk_update_books.java' 로 지정하였다. 이때 경로는 src/java 하위에 바로 db/migration 경로를 생성해주었고 그 안에 java 파일을 위치한다.
BaseJavaMigration 을 상속받아 migrate 함수를 Override 하여 사용해주면 된다.
createStatement 를 통해 쿼리를 생성할 수 있는 객체를 만들고, SELECT 를 통해 가져온 rows 를 하나하나 while 문으로 돌려가며
UPDATE 쿼리를 날려주었다.
package db.migration;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import java.sql.ResultSet;
import java.sql.Statement;
public class V3__bulk_update_books extends BaseJavaMigration {
@Override
public void migrate(Context context) throws Exception {
try (Statement select = context.getConnection().createStatement()) {
try (ResultSet rows = select.executeQuery("SELECT id FROM books ORDER BY id")) {
while (rows.next()) {
int id = rows.getInt(1);
String nameToChange = "LHWN2";
try (Statement update = context.getConnection().createStatement()) {
update.execute("UPDATE books SET name='" + nameToChange + "'WHERE id=" + id);
}
}
}
}
}
}
출처 : https://fastcampus.co.kr/courses/204729/clips/
패스트캠퍼스 온라인 강의 - 스프링 아카데미아 15개 영상강의 코스(5월)
fastcampus.co.kr
'IT 기술 > [JAVA] Spring Boot' 카테고리의 다른 글
16-2. Spring REST Docs - 실제 사용해보기 (0) | 2021.05.29 |
---|---|
16-1. Spring REST Docs - 실제 사용해보기 (0) | 2021.05.28 |
15. Spring REST Docs (0) | 2021.05.26 |
14-2. Flyway 사용해보기 - Flyway API (Java) (0) | 2021.05.25 |
14-1. Flyway 사용해보기 - Gradle Usage (0) | 2021.05.25 |
Comments