Search code examples
mysqlnode.jsknex.js

Knex.js: Can I have multiple migrations to save in only one migration table?


I have a database which is used by multiple projects. Each project has its database migration.

I have tried to google and read the knex documentation, but no luck. I have seen some suggestion to fake the migration files to trick the migration table, but I don't think it is a good solution.

I want to keep all the migrations data in one migration table. Is it possible on knex?


Solution

  • Having different set of migration files for each projects and trying to run them separately against the same migration table is not possible. There is no good solution for it and it would not make sense anyways to do it.

    If migrations are not related to each other, then there is no reason to have them in the same table. On the other hand if they are related, then the files really should be hosted in the same place to guarantee that everything is done in correct order.

    You can setup migration table name tableName (http://knexjs.org/#Migrations-API) to be different for every project in knex config.

    However I would never recommend having multiple projects using the same database and everyone having separate migrations for it.

    Only reason where that could be remotely acceptable would be the case where you don't have access to create separate databases for each project.

    If projects are sharing the same data model (microservices with shared DB), in that case you should still be using multiple databases or to have single service which is the owner of the schema changes and the rest of the services should only read/write data.