7

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?

Zanon
  • 29,231
  • 20
  • 113
  • 126
Manan Vaghasiya
  • 881
  • 1
  • 10
  • 25

2 Answers2

4

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
  • 1
    I don't really get the reason behind this plugin. Why not using the Sequelize command line tool directly? This sls command is run as part of your CI and therefore will not solve the problem of having your database in a VPC and not publically accessible. – maxwell2022 Jun 19 '19 at 05:11
3

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.

Zanon
  • 29,231
  • 20
  • 113
  • 126
  • 3
    The reason why I would want to create a lambda function for that is that database and all the functions are running on a vpc and I wouldn't have direct access from my local machine, but I guess it would be a better idea to do the migrations from a machine that is in the same vpc. – Manan Vaghasiya May 03 '17 at 04:41
  • 3
    I disagree with this answer. The question asked for a `sequelize`-specific answer, so this would be repeatable code, as `sequelize` supports the `db:migrate` command which you could trigger on every deploy. From a security-perspective, I wouldn't want every deployer to have access to the production-database. Furthermore, I wouldn't want to disconnect running migrations from my deployment strategy. – Jaap Haagmans Nov 16 '17 at 18:06
  • @JaapHaagmans triggering migrations on deploy is a great idea, but the problem is that users might not have direct access to the database while deploying, users might have to connect to network where the database is running before deploying. – Manan Vaghasiya Jan 22 '18 at 13:48
  • @MananVaghasiya Your application has access to the database, doesn't it? Your users (I assume they are developers) will only need to trigger the deploy, migrations will run as part of your deployment strategy. In your case, e.g. create a Lambda function that triggers the migrations and trigger that function on deploy. Or better yet, if you have some kind of CI setup, use that so you can monitor the migrations and rollback if needed. – Jaap Haagmans Jan 25 '18 at 08:33