【问题标题】:knex.js - migrate latest 'Already Up To Date'knex.js - 迁移最新的“已经是最新的”
【发布时间】:2018-11-27 19:38:13
【问题描述】:

当我运行 knex migrate: latest 时,我得到以下信息,“Already up to date”。

您看,我之前运行过成功的迁移,但后来我使用 mySQL CLI 删除了这些表,因为我收到了“已经是最新的”消息。我认为删除表格会迫使 knex 认识到它不是最新的。没用。

有什么建议吗?

这是我的文件:

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'
    }
  }

};

迁移:

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')
    ])
};

【问题讨论】:

  • 一点额外信息:我使用 MySQL CLI 手动删除了我的数据库。然后当我运行最新的迁移时,它似乎运行:Batch 1 run: 4 migrations。但是,当我通过 MySQL CLI 执行“显示表”时,它只显示“knex_migrations”和“knex_migrations_lock”表。
  • 如果knex 的cli 类似于sequelize,它可能会跟踪在某种migrations 表(可能是knex_migrations?)中运行了哪些迁移。也许knex 正在查看您所有可用的迁移文件,认识到它们在您的knex_migrations 表中都有条目,并拒绝运行它们。我想知道您是否更改了迁移文件的名称或尝试手动删除其在knex_migrations 中的条目(如果它在那里),如果 knex 会再次为您运行迁移文件。
  • @therobinkim 感谢罗宾。我终于了解到,当这种情况发生时,我需要删除 knex_migrations 表中的记录,并删除 repo 中的迁移文件,创建新的迁移文件并运行它们。这似乎有效。

标签: javascript mysql node.js knex.js


【解决方案1】:

您可能需要删除与当前迁移同名的任何迁移日志,不久前我在 postgres 上遇到了同样的问题。

【讨论】:

  • 具体来说,删除knex_migrationsknex_migrations_lock 表。 (实际上,您应该可以只删除 knex_migrations 中要重新运行的迁移文件的行)
猜你喜欢
  • 2017-07-03
  • 1970-01-01
  • 2020-10-30
  • 2018-06-27
  • 2020-08-03
  • 2017-10-08
  • 2021-09-24
  • 1970-01-01
  • 2022-07-09
相关资源
最近更新 更多