Skip to content
tezvyn:
Menu

Sequelize Migrations: Version Control for Your Database

Source: sequelize.orgMediumHow cards are made

Think of Sequelize migrations as Git for your database schema. Each file is a commit describing how to apply (up) and revert (down) a change. Use them to evolve your schema reliably across environments. The footgun: never edit the DB directly.

Why it exists

In any project with multiple developers or deployment environments (dev, staging, production), keeping the database schema synchronized is a major challenge. Manually running SQL scripts is error-prone and impossible to version control. Migrations solve this by treating schema changes as code.

The mental model

A migration is like a commit in Git, but for your database structure. Each migration is a file containing two functions: up to apply a change (like adding a table) and down to revert it. The Sequelize Command-Line Interface (CLI) acts as your Git client, applying these commits in order to bring any database to the desired state.

How it works

The process is managed by the sequelize-cli. First, you run npx sequelize-cli init to create the necessary folders: config, models, migrations, and seeders. You then configure your database connection in config/config.json. To create a new table, you run a command like npx sequelize-cli model:generate. This creates both a model file for your application code and a timestamped migration file. This migration file contains the up and down logic using Sequelize's queryInterface. Finally, you run npx sequelize-cli db:migrate to execute the up function on all pending migrations. Sequelize tracks which migrations have already been applied in a special SequelizeMeta table in your database.

When to use it

Use migrations for every single change to your database schema. This includes creating or dropping tables, adding or removing columns, creating indexes, or modifying constraints. It is the standard, non-negotiable way to manage database evolution in a professional Node.js project using Sequelize.

When not to use it

Migrations are for schema, not for data. While you can insert data in a migration, it's an anti-pattern; use seeders for populating tables with initial or test data. The biggest footgun is editing a migration file after it has been run and pushed to a shared branch. This is like rewriting Git history and will cause chaos. If you make a mistake, create a new migration to fix it.

One canonical example

To create a User table, you run npx sequelize-cli model:generate --name User --attributes firstName:string,email:string. This generates a migration file. The up function in that file will contain await queryInterface.createTable('Users', { ... }) defining the columns. The down function will contain await queryInterface.dropTable('Users');. This file is now a permanent, executable record of that specific schema change.

Interview question

According to the card, what is considered the biggest 'footgun' when working with Sequelize migrations?

  • a.Using a migration to populate a database with initial test data.
  • b.Generating a new migration for every single column addition or deletion.
  • c.Failing to define a down function for a migration that creates a new table.
  • d.Modifying a migration file after it has been executed and shared with other developers.Correct
Why?

The card explicitly identifies modifying a migration file after it has been run and pushed to a shared branch as 'the biggest footgun,' comparing it to rewriting Git history. While using migrations for initial data (Option A) is an anti-pattern, the card does not label it as the *biggest* footgun.

Just read this? Test yourself on what you have been reading.

Read the original → sequelize.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on nodejs — each one lists the topics its interview covers.

See open roles