Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How/Where to run sequelize migrations in a serverless project?

I am trying to use Sequelize js with Serverless, coming from traditional server background, I am confused where/how to run database migrations.

Should I create a dedicated function for running migration or is there any other way of running migrations?

like image 574
Manan Vaghasiya Avatar asked May 02 '17 12:05

Manan Vaghasiya


2 Answers

I found myself with this same question some days ago while structuring a serverless project, so I've decided to develop a simple serverless plugin to manage sequelize migrations through CLI.

With the plugin you can:

  • Create a migration file
  • List pending and executed migrations
  • Apply pending migrations
  • Revert applied migrations
  • Reset all applied migrations

I know this question was posted about two years ago but, for those who keep coming here looking for answers, the plugin can be helpful.

The code and the instructions to use it are on the plugin repository on github and plugin page on npm.

To install the plugin directly on your project via npm, you can run:

npm install --save serverless-sequelize-migrations
like image 177
Manoel Ferreira Avatar answered Oct 12 '22 23:10

Manoel Ferreira


Lambda functions were designed to be available to run whenever necessary. You deploy them when you expect multiple executions.

Why would you create a Lambda function for a migration task? Applying a database migration is a maintenance task that you should execute just one time per migration ID. If you don't want to execute the same SQL script multiple times, I think that you should avoid creating a Lambda function for that purpose.

In this case, I would use a command line tool to connect with this database and execute the appropriate task. You could also run a Node.js script for this, but creating a Lambda to execute the script and later removing this Lambda sounds strange and should be used only if you don't have direct access to this database.

like image 37
Zanon Avatar answered Oct 13 '22 00:10

Zanon