TIL

23/11/17 TIL __ ์‹œํ€„๋ผ์ด์ฆˆ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ˆ˜์ • sequelize migration

GABOJOK 2023. 11. 17. 23:08

 

 

๐Ÿง ์ƒํ™ฉ 

sequelize-cli ์™€, aws rds๋ฅผ ์ด์šฉํ•ด ๊ฐœ์ธ๊ณผ์ œ๋ฅผ ์ง„ํ–‰ํ•˜๋˜ ์ค‘,

์ด๋ฏธ ๋งŒ๋“  ํ…Œ์ด๋ธ”์„ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค.

birth_year, birth_day, birth_month, ์ด ์„ธ๊ฐ€์ง€์˜ ์ปฌ๋Ÿผ์„ ์‚ญ์ œํ•˜๊ณ ,

birth_date ๋ผ๋Š” ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•˜๋ฉฐ, ํƒ€์ž…๋„ date๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ์—ˆ๋‹ค.

 

 

 

๐Ÿš— ์•ก์…˜

Sequelize ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋ณด๋‹ˆ,  ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ˆ˜์ • ํŒŒ์ผ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

npx sequelize-cli migration:generate --name ํŒŒ์ผ์ด๋ฆ„์ ๊ธฐ-ํ…Œ์ด๋ธ”๋ช…

์˜ˆ์‹œ 
npx sequelize-cli migration:generate --name modify-user

 

 

๊ทธ๋Ÿผ ์•„๋ž˜์™€ ๊ฐ™์€ ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๋Š”๋ฐ, up์—๋Š” ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ๋‚ด์šฉ์„ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

down ์—์„œ๋Š” undo๋ฅผ ์ด์šฉํ•ด ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์„ ๋˜๋Œ๋ฆด๋•Œ ์ˆ˜ํ–‰ํ•  ๋กœ์ง์„ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Person', {
      name: Sequelize.DataTypes.STRING,
      isBetaMember: {
        type: Sequelize.DataTypes.BOOLEAN,
        defaultValue: false,
        allowNull: false
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Person');
  }
};

 

 

 

๋‚ด ๊ฒฝ์šฐ ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๊ฐ€ user, product 2๊ฐœ์˜€๊ณ ,

product ํ…Œ์ด๋ธ”์—์„œ user_id ๋ผ๋Š” ์ปฌ๋Ÿผ์ด user ํ…Œ์ด๋ธ”์—์„œ ๊ฐ€์ ธ์˜จ ์™ธ๋ž˜ํ‚ค์˜€๊ธฐ ๋•Œ๋ฌธ์—,

user ํ…Œ์ด๋ธ”์ด ๋จผ์ € ์กด์žฌ ํ•œ ๋’ค product ํ…Œ์ด๋ธ”์ด ์žˆ์–ด์•ผ๋งŒ ํ–ˆ๋‹ค.

 

ํ…Œ์ด๋ธ”์„ ๋‹ค์‹œ ํ•˜๋ ค๋ฉด undo ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋Œ๋ฆฐ ๋’ค์— ๋‹ค์‹œ ๋งŒ์ ธ์•ผ ํ•œ๋‹ค๋Š” ๊ธ€์„ ๋ด์„œ

๋‘๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ undo ํ• ๊ฑฐ๋ฉด, ๊ทธ๋ƒฅ drop ํ•ด์•ผํ•˜๋‚˜ ๊ณ ๋ฏผํ–ˆ์ง€๋งŒ,

์ผ๋‹จ์€ modify ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์—์„œ ์ปฌ๋Ÿผ ์‚ญ์ œ ๋ฐ ์ถ”๊ฐ€๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

 

 

๋จผ์ € ๊ธฐ์กด ํ…Œ์ด๋ธ”์— ๋‚ด์šฉ๋ฌผ์„ ๋‹ค ์‚ญ์ œํ•ด ์ฃผ๊ณ  ๋นˆ ํ…Œ์ด๋ธ”๋กœ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ˆ˜์ • ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ๋’ค,

๋‚ด ๊ฒฝ์šฐ์—๋Š” up์—๋งŒ ์ž‘์„ฑํ•ด ์ฃผ์—ˆ๋‹ค.

 

'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
	async up(queryInterface, Sequelize) {
		await queryInterface.removeColumn('User', 'birth_year');
		await queryInterface.removeColumn('User', 'birth_month');
		await queryInterface.removeColumn('User', 'birth_day');

		await queryInterface.addColumn('User', 'birth_date', {
			type: Sequelize.DATE
		});
	},
	async down(queryInterface, Sequelize) {
	}
};

 

 

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•œ ํ›„์—, ๋ชจ๋ธ ํŒŒ์ผ๋„ ๋™์ผํ•˜๊ฒŒ ์ˆ˜์ •์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์ดํ›„  ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด์„œ  ํ…Œ์ด๋ธ”์„ ๋‹ค์‹œ ์ƒ์„ฑํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

npx sequelize db:migrate

 

 

 

 

 

 

 

์‚ฌ์‹ค up์€ ์•Œ๊ฒ ๋Š”๋ฐ down์€ ๊ทธ๋ž˜์„œ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”๊ฑด์ง€ ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ”๋‹ค.

๋˜๋Œ๋ฆฌ๋Š” ๊ธฐ๋Šฅ์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ• ์ง€ ๋ชฐ๋ผ์„œ ๋‚˜์ค‘์— ๋‹ค์‹œ ์ฐพ์•„๋ณด๋‹ˆ,

down ํ•จ์ˆ˜๋Š”  up ํ•จ์ˆ˜์—์„œ ์ˆ˜ํ–‰ํ•œ ๋ณ€๊ฒฝ ์ž‘์—…์„ ์—ญ์ˆœ์œผ๋กœ ๋˜๋Œ๋ฆฌ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
up ํ•จ์ˆ˜์—์„œ ์ˆ˜ํ–‰ํ•œ ์ž‘์—…์„ ์—ญ์œผ๋กœ ๋˜๋Œ๋ฆฌ๋Š” ๊ตฌ๋ฌธ์„ down ํ•จ์ˆ˜์— ๊ตฌํ˜„ํ•˜์—ฌ ์ด์ „ ์ƒํƒœ๋กœ ๋˜๋Œ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜๋“œ์‹œ ์ •์˜ํ•  ํ•„์š”๋Š” ์—†์œผ๋ฉฐ,
์‹ค์ œ ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ ๋ณ€๊ฒฝ์— ์‹คํŒจํ•œ ๊ฒฝ์šฐ ์ด์ „์ƒํƒœ๋กœ ์‰ฝ๊ฒŒ ๋กค๋ฐฑ ํ•  ์ˆ˜ ์žˆ๋‹ค.
๋งŒ์•ฝ ์ •์˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ์ˆ˜๋™์œผ๋กœ ๋˜๋Œ๋ ค์•ผ ํ•˜๋ฉฐ, ์‹ค์ˆ˜ ํ™•๋ฅ ์ด ๋†’์•„์ง„๋‹ค.

 

๋‹น์žฅ์€ ํ•„์š”ํ•˜์ง€ ์•Š์ง€๋งŒ,

์ข€๋” ์•ˆ์ „ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•˜๊ณ , ์ปจํ‹€๋กค ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์ข‹๊ฒŸ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

 

 

 

 

์ฐธ๊ณ ํ•œ ์‚ฌ์ดํŠธ

https://sequelize.org/docs/v6/other-topics/migrations/

 

Migrations | Sequelize

Just like you use version control systems such as Git to manage changes in your source code, you can use migrations to keep track of changes to the database. With migrations you can transfer your existing database into another state and vice versa: Those s

sequelize.org