Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : coopshop 기존 데이터가 없는 로컬에서도 동작하도록 flyway 쿼리 수정 #957

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public record AdminCoopSemesterResponse(
@JsonFormat(pattern = "yyyy-MM-dd")
LocalDate toDate,

@Schema(description = "업데이트 일자", example = "2024-09-01 14:02:48", requiredMode = REQUIRED)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime updatedAt,

@Schema(description = "생협 매장 정보 리스트", requiredMode = REQUIRED)
List<InnerCoopShop> coopShops
) {
Expand All @@ -40,6 +44,7 @@ public static AdminCoopSemesterResponse from(CoopSemester coopSemester) {
coopSemester.getSemester(),
coopSemester.getFromDate(),
coopSemester.getToDate(),
coopSemester.getUpdatedAt(),
coopSemester.getCoopShops().stream().map(InnerCoopShop::from).toList()
);
}
Expand All @@ -62,11 +67,7 @@ public record InnerCoopShop(
String location,

@Schema(example = "공휴일 휴무", description = "생협 매장 특이사항")
String remarks,

@JsonFormat(pattern = "yyyy-MM-dd")
@Schema(example = "2024-06-26", description = "학식 운영시간 업데이트 날짜", requiredMode = REQUIRED)
LocalDateTime updatedAt
String remarks
) {

public static InnerCoopShop from(CoopShop coopShop) {
Expand All @@ -78,8 +79,7 @@ public static InnerCoopShop from(CoopShop coopShop) {
.toList(),
coopShop.getPhone(),
coopShop.getLocation(),
coopShop.getRemarks(),
coopShop.getUpdatedAt()
coopShop.getRemarks()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.data.domain.Page;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import in.koreatech.koin.domain.coopshop.model.CoopSemester;
Expand Down Expand Up @@ -35,18 +37,32 @@ public record AdminCoopSemestersResponse(

@JsonNaming(value = SnakeCaseStrategy.class)
public record InnerSemester(
@Schema(description = "학기 고유 id", example = "1", requiredMode = REQUIRED)
Integer id,

@Schema(description = "학기", example = "24-2학기", requiredMode = REQUIRED)
String semester,

@Schema(description = "학기 기간 시작일", example = "2024-09-02", requiredMode = REQUIRED)
@JsonFormat(pattern = "yyyy-MM-dd")
LocalDate fromDate,
LocalDate toDate

@Schema(description = "학기 기간 마감일", example = "2024-12-20", requiredMode = REQUIRED)
@JsonFormat(pattern = "yyyy-MM-dd")
LocalDate toDate,

@Schema(description = "업데이트 일자", example = "2024-09-01 14:02:48", requiredMode = REQUIRED)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime updatedAt
) {

public static InnerSemester from(CoopSemester coopSemester) {
return new InnerSemester(
coopSemester.getId(),
coopSemester.getSemester(),
coopSemester.getFromDate(),
coopSemester.getToDate()
coopSemester.getToDate(),
coopSemester.getUpdatedAt()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public record CoopShopsResponse(
@JsonFormat(pattern = "yyyy-MM-dd")
LocalDate toDate,

@Schema(description = "업데이트 일자", example = "2024-09-02 14:02:48", requiredMode = REQUIRED)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime updatedAt,

@Schema(example = "하계방학", description = "운영 학기", requiredMode = REQUIRED)
List<InnerCoopShop> coopShops
) {
Expand All @@ -37,6 +41,7 @@ public static CoopShopsResponse from(CoopSemester coopSemester) {
coopSemester.getSemester(),
coopSemester.getFromDate(),
coopSemester.getToDate(),
coopSemester.getUpdatedAt(),
coopSemester.getCoopShops().stream()
.map(InnerCoopShop::from)
.toList()
Expand All @@ -61,11 +66,7 @@ public record InnerCoopShop(
String location,

@Schema(example = "공휴일 휴무", description = "생협 매장 특이사항")
String remarks,

@JsonFormat(pattern = "yyyy-MM-dd")
@Schema(example = "2024-06-26", description = "학식 운영시간 업데이트 날짜", requiredMode = REQUIRED)
LocalDateTime updatedAt
String remarks
) {

public static InnerCoopShop from(CoopShop coopShop) {
Expand All @@ -77,8 +78,7 @@ public static InnerCoopShop from(CoopShop coopShop) {
.toList(),
coopShop.getPhone(),
coopShop.getLocation(),
coopShop.getRemarks(),
coopShop.getUpdatedAt()
coopShop.getRemarks()
);
}

Expand Down
100 changes: 64 additions & 36 deletions src/main/resources/db/migration/V77__insert_coop_shop_semester.sql
Original file line number Diff line number Diff line change
@@ -1,40 +1,68 @@
ALTER TABLE coop_semester AUTO_INCREMENT = 1;
ALTER TABLE coop_shop AUTO_INCREMENT = 2;
ALTER TABLE coop_opens AUTO_INCREMENT = 7;

insert into `coop_semester` (`semester`, `from_date`, `to_date`, `is_applied`)
values ('24-2학기', '2024-09-02', '2024-12-20', 1);

INSERT INTO `coop_shop` (`name`, `phone`, `location`, `remarks`, `semester_id`)
VALUES
('복지관식당', '041-560-1778', 'NULL', '능수관', 1),
('대즐', '041-560-1779', '복지관 1층', '배달 서비스, 공·일요일 미운영', 1),
('서점', '041-560-1756', '복지관 1층', '점심시간 12:00 - 13:00', 1),
('세탁소', '041-552-1489', '학생회관 2층', 'NULL', 1),
('복사실', '041-560-1093', '학생회관 2층', '점심시간 11:30 - 12:30', 1),
('복지관 참빛관 편의점', '041-560-1093', '복지관 1층, 참빛관 1층', '복지관 임시운영 별도공지', 1),
('미용실', '041-560-1769', '학생회관 1층', '예약제운영', 1),
('오락실', '041-560-1472', '학생회관 1층', 'NULL', 1);

update `coop_shop` set `semester_id` = 1 where `id` = 1;
update `coop_opens` set `day_of_week` = 'WEEKDAYS' where `id` = 1;
update `coop_opens` set `day_of_week` = 'WEEKDAYS' where `id` = 2;
update `coop_opens` set `day_of_week` = 'WEEKDAYS' where `id` = 3;
update `coop_opens` set `day_of_week` = 'WEEKEND' where `id` = 4;
update `coop_opens` set `day_of_week` = 'WEEKEND' where `id` = 5;
update `coop_opens` set `day_of_week` = 'WEEKEND' where `id` = 6;

INSERT INTO coop_opens (`coop_shop_id`, `type`, `day_of_week`, `open_time`, `close_time`)
VALUES
(2, '점심', 'WEEKDAYS', '11:40', '13:30'),
(2, '점심', 'WEEKEND', '미운영', '미운영'),
(3, 'NULL', 'WEEKDAYS', '08:30', '21:00'),
(3, 'NULL', 'SATURDAY', '11:00', '18:00'),
(4, 'NULL', 'WEEKDAYS', '09:00', '18:00'),
(4, 'NULL', 'WEEKEND', '휴점', '휴점'),
(5, 'NULL', 'WEEKDAYS', '11:30', '18:00'),
(5, 'NULL', 'WEEKEND', '휴점', '휴점'),
(6, 'NULL', 'WEEKDAYS', '08:30', '18:00'),
(6, 'NULL', 'WEEKEND', '휴점', '휴점'),
(7, 'NULL', 'WEEKDAYS', '24시간', '24시간'),
(7, 'NULL', 'WEEKEND', '24시간', '24시간'),
(8, 'NULL', 'WEEKDAYS', '09:30', '17:00'),
(8, 'NULL', 'WEEKEND', '휴점', '휴점'),
(9, 'NULL', 'WEEKDAYS', '24시간', '24시간'),
(9, 'NULL', 'WEEKEND', '24시간', '24시간');
VALUES ('복지관식당', '041-560-1778', '복지관 2층', '능수관', 1);
INSERT INTO coop_opens (`coop_shop_id`, `type`, `day_of_week`, `open_time`, `close_time`)
VALUES (last_insert_id(), '점심', 'WEEKDAYS', '11:40', '13:30'),
(last_insert_id(), '점심', 'WEEKEND', '미운영', '미운영');

INSERT INTO `coop_shop` (`name`, `phone`, `location`, `remarks`, `semester_id`)
VALUES ('대즐', '041-560-1779', '복지관 1층', '배달 서비스, 공·일요일 미운영', 1);
INSERT INTO coop_opens (`coop_shop_id`, `type`, `day_of_week`, `open_time`, `close_time`)
VALUES (last_insert_id(), NULL, 'WEEKDAYS', '08:30', '21:00'),
(last_insert_id(), NULL, 'SATURDAY', '11:00', '18:00');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A

참조 id를 알 수 없는 경우 어떻게 넣어줄 수 있나 고민했었는데
last_insert_id() 이런 함수가 있었네요


INSERT INTO `coop_shop` (`name`, `phone`, `location`, `remarks`, `semester_id`)
VALUES ('서점', '041-560-1756', '복지관 1층', '점심시간 12:00 - 13:00', 1);
INSERT INTO coop_opens (`coop_shop_id`, `type`, `day_of_week`, `open_time`, `close_time`)
VALUES (last_insert_id(), NULL, 'WEEKDAYS', '09:00', '18:00'),
(last_insert_id(), NULL, 'WEEKEND', '휴점', '휴점');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C

insert 구문이 전부 동일한 형태에 value만 다른 것 같은데, 기존처럼 단일 insert문으로 묶어버리는 건 어떻게 생각하시나요??
LAST_INSERT_ID()의 정확한 반환값은 마지막 INSERT 구문의 최초 삽입 id 으로 알고 있습니다.
단일 데이터 삽입 시 마지막으로 삽입된 데이터 id를 반환하지만 단일 INSERT문으로 다중 데이터 삽입 시 삽입된 데이터 중 첫 id값을 반환합니다. 이 특성을 활용하면 기존과 같이 insert문 두개만으로 모든 데이터를 삽입할 수 있어 보입니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coop_shopcoop_opens로 insert문을 묶고, coop_opens가 참조하는 외래키는 last_insert_id(), last_insert_id() + 1, last_insert_id() + 2 ... 와 같이 묶을 수 있다는 말 맞을까요?

Copy link
Collaborator

@songsunkook songsunkook Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용자 정의 변수를 사용하는 방향을 생각했습니다.

예시

SET @FIRST_ID = LAST_INSERT_ID();

INSERT INTO coop_opens (`coop_shop_id`, `type`, `day_of_week`, `open_time`, `close_time`)
    VALUES
    (@FIRST_ID, '점심', 'WEEKDAYS', '11:40', '13:30'),
    (@FIRST_ID, '점심', 'WEEKEND', '미운영', '미운영'),
    (@FIRST_ID + 1, 'NULL', 'WEEKDAYS', '08:30', '21:00'),
    (@FIRST_ID + 1, 'NULL', 'SATURDAY', '11:00', '18:00'),

참고자료


INSERT INTO `coop_shop` (`name`, `phone`, `location`, `remarks`, `semester_id`)
VALUES ('세탁소', '041-552-1489', '학생회관 2층', NULL, 1);
INSERT INTO coop_opens (`coop_shop_id`, `type`, `day_of_week`, `open_time`, `close_time`)
VALUES (last_insert_id(), NULL, 'WEEKDAYS', '11:30', '18:00'),
(last_insert_id(), NULL, 'WEEKEND', '휴점', '휴점');

INSERT INTO `coop_shop` (`name`, `phone`, `location`, `remarks`, `semester_id`)
VALUES ('복사실', '041-560-1093', '학생회관 2층', '점심시간 11:30 - 12:30', 1);
INSERT INTO coop_opens (`coop_shop_id`, `type`, `day_of_week`, `open_time`, `close_time`)
VALUES (last_insert_id(), NULL, 'WEEKDAYS', '08:30', '18:00'),
(last_insert_id(), NULL, 'WEEKEND', '휴점', '휴점');

INSERT INTO `coop_shop` (`name`, `phone`, `location`, `remarks`, `semester_id`)
VALUES ('복지관 참빛관 편의점', '041-560-1093', '복지관 1층, 참빛관 1층', '복지관 임시운영 별도공지', 1);
INSERT INTO coop_opens (`coop_shop_id`, `type`, `day_of_week`, `open_time`, `close_time`)
VALUES (last_insert_id(), NULL, 'WEEKDAYS', '24시간', '24시간'),
(last_insert_id(), NULL, 'WEEKEND', '24시간', '24시간');

INSERT INTO `coop_shop` (`name`, `phone`, `location`, `remarks`, `semester_id`)
VALUES ('미용실', '041-560-1769', '학생회관 1층', '예약제운영', 1);
INSERT INTO coop_opens (`coop_shop_id`, `type`, `day_of_week`, `open_time`, `close_time`)
VALUES (last_insert_id(), NULL, 'WEEKDAYS', '09:30', '17:00'),
(last_insert_id(), NULL, 'WEEKEND', '휴점', '휴점');

INSERT INTO `coop_shop` (`name`, `phone`, `location`, `remarks`, `semester_id`)
VALUES ('오락실', '041-560-1472', '학생회관 1층', NULL, 1);
INSERT INTO coop_opens (`coop_shop_id`, `type`, `day_of_week`, `open_time`, `close_time`)
VALUES (last_insert_id(), NULL, 'WEEKDAYS', '24시간', '24시간'),
(last_insert_id(), NULL, 'WEEKEND', '24시간', '24시간');

UPDATE coop_shop
SET semester_id = 1
WHERE name = '학생식당';

UPDATE coop_opens
SET day_of_week = 'WEEKDAYS'
WHERE coop_shop_id = 1
AND day_of_week = '평일';

UPDATE coop_opens
SET day_of_week = 'WEEKEND'
WHERE coop_shop_id = 1
AND day_of_week = '주말';
11 changes: 5 additions & 6 deletions src/test/java/in/koreatech/koin/acceptance/CoopShopApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void setUp() {
"semester": "23-2학기",
"from_date": "2023-09-02",
"to_date": "2023-12-20",
"updated_at" : "2024-01-15 12:00:00",
"coop_shops": [
{
"id": 1,
Expand All @@ -58,17 +59,15 @@ void setUp() {
],
"phone": "041-000-0000",
"location": "학생회관 1층",
"remarks": "공휴일 휴무",
"updated_at" : "2024-01-15"
"remarks": "공휴일 휴무"
},
{
"id": 2,
"name": "세탁소",
"opens": [],
"phone": "041-000-0000",
"location": "학생회관 2층",
"remarks": "연중무휴",
"updated_at" : "2024-01-15"
"remarks": "연중무휴"
}
]
}
Expand Down Expand Up @@ -116,6 +115,7 @@ void setUp() {
"semester": "23-겨울학기",
"from_date": "2023-12-21",
"to_date": "2024-02-28",
"updated_at" : "2024-01-15 12:00:00",
"coop_shops": [
{
"id": 3,
Expand All @@ -130,8 +130,7 @@ void setUp() {
],
"phone": "041-000-0000",
"location": "학생회관 2층",
"remarks": "연중무휴",
"updated_at" : "2024-01-15"
"remarks": "연중무휴"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ void setUp() {
"semester": "23-2학기",
"from_date": "2023-09-02",
"to_date": "2023-12-20",
"updated_at" : "2024-01-15 12:00:00"
},
{
"id": 2,
"semester": "23-겨울학기",
"from_date": "2023-12-21",
"to_date": "2024-02-28",
"updated_at" : "2024-01-15 12:00:00"
}
]
}
Expand All @@ -87,6 +89,7 @@ void setUp() {
"semester": "23-2학기",
"from_date": "2023-09-02",
"to_date": "2023-12-20",
"updated_at" : "2024-01-15 12:00:00",
"coop_shops": [
{
"id": 1,
Expand All @@ -101,17 +104,15 @@ void setUp() {
],
"phone": "041-000-0000",
"location": "학생회관 1층",
"remarks": "공휴일 휴무",
"updated_at" : "2024-01-15"
"remarks": "공휴일 휴무"
},
{
"id": 2,
"name": "세탁소",
"opens": [],
"phone": "041-000-0000",
"location": "학생회관 2층",
"remarks": "연중무휴",
"updated_at" : "2024-01-15"
"remarks": "연중무휴"
}
]
}
Expand Down
Loading