How to package knex migrations to a module?

1k Views Asked by At

I have a Serverless framework project with serverless-webpack. Webpack bundles the application itself nicely.

I have setup the lambdas to try migrations after update like described in the linked blog post: https://spin.atomicobject.com/2018/01/06/database-migration-aws-lambda/

The code is in ES6 and serverless framwork runs it through webpack babel-loader. I have also setup the webpack to include migration files in the bundle by copying them to correct folder:

plugins: [
    new CopyWebpackPlugin([{ from: './src/migrations/*.js', to: '' }], {}),
]

When I get the files in the correct folder so that they should be found, I get this error:

Error running migrations: Error: Cannot find module '.../.webpack/service/src/migrations/20180512154816_create-parameters.js'

I tried to include the migrations folder to package.json as a dependency (migrations: './src/migrations'). I tried to require all files from the file where withDB function is called, still the same error.

Migration files contain up and down exports:

exports.up = function (knex, Promise) {
  ..
};

exports.down = function (knex, Promise) {
  ..
};

Question is that I could create a module that I can ship with serverless deployment so that knex is able to find the modules..

By creating a index.js file to migrations folder that exports all migrations somehow? require('./migration-file.js') did not work.

0

There are 0 best solutions below