knex / knex

A query builder for PostgreSQL, MySQL, CockroachDB, SQL Server, SQLite3 and Oracle, designed to be flexible, portable, and fun to use.
https://knexjs.org/
MIT License
18.98k stars 2.11k forks source link

Set custom route for knex migrate:make #3078

Open whitte-h opened 5 years ago

whitte-h commented 5 years ago

Currently, I have this project structure: src -entities --users ---model.js ---migrations ----20190303042400_create_users_table

What I'm trying to accomplish is to have my config setted up to my entities folder, so I can do like: npx knex migrate:make create_users_table --migrations-directory users

But I can only accomplish this by doing: npx knex migrate:make create_users_table1 --cwd="./src/entities" --migrations-directory users/migrations That I find very repetitive given the fact that my entities folder will never change, I tried to set a base folder route in my knex file but I didn't get to make it work, so, is it possible?

elhigu commented 5 years ago

I don't quite understand what are you after... why do you have to give --cwd there and not only --migrations-directory users/migrations.

Closing as this sounds like knex usage question and github issue tracker should not be used for that. Can be reopened if there is a knex bug involved.

whitte-h commented 5 years ago

I don't quite understand what are you after... why do you have to give --cwd there and not only --migrations-directory users/migrations.

Closing as this sounds like knex usage question and github issue tracker should not be used for that. Can be reopened if there is a knex bug involved.

@elhigu So this is a bug, cause when I do just the command that you suggest, the migration is been created at the same level as src in the usual migration folder, not even creating the folder users inside it I expect that command to at least create something like: src migrations -users --my_new_migration.js

Even when I try: npx knex migrate:make create_users_table1 --migrations-directory src/entities/users/migrations It still create the migration in the migration folder, that's the reason why I use --cwd

elhigu commented 5 years ago

Sounds like a bug I suppose... should write jake test to verify this.

idododu commented 4 years ago

I specified multiple migration directories, and after run knex migrate:make create_table_name, it always create file in the last folder in directories(migrations/DML/201901010101_create_table_name.js).

// knex.js
module.exports = {
  migrations: {
    directory: ['migrations/DCL', 'migrations/DDL', 'migrations/DML']
  }
}

after debugging, I find below line in knex source migrationGenerator.js

// line 65 of MigrationGenerator.js in knex source codes
_writeNewMigration(name, tmpl) {
    const config = this.config;

    const dirs = this._absoluteConfigDirs();

   // below line will only fetch the last specified directory
    const dir = dirs.slice(-1)[0]; // Get last specified directory
  }
elhigu commented 4 years ago

@idododu it is expected behavior that migration is created to the last of the configured migration directories. Please add feature request if you need it to work in a different manner.

tsarchghs commented 4 years ago

You can do this:

require('dotenv').config()

module.exports = {
  development: {
    client: "pg",
    connection: {
        database : process.env.DB_NAME,
        host : process.env.DB_HOST,
        user : process.env.DB_USERNAME,
        password : process.env.DB_PASSWORD,
    },
    migrations: {
      directory: "src/migrations"
    }
    // debug: true
  }
};
himanshu199728 commented 3 years ago

In my case I mistakenly named migrations key as migration in knexfile.ts. like Expected:

migrations: {
      directory: './database/migrations'
    },

What I had written:

migration: {
      directory: './database/migrations'
    },