How to Rollback Migrations and Seeds in Render

When you deploy your application on Render, you often need to handle your database migrations and seeders automatically. By default, you might run a build command that migrates and seeds your database, but what happens when you need to undo those operations to reset your database or re-run them with new changes?

In this tutorial, you’ll learn how to rollback your Sequelize migrations and seeders on Render by modifying your build command, then restoring it once you’ve updated your data.

The Render Build Command

You likely have a package.json at the root of your repository that orchestrates the commands needed to build and migrate your backend code. Because your code is separated into backend and frontend folders, you need a convenient place (the root package.json) to run commands targeting each subdirectory.

A typical “build command” for your Express API might look like this:

npm install &&
npm run build &&
npm run sequelize --prefix backend db:migrate &&
npm run sequelize --prefix backend db:seed:all

Render executes these commands in sequence whenever you deploy.

Why Roll Back Migrations and Seeds?

Occasionally, you’ll need to roll back to a prior state:

Without rolling back, your changes might not take effect properly. Rolling back undoes everything (migrations and seeds) and gives you a clean slate.

Rolling Back in Render

To roll back and re-run migrations/seeders, you’ll update your build command in the Render dashboard. Replace your existing build command with something like:

npm install && 
npm run build && 
npm run sequelize --prefix backend db:seed:undo:all && 
npm run sequelize --prefix backend db:migrate:undo:all && 
npm run sequelize --prefix backend db:migrate && 
npm run sequelize --prefix backend db:seed:all

Let’s break it down:

After setting this new build command, trigger a redeployment in Render. It’ll run this sequence and effectively refresh your database.

Restoring Your Original Build Command

Once your database is in the desired state, switch the build command back to the original:

npm install &&
npm run build &&
npm run sequelize --prefix backend db:migrate &&
npm run sequelize --prefix backend db:seed:all

Why revert? Because it’s typically not ideal to blow away data and re-run migrations every time you push new code. We only do the rollback sequence when we need a fresh reset or to correct database schema changes.

Key Takeaways

This workflow ensures you have the power to reinitialize your database whenever needed, without manually logging into the server or performing complicated steps. It’s all driven by a single build command that you can edit in the Render dashboard.