Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Way to ignore built in columns #20

Closed
felixmosh opened this issue Mar 2, 2020 · 7 comments
Closed

Way to ignore built in columns #20

felixmosh opened this issue Mar 2, 2020 · 7 comments

Comments

@felixmosh
Copy link

Hi,

Knex internally has some times (at migrations) usage of internal tables (such as information_schema).
Those builtin tables has special cases (CONST_CASE), and because of the casting, knex fails on migrations. (trying to access TABLE_NAME) but the object is camelCased.

There is any why to ignore those built in tables?

@Kequc
Copy link
Owner

Kequc commented Mar 2, 2020

I wasn't aware of this, could you expand on it with an example so that I understand?

@felixmosh
Copy link
Author

@Kequc
Copy link
Owner

Kequc commented Mar 2, 2020

Could you give an example of what you're doing, what's happening and how you expect it to work? I'm trying to figure out what's going on and how I can fix it. I haven't ever run into the problem you are experiencing.

@felixmosh
Copy link
Author

Create a knexfile.js with simple config.

// knexfile.js
const knexStringcase = require('knex-stringcase');

module.exports = {
  development: knexStringcase({
    client: 'mysql2',
    asyncStackTraces: true,
    connection: {
      host: process.env.MYSQL_HOST,
      database: process.env.MYSQL_DBNAME,
      user: process.env.MYSQL_USER,
      password: process.env.MYSQL_PASSWORD,
      typeCast: (field, next) => {
        if (field.type === 'TINY' && field.length === 1) {
          return field.string() === '1'; // 1 = true, 0 = false
        }
        return next();
      },
    },
    pool: {
      min: 2,
      max: 10,
    },
    migrations: {
      directory: './db/migrations',
      tableName: '__knex_migrations',
    },
    seeds: {
      directory: './db/seeds',
    },
  }),
};

Run in the terminal npx knex migrate:make --knexfile ./knexfile.js rename_columns;
This will create a new migration file.
Set it's content to:

export async function up(knex) {
  await knex.schema.alterTable('someTableName', t => {
    t.renameColumn('old_column_name', 'new_column_name');
  });
}

export async function down(knex: Knex) {
  await knex.schema.alterTable('someTableName', t => {
    t.renameColumn('new_column_name', 'old_column_name');
  });
}

Then run in the terminal npx knex migrate:latest --knexfile ./knexfile.js
This will execute in the background this internal method
which executes this, which tries to access TABLE_NAME column.

The migration will fail on alter table 'db_name.undefined' drop foreign key 'undefined'.
'undefined' is not exists in 'db_name'

@Kequc
Copy link
Owner

Kequc commented Mar 3, 2020

That's kinda interesting. The only thing my library is doing is using the post process response and wrap identifier methods provided by knex as I think they are supposed to be used. I don't have any access to knex internals nor any control over when or how knex uses these methods.

It seems like there was a similar issue posted to their repository regarding a different string mapping library.

knex/knex#3033

It says it was resolved but maybe this is a new issue?

One thing that does stand out to me a little is that you're using camel case in your table names. I think this shouldn't be a problem though, I'm curious whether or not on your database your table names are snake case.

The way knex handles using these functions is a bit of a mystery to me. On the way to the database it uses wrap identifier in order to convert strings one at a time. Then on the way back it passes the entire result as a single object.

But on the way to the database for example I don't know when it's being used. I could add an additional option for ignoring certain strings but would that be helpful?

@felixmosh
Copy link
Author

Thank you for the quick answer!

All of my tables & columns are snake case.

Actually I don't know why it surfaced now, cause sometime ago I've used renameColumn function and it worked (I'm not sure it there was a use of stringCase).

I think that adding ignore list will allow me to ignore columns by name.
Maybe a better approach will be a PR to knex, to prevent this mapping on internal tables.

@Kequc
Copy link
Owner

Kequc commented Mar 8, 2020

Thanks I think that's the right place to ask for now. Please re-open if it comes back to me.

@Kequc Kequc closed this as completed Mar 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants