Search code examples
migrationsequelize.jsserverless-frameworksequelize-cliserverless-architecture

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?


Solution

  • 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.