라이브 배포가 나가기 전에 migration 파일 점검을 해야 했다. 근데 내 로컬에서만 삭제하고, 삭제한 파일을 push 하지 않았던 문제가 있었다. 이 코드가 다른 곳에서 쓰이지는 않지만, 배포 나가기 전이라 안전한 방법으로 migration 파일 삭제하기 위한 방법을 알게 되었다.
1) Drop에 관한 migration 파일 작성
만약 내가 project_post 라는 테이블을 만들었다고 치자. 이런 상황에서 기존에 CreateProjectPost migration은 이미 다른 환경에서 돌아갔었을 것이므로, 이 project_post 라는 테이블을 삭제하는 migration 파일을 만들어야 한다.
기존 CreateProjectPost.ts migration 파일
import { MigrationInterface, QueryRunner } from 'typeorm';
export class CreateProjectPostTable1713788536680 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`create table IF NOT EXISTS project_post
(
"id" serial constraint "project_post_pk" primary key,
"created_at" timestamp with time zone default now() not null,
"updated_at" timestamp with time zone default now() not null,
"is_active" boolean default true not null,
"project_id" integer,
"post_id" integer
)`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}
DropProjectPost 파일 만들기
import { MigrationInterface, QueryRunner } from 'typeorm';
export class DropProjectPost1715216916156 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
drop table if exists project_post cascade;
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}
-> 이 파일을 만들때 drop table if exists ~ 라는 명령어는
IntelliJ 기준으로 테이블에서 Drop을 선택하면 다음과 같은 창이 뜬다.
이 체크박스 3개를 다 해주면 위의 drop과 관련된 명령어가 완성된다.
2) table 삭제
intelliJ database 리스트에서 project_post 엔티티를 수동으로 삭제해준다.
그러면 배포 전에 안전하게 migration 파일과 엔티티를 삭제할 수 있게 된다.
'Backend > DB' 카테고리의 다른 글
db migration 파일을 작업하면서 알게된 점 (0) | 2024.03.20 |
---|---|
MYSQL) 현재 DB의 timezone 확인해보기 (0) | 2023.10.31 |
timestamp값이 년-월-일만 나왔던 경우(분,초가 안나옴!) (0) | 2023.10.27 |
created_at과 updated_at에서 날짜가 찍혀서 안나오는 문제 (0) | 2023.10.27 |
table이나 column drop할때 오류 최소화 하는법 (0) | 2023.10.19 |