๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ‘จ‍๐Ÿ’ป web.dev/node

Sequelize ORM์—์„œ migration ํ™œ์šฉํ•˜๊ธฐ

by HandHand 2021. 3. 3.

Sequelize๋ฅผ ํ†ตํ•œ DB ์ ‘๊ทผ

ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ ๋„์ค‘ ์œ ์ € ๋ชจ๋ธ์— ๊ด€๋ฆฌ์ž ์œ ๋ฌด ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ๊นœ๋นกํ•ด ์ด๋ฅผ ์ˆ˜์ •ํ•ด์•ผํ•  ์ผ์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค.
DB๋ฅผ ๋‹ค์‹œ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์กด์žฌํ•˜๊ฒ ์ง€๋งŒ ๊ธฐ์กด์˜ ๋ฐ์ดํ„ฐ๋“ค์˜ ๋ณต๊ตฌ๊ฐ€ ํž˜๋“ค๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.
์ด๋Ÿด๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด sequelize ์˜ migration ์ž…๋‹ˆ๋‹ค.

์ปฌ๋Ÿผ ์ถ”๊ฐ€ํ•˜๊ธฐ

ํ˜„์žฌ User ํ…Œ์ด๋ธ”์€ admin ์ด๋ผ๋Š” ์ปฌ๋Ÿผ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ migration ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ๋’ค ์ด๋ฅผ ๊ธฐ์กด์˜ DB์— ๋ฐ˜์˜ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

migration ํŒŒ์ผ ์ƒ์„ฑ

์šฐ์„  ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•  migration ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.
์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด migrations ๋””๋ ‰ํ† ๋ฆฌ ํ•˜์œ„์— addColumn.js ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

$ sequelize migration:create โ€“-name addcolumn

migration ์šฉ js ํŒŒ์ผ ์ˆ˜์ •

migration ํŒŒ์ผ์„ ์ƒ์„ฑํ–ˆ์œผ๋ฉด ์ด์ œ ๊ธฐ์กด ํ…Œ์ด๋ธ”์•  ์ƒˆ๋กœ์šด ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
queryInterface ์˜ addColumn ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด user ํ…Œ์ด๋ธ”์— admin ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
์ด๋•Œ ํ•ด๋‹น ์ปฌ๋Ÿผ์˜ ์†์„ฑ์€ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ •์˜ํ•œ ๋’ค ์ธ์ž๊ฐ’์œผ๋กœ ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค.

migrations/addColumn.js

"use strict";

module.exports = {
  up: (queryInterface, Sequelize) => {
    /*
      Add altering commands here.
      Return a promise to correctly handle asynchronicity.
    */
    return queryInterface.addColumn("users", "admin", {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false,
    });
  },

  down: (queryInterface, Sequelize) => {},
};

migrate ํ•˜๊ธฐ

addColumn.js ํŒŒ์ผ ํŽธ์ง‘์„ ๋งˆ์ณค๋‹ค๋ฉด ์ด์ œ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์‹ค์ œ DB์— ๋ฐ˜์˜ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด migrate ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
์ €๋Š” ํ˜„์žฌ ๊ฐœ๋ฐœํ™˜๊ฒฝ์—์„œ DB๋ฅผ ์กฐ์ž‘ํ•˜๊ณ  ์žˆ์–ด์„œ ์ธ์ž๋กœ development ๋ฅผ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. (config ์„ค์ •์„ ๋”ฐ๋ฆ„)
์ดํ›„ DB ์Šคํ‚ค๋งˆ๋ฅผ ์กฐํšŒํ•ด๋ณด๋ฉด User ํ…Œ์ด๋ธ”์— admin ์ปฌ๋Ÿผ์ด ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ sequelize db:migrate --env development

๋‹ค์ค‘ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜

์—ฌ๋Ÿฌ๊ฐœ์˜ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ž‘์—…์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ๋Š” ํ•ด๋‹น ์ž‘์—…๋“ค์„ Promise ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
์˜ˆ์‹œ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒˆ๋กœ์šด ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

"use strict";

module.exports = {
  up: (queryInterface, Sequelize) => {
    return Promise.all([
      queryInterface.addColumn("users", "admin", {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      }),
      queryInterface.changeColumn("users", "password", {
        type: Sequelize.STRING(30),
        allowNull: false,
        unique: false,
      }),
    ]);
  },

  down: (queryInterface, Sequelize) => {
    /*
      Add reverting commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.dropTable('users');
    */
  },
};

์ฐธ๊ณ  ์ž๋ฃŒ

๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€