Search code examples
postgresqlknex.js

Knex migrations with Postgres always say 'Already up to date'


I am trying to alter my Postgres database using Knex migrations but they are not working.

My knexfile.js looks like this:

module.exports = {
  development: {
    client: 'pg',
    connection: {
      database: 'cms_dev',
    },
  },
};

Then I have a db.js that looks like this:

const config = require('../knexfile.js');

const env = 'development';
const knex = require('knex')(config[env]);

module.exports = knex;

knex.migrate.latest([config]);

If I am starting with a fresh database my first migration works but if I try to update the database with a new (2nd) migration it does nothing.

When I try running knex migrate:latest --env development it says:

Using environment: development
Already up to date

*****EDIT*****

I ended up generating another new (3rd) migration and pasted in the exact same code from the previous one that was being ignored and it worked. No clue why the 2nd migration silently failed and the 3rd one worked.


Solution

  • I figured out the issue. I had forgotten to configure nodemon to ignore my migrations folder.

    Because I have this line in db.js:

    knex.migrate.latest([config]);
    

    If I saved at all while creating the migration, my server would restart and cause the latest and incomplete migration to fire off. Since migrations only run once, Knex would think the database was already up to date.