Sequelize provides a powerful migration system that allows you to carefully manage database
schema changes. Migrations ensure that you can apply specific changes to the
database without risking data loss.
Steps to Set Up Migrations:
- Install Sequelize CLI
- npm install --save-dev sequelize-cli
- Initialize Sequelize CLI:
now if you have to add new column in table
· Create a
migration for the new column (kil_col):
npx sequelize-cli migration:generate --name
add-kil-col
This will
generate a new migration file where you can specify the column additions.
· Inside the
generated migration file, write the migration logic:
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface, Sequelize) {
// Add the 'kil_col' column to the 'tbl_members' table
await queryInterface.addColumn('tbl_members', 'kil_col', {
type: Sequelize.STRING(255),
allowNull: false,
defaultValue: 'kil_colkil_col', // Default value
});
},
async down (queryInterface, Sequelize) {
// Remove the 'kil_col' column from the 'tbl_members' table
await queryInterface.removeColumn('tbl_members', 'kil_col');
}
};
· Run the
migration to apply the changes:
npx sequelize-cli db:migrate
3. Rollback (if necessary):
If you need to undo the migration (for example, if you made a mistake or want to revert the changes), you can run:
npx sequelize-cli db:migrate:undo
// NOW IF YOU WANT TO CHANGE AGAIN ( THIS TIME REMOVE SAME COLUMN )
Steps to Remove the Column
1. Create a New Migration to Remove the Column
Instead of deleting it manually from MySQL, create a new migration to remove the column. This approach is cleaner and maintains a proper history of database changes.
Run the following command to generate a new migration:
npx sequelize-cli migration:generate --name remove-kil-col
This will create a migration file in the migrations folder. Open it and modify it like this:
// INSIDE REMOVE MIGRATIN FILE
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.removeColumn('tbl_members', 'kil_col');
},
async down(queryInterface, Sequelize) {
// If you want to be able to undo this migration, define how to re-add the column
await queryInterface.addColumn('tbl_members', 'kil_col', {
type: Sequelize.STRING(255),
allowNull: false,
defaultValue: 'kil_colkil_col', // Restore the original default value
});
},
};
2. Run the Migration
Run the migration to remove the column from the database:
npx sequelize-cli db:migrate
3. Remove kil_col from the Sequelize Model
After the migration has successfully removed the column from the database, update your
Sequelize model (Member.js) to remove the kil_col field:
// Remove this block from the model
kil_col: {
type: DataTypes.STRING(255),
allowNull: false,
defaultValue: 'kil_colkil_col',
},
Why This Approach?
Consistency: Ensures the model and database stay in sync.
Rollback Capability: If you ever need to restore the column, you can do so by rolling back the migration.
Clean History: Keeps a proper record of schema changes for yourself or other developers.
For Mulitple changes ( adding multiple columns
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
// Add the first column
await queryInterface.addColumn('tbl_members', 'first_new_column', {
type: Sequelize.STRING(100),
allowNull: true,
defaultValue: null,
}, {
after: 'email', // Place it after the 'email' column
});
// Add the second column
await queryInterface.addColumn('tbl_members', 'second_new_column', {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0,
}, {
after: 'first_new_column', // Place it after the 'first_new_column'
});
// Add more columns as needed
await queryInterface.addColumn('tbl_members', 'third_new_column', {
type: Sequelize.DATE,
allowNull: true,
defaultValue: null,
}, {
after: 'second_new_column', // Place it after the 'second_new_column'
});
},
async down(queryInterface, Sequelize) {
// Remove the columns in reverse order for rollback
await queryInterface.removeColumn('tbl_members', 'third_new_column');
await queryInterface.removeColumn('tbl_members', 'second_new_column');
await queryInterface.removeColumn('tbl_members', 'first_new_column');
},
};
Comments
Post a Comment