最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - knex.js - migrate latest 'Already Up To Date' - Stack Overflow

programmeradmin4浏览0评论

When I run knex migrate: latest, I get the following, "Already up to date".

You see, I previously ran successful migrations, but then I used the mySQL CLI to delete those tables, because I was getting this 'Already up to date' message. I thought deleting the tables would force knex to recognize it wasn't up to date. Didn't work.

Any pointers?

Here are my files:

knexfile.js:

require('dotenv').config();

module.exports = {

  development: {
    client: 'mysql',
    connection: {
      host: process.env.DATABASE_HOST_DEV || '127.0.0.1',
      user: process.env.DATABASE_USER_DEV,
      password: process.env.DATABASE_PASSWORD_DEV,
      database: process.env.DATABASE_NAME_DEV
    },
    migrations: {
      directory: __dirname+'/database/migrations'
    }
  },

  staging: {
    client: 'mysql',
    connection: {
      host: '127.0.0.1',
      user: 'root',
      password: 'password',
      database: 'recipes'
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      directory: __dirname+'/database/migrations'
    }
  },

  production: {
    client: 'mysql',
    connection: {
      host: process.env.DATABASE_HOST,
      user: process.env.DATABASE_USER,
      password: process.env.DATABASE_PASSWORD,
      database: process.env.DATABASE_NAME
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      directory: __dirname+'/database/migrations'
    }
  }

};

Migrations:

exports.up = function(knex, Promise) {
    return Promise.all([
        knex.schema.hasTable('recipe').then((exists) => {
            if (!exists) {
                knex.schema.createTable('recipe', (table) => {
                    table.uuid('id');
                    table.string('name');
                    table.string('description');
                })
            }
        }),
        knex.schema.hasTable('ingredient').then((exists) => {
            if (!exists) {
                knex.schema.createTable('ingredient', (table) => {
                    table.uuid('id');
                    table.string('name');
                })
            }
        }),
        knex.schema.hasTable('recipe-ingredient').then((exists) => {
            if (!exists) {
                knex.schema.createTable('recipe-ingredient', (table)=> {
                    table.foreign('recipe_id').references('recipe.id');
                    table.foreign('ingredient_id').references('ingredient.id');
                    table.string('qty');  // <-- chose string instead of int because receipes include qty such as '1/3 cup', '1 teaspoon', etc.
                })
            }
        })
    ])
};

exports.down = function(knex, Promise) {
    return Promise.all([
        knex.schema.dropTable('recipe-ingredient'),
        knex.schema.dropTable('ingredient'),
        knex.schema.dropTable('recipe')
    ])
};

When I run knex migrate: latest, I get the following, "Already up to date".

You see, I previously ran successful migrations, but then I used the mySQL CLI to delete those tables, because I was getting this 'Already up to date' message. I thought deleting the tables would force knex to recognize it wasn't up to date. Didn't work.

Any pointers?

Here are my files:

knexfile.js:

require('dotenv').config();

module.exports = {

  development: {
    client: 'mysql',
    connection: {
      host: process.env.DATABASE_HOST_DEV || '127.0.0.1',
      user: process.env.DATABASE_USER_DEV,
      password: process.env.DATABASE_PASSWORD_DEV,
      database: process.env.DATABASE_NAME_DEV
    },
    migrations: {
      directory: __dirname+'/database/migrations'
    }
  },

  staging: {
    client: 'mysql',
    connection: {
      host: '127.0.0.1',
      user: 'root',
      password: 'password',
      database: 'recipes'
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      directory: __dirname+'/database/migrations'
    }
  },

  production: {
    client: 'mysql',
    connection: {
      host: process.env.DATABASE_HOST,
      user: process.env.DATABASE_USER,
      password: process.env.DATABASE_PASSWORD,
      database: process.env.DATABASE_NAME
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      directory: __dirname+'/database/migrations'
    }
  }

};

Migrations:

exports.up = function(knex, Promise) {
    return Promise.all([
        knex.schema.hasTable('recipe').then((exists) => {
            if (!exists) {
                knex.schema.createTable('recipe', (table) => {
                    table.uuid('id');
                    table.string('name');
                    table.string('description');
                })
            }
        }),
        knex.schema.hasTable('ingredient').then((exists) => {
            if (!exists) {
                knex.schema.createTable('ingredient', (table) => {
                    table.uuid('id');
                    table.string('name');
                })
            }
        }),
        knex.schema.hasTable('recipe-ingredient').then((exists) => {
            if (!exists) {
                knex.schema.createTable('recipe-ingredient', (table)=> {
                    table.foreign('recipe_id').references('recipe.id');
                    table.foreign('ingredient_id').references('ingredient.id');
                    table.string('qty');  // <-- chose string instead of int because receipes include qty such as '1/3 cup', '1 teaspoon', etc.
                })
            }
        })
    ])
};

exports.down = function(knex, Promise) {
    return Promise.all([
        knex.schema.dropTable('recipe-ingredient'),
        knex.schema.dropTable('ingredient'),
        knex.schema.dropTable('recipe')
    ])
};
Share Improve this question asked Jun 18, 2018 at 22:24 208_man208_man 1,7388 gold badges40 silver badges70 bronze badges 3
  • A little extra info: I manually deleted my database using the MySQL CLI. then when I run the latest migration, it appears to run: Batch 1 run: 4 migrations. BUT, when I execute "show tables" via MySQL CLI, it only shows the `knex_migrations' and the 'knex_migrations_lock' tables. – 208_man Commented Jun 18, 2018 at 22:39
  • If the cli for knex is like that of sequelize, it might keep track of which migrations have been run in some sort of migrations table (perhaps the knex_migrations?). Perhaps knex is looking at all of your available migration files, recognizing that they all have entries in your knex_migrations table, and refusing to run them. I wonder if you change the name of your migration file or try to manually delete its entry in knex_migrations (if it's there), if knex will run the migration file for you again. – therobinkim Commented Jun 19, 2018 at 4:02
  • @therobinkim thanks Robin. I've finally learned that when this happens, I need to delete the records in the knex_migrations table, and delete the migration files in the repo, create new migration files, and run them. This seems to work. – 208_man Commented Jun 19, 2018 at 16:24
Add a ment  | 

1 Answer 1

Reset to default 8

You may need to delete the logs of any migrations with the same name as the previous as your current migration in question, I had the same issue on postgres a while back.

发布评论

评论列表(0)

  1. 暂无评论