DEV Community

theBridge2
theBridge2

Posted on

Sequelize migrations

Given the following sequelize model for my basic todo project's metadata table, I'd like to make some changes using sequelize's migrations.

module.exports = (sequelize, Sequelize) => {
    const Metadata = sequelize.define("metadata", {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },     
      urgencyId: {
        type: Sequelize.INTEGER,
        allowNull: true
      },      
      dataId: {
        type: Sequelize.INTEGER,
        allowNull: true
      },      
      taskTypeId: {
        type: Sequelize.INTEGER,
        allowNull: true
      },  
      projectId: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      data_id: {
        type: Sequelize.INTEGER,
        allowNull: true
      },    
    });
    Metadata.associate = function (models) {
      Metadata.belongsTo(models.data,{
        foreignKey: {
          name: 'data_id',
          allowNull: false,
          hooks: true
        },
        onDelete: 'cascade'

      });

    };

    return Metadata;
  };
Enter fullscreen mode Exit fullscreen mode

Sequelize utilizes migration files that are essentially like database commits. To create a migrations file type in your command line:

npx sequelize-cli migration:generate --name data-columns-rename
Enter fullscreen mode Exit fullscreen mode

This will create a migrations file with a template in /migrations folder and the name you provided in the command above (in this case data-columns-rename.js with the current date/time in front).

Template migrations file created for you:

'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up (queryInterface, Sequelize) {
    /**
     * Add altering commands here.
     *
     * Example:
     * await queryInterface.createTable('users', { id: Sequelize.INTEGER });
     */
  },

  async down (queryInterface, Sequelize) {
    /**
     * Add reverting commands here.
     *
     * Example:
     * await queryInterface.dropTable('users');
     */
  }
};
Enter fullscreen mode Exit fullscreen mode

The async up command is the command it runs to try to make changes to the table, and the async down command runs to remove the changes. When you do your migrations, make sure the down commands are in reverse.

In my case, I'm trying to do some column name changes for some basic todo type table information so my filled in migrations table looks like this:

'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up (queryInterface, Sequelize) {
    await queryInterface.removeColumn('metadata','dataId')
// renameColumn: table, old column name , new column name
    await queryInterface.renameColumn('metadata','urgencyId','urgency_id')
    await queryInterface.renameColumn('metadata','taskTypeId','task_type_id')
    await queryInterface.renameColumn('metadata','projectId','project_id')

  },

  async down (queryInterface, Sequelize) {
    await queryInterface.addColumn('metadata','dataId', {
      type: Sequelize.INTEGER,
      allowNull: false
    })
    await queryInterface.renameColumn('metadata','urgency_id','urgencyId')
    await queryInterface.renameColumn('metadata','task_type_id','taskTypeId')
    await queryInterface.renameColumn('metadata','project_id','projectId')
  }
};

Enter fullscreen mode Exit fullscreen mode

(These changes are simply me still not being used to snake case as desired by postgres)

Once the migrations table is ready, run it with this command

npx sequelize-cli db:migrate
Enter fullscreen mode Exit fullscreen mode

Among other printouts from running the command, the key text for me to see success is:

== 20241224113716-metadata-columns-rename: migrating =======
== 20241224113716-metadata-columns-rename: migrated (0.065s)
Enter fullscreen mode Exit fullscreen mode

And you can see the changes verified in this screenshot from DBeaver:

Image description

Let me know if there is interest in a post describing the basics of getting started with sequelize.

Top comments (0)