Skip to content

Commit

Permalink
[knex#3033] fix: restrict hasColumn test to sqlite and mysql, add mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
whefter committed Feb 8, 2019
1 parent 85f998e commit 350ff63
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
14 changes: 8 additions & 6 deletions src/dialects/mysql/schema/compiler.js
Expand Up @@ -3,7 +3,7 @@
import inherits from 'inherits';
import SchemaCompiler from '../../../schema/compiler';

import { assign } from 'lodash';
import { assign, some } from 'lodash';

function SchemaCompiler_MySQL(client, builder) {
SchemaCompiler.call(this, client, builder);
Expand Down Expand Up @@ -44,12 +44,14 @@ assign(SchemaCompiler_MySQL.prototype, {
// Check whether a column exists on the schema.
hasColumn(tableName, column) {
this.pushQuery({
sql:
`show columns from ${this.formatter.wrap(tableName)}` +
' like ' +
this.formatter.parameter(column),
sql: `show columns from ${this.formatter.wrap(tableName)}`,
output(resp) {
return resp.length > 0;
return some(resp, (row) => {
return (
this.client.wrapIdentifier(row.Field) ===
this.client.wrapIdentifier(column)
);
});
},
});
},
Expand Down
41 changes: 27 additions & 14 deletions test/integration/schema/index.js
Expand Up @@ -930,22 +930,35 @@ module.exports = function(knex) {
});

describe('using snakeCaseMappers', function() {
beforeEach(function() {
knex.client.config.postProcessResponse = postProcessResponse;
knex.client.config.wrapIdentifier = wrapIdentifier;
});
describe('sqlite and mysql only', function() {
if (
!knex ||
!knex.client ||
!(
/sqlite3/i.test(knex.client.driverName) ||
/mysql/i.test(knex.client.driverName)
)
) {
return Promise.resolve();
}

afterEach(function() {
knex.client.config.postProcessResponse = null;
knex.client.config.wrapIdentifier = null;
});
beforeEach(function() {
knex.client.config.postProcessResponse = postProcessResponse;
knex.client.config.wrapIdentifier = wrapIdentifier;
});

it('checks whether a column exists, resolving with a boolean', function() {
return knex.schema
.hasColumn('accounts', 'firstName')
.then(function(exists) {
expect(exists).to.equal(true);
});
afterEach(function() {
knex.client.config.postProcessResponse = null;
knex.client.config.wrapIdentifier = null;
});

it('checks whether a column exists, resolving with a boolean', function() {
return knex.schema
.hasColumn('accounts', 'firstName')
.then(function(exists) {
expect(exists).to.equal(true);
});
});
});
});
});
Expand Down

0 comments on commit 350ff63

Please sign in to comment.