Migration 파일 수정하기

손연주·2022년 9월 1일
0
post-custom-banner
import { MigrationInterface, QueryRunner } from 'typeorm';

export class CreateUserGroupList1662019410072 implements MigrationInterface {
  name = 'CreateUserGroupList1662019410072';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "subtask_notices" DROP CONSTRAINT "FK_89915798ca2d959b3a9b59fee1b"`,
    );
    await queryRunner.query(
      `CREATE TABLE "user_groups" ("id" BIGSERIAL NOT NULL, "created_at" TIMESTAMP(6) NOT NULL DEFAULT now(), "updated_at" TIMESTAMP(6) NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP(6), "name" character varying(20) NOT NULL, CONSTRAINT "PK_ea7760dc75ee1bf0b09ab9b3289" PRIMARY KEY ("id"))`,
    );
    await queryRunner.query(
      `CREATE TABLE "user_group_lists" ("id" BIGSERIAL NOT NULL, "created_at" TIMESTAMP(6) NOT NULL DEFAULT now(), "updated_at" TIMESTAMP(6) NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP(6), "group_id" bigint NOT NULL, "user_id" integer NOT NULL, CONSTRAINT "PK_6be20458e8b2cff1cbdb6bd025f" PRIMARY KEY ("id"))`,
    );
    await queryRunner.query(
      `ALTER TABLE "read_subtask_notices" DROP CONSTRAINT "FK_0917eba31856663746890d371bc"`,
    );
    await queryRunner.query(
      `ALTER TABLE "read_subtask_notices" DROP COLUMN "user_id"`,
    );
    await queryRunner.query(
      `ALTER TABLE "read_subtask_notices" ADD "user_id" integer NOT NULL`,
    );
    await queryRunner.query(
      `ALTER TABLE "subtask_notices" DROP CONSTRAINT "FK_8d864be01ba4300283c7352189f"`,
    );
    await queryRunner.query(
      `ALTER TABLE "subtask_notices" DROP COLUMN "subtask_id"`,
    );
    await queryRunner.query(
      `ALTER TABLE "subtask_notices" ADD "subtask_id" integer NOT NULL`,
    );
    await queryRunner.query(
      `ALTER TABLE "read_subtask_notices" ADD CONSTRAINT "FK_0917eba31856663746890d371bc" FOREIGN KEY ("user_id") REFERENCES "Users"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
    );
    await queryRunner.query(
      `ALTER TABLE "subtask_notices" ADD CONSTRAINT "FK_8d864be01ba4300283c7352189f" FOREIGN KEY ("subtask_id") REFERENCES "Subtasks"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
    );
    await queryRunner.query(
      `ALTER TABLE "subtask_notices" ADD CONSTRAINT "FK_d9d18f5b57ec0b111470229b65c" FOREIGN KEY ("subtask_notice_cotent_id") REFERENCES "subtask_notice_content"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
    );
    await queryRunner.query(
      `ALTER TABLE "user_group_lists" ADD CONSTRAINT "FK_817133f1eb5fbfefde80d91cf10" FOREIGN KEY ("group_id") REFERENCES "user_groups"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    );
    await queryRunner.query(
      `ALTER TABLE "user_group_lists" ADD CONSTRAINT "FK_ca8711dc557db3d1369f3518c90" FOREIGN KEY ("user_id") REFERENCES "Users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "user_group_lists" DROP CONSTRAINT "FK_ca8711dc557db3d1369f3518c90"`,
    );
    await queryRunner.query(
      `ALTER TABLE "user_group_lists" DROP CONSTRAINT "FK_817133f1eb5fbfefde80d91cf10"`,
    );
    await queryRunner.query(
      `ALTER TABLE "subtask_notices" DROP CONSTRAINT "FK_d9d18f5b57ec0b111470229b65c"`,
    );
    await queryRunner.query(
      `ALTER TABLE "subtask_notices" DROP CONSTRAINT "FK_8d864be01ba4300283c7352189f"`,
    );
    await queryRunner.query(
      `ALTER TABLE "read_subtask_notices" DROP CONSTRAINT "FK_0917eba31856663746890d371bc"`,
    );
    await queryRunner.query(
      `ALTER TABLE "subtask_notices" DROP COLUMN "subtask_id"`,
    );
    await queryRunner.query(
      `ALTER TABLE "subtask_notices" ADD "subtask_id" bigint NOT NULL`,
    );
    await queryRunner.query(
      `ALTER TABLE "subtask_notices" ADD CONSTRAINT "FK_8d864be01ba4300283c7352189f" FOREIGN KEY ("subtask_id") REFERENCES "Subtasks"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
    );
    await queryRunner.query(
      `ALTER TABLE "read_subtask_notices" DROP COLUMN "user_id"`,
    );
    await queryRunner.query(
      `ALTER TABLE "read_subtask_notices" ADD "user_id" bigint NOT NULL`,
    );
    await queryRunner.query(
      `ALTER TABLE "read_subtask_notices" ADD CONSTRAINT "FK_0917eba31856663746890d371bc" FOREIGN KEY ("user_id") REFERENCES "Users"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
    );
    await queryRunner.query(`DROP TABLE "user_group_lists"`);
    await queryRunner.query(`DROP TABLE "user_groups"`);
    await queryRunner.query(
      `ALTER TABLE "subtask_notices" ADD CONSTRAINT "FK_89915798ca2d959b3a9b59fee1b" FOREIGN KEY ("subtask_notice_cotent_id") REFERENCES "subtask_notice_content"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
    );
  }
}

내가 설계한 테이블은 User, user_group, user_group_list 인데 다른 엉뚱한 테이블을 가져와 alter해서 drop 후 다시 add하고 있다. 마이그레이션을 실행하기 전에 파일을 먼저 꼭 확인하자.

import { MigrationInterface, QueryRunner } from 'typeorm';

export class CreateUserGroupList1662019410072 implements MigrationInterface {
  name = 'CreateUserGroupList1662019410072';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
        CREATE TABLE "user_groups" (
            "id" BIGSERIAL NOT NULL, 
            "name" character varying(20) NOT NULL, 
            "created_at" TIMESTAMP(6) NOT NULL DEFAULT now(), 
            "updated_at" TIMESTAMP(6) NOT NULL DEFAULT now(), 
            "deleted_at" TIMESTAMP(6), 
            CONSTRAINT "PK_ea7760dc75ee1bf0b09ab9b3289" PRIMARY KEY ("id"))
            `);
    await queryRunner.query(`
        CREATE TABLE "user_group_lists" (
            "id" BIGSERIAL NOT NULL, 
            "group_id" bigint NOT NULL, 
            "user_id" integer NOT NULL, 
            "created_at" TIMESTAMP(6) NOT NULL DEFAULT now(), 
            "updated_at" TIMESTAMP(6) NOT NULL DEFAULT now(), 
            "deleted_at" TIMESTAMP(6), 
            CONSTRAINT "PK_6be20458e8b2cff1cbdb6bd025f" PRIMARY KEY ("id"))
        `);
    await queryRunner.query(`
        ALTER TABLE "user_group_lists" 
        ADD CONSTRAINT "FK_817133f1eb5fbfefde80d91cf10" 
        FOREIGN KEY ("group_id") 
        REFERENCES "user_groups"("id") 
        ON DELETE NO ACTION ON UPDATE NO ACTION
        `);
    await queryRunner.query(`
        ALTER TABLE "user_group_lists" 
        ADD CONSTRAINT "FK_ca8711dc557db3d1369f3518c90" 
        FOREIGN KEY ("user_id") 
        REFERENCES "Users"("id") 
        ON DELETE NO ACTION ON UPDATE NO ACTION
      `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "user_group_lists" DROP CONSTRAINT "FK_ca8711dc557db3d1369f3518c90"`,
    );
    await queryRunner.query(
      `ALTER TABLE "user_group_lists" DROP CONSTRAINT "FK_817133f1eb5fbfefde80d91cf10"`,
    );
    await queryRunner.query(`DROP TABLE "user_group_lists"`);
    await queryRunner.query(`DROP TABLE "user_groups"`);
  }
}

컬럼 순서도 바꾸고, 하지 않아야될 drop 쿼리들을 없애줬으며 보기 쉽게 정렬해두었다.

profile
할 수 있다는 생각이 정말 나를 할 수 있게 만들어준다.
post-custom-banner

1개의 댓글

comment-user-thumbnail
2023년 5월 24일

뭐 좀 물어봐도 될까요?

답글 달기