knex.js - 迁移最新的“已经最新”

时间:2018-06-18 22:24:49

标签: javascript mysql node.js knex.js

当我运行knex migrate:latest时,我得到以下内容,“已经是最新的”。

你看,我以前运行过成功的迁移,但之后我使用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')
    ])
};

1 个答案:

答案 0 :(得分:2)

您可能需要删除与以前的迁移名称相同的所有迁移日志,因为前一段时间我在postgres上遇到了同样的问题。