Skip to content

Commit

Permalink
[knex#3033] fix: add tests, fix hasColumn
Browse files Browse the repository at this point in the history
  • Loading branch information
whefter committed Feb 8, 2019
1 parent 52c4f68 commit 85f998e
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 93 deletions.
7 changes: 6 additions & 1 deletion src/dialects/sqlite3/schema/compiler.js
Expand Up @@ -26,7 +26,12 @@ SchemaCompiler_SQLite3.prototype.hasColumn = function(tableName, column) {
this.pushQuery({
sql: `PRAGMA table_info(${this.formatter.wrap(tableName)})`,
output(resp) {
return some(resp, { name: column });
return some(resp, (col) => {
return (
this.client.wrapIdentifier(col.name) ===
this.client.wrapIdentifier(column)
);
});
},
});
};
Expand Down
7 changes: 6 additions & 1 deletion src/dialects/sqlite3/schema/ddl.js
Expand Up @@ -30,7 +30,12 @@ function SQLite3_DDL(client, tableCompiler, pragma, connection) {

assign(SQLite3_DDL.prototype, {
getColumn: Promise.method(function(column) {
const currentCol = find(this.pragma, { name: column });
const currentCol = find(this.pragma, (col) => {
return (
this.client.wrapIdentifier(col.name) ===
this.client.wrapIdentifier(column)
);
});
if (!currentCol)
throw new Error(
`The column ${column} is not in the ${this.tableName} table`
Expand Down
34 changes: 34 additions & 0 deletions src/util/snake-case-mappers.js
@@ -0,0 +1,34 @@
import { isArray, isObject, snakeCase, camelCase, mapKeys } from 'lodash';

function mapResponseItem(item) {
if (!isObject(item)) {
return item;
}

return mapKeys(item, (val, key) => {
return camelCase(key);
});
}

export function postProcessResponse(response) {
if (isArray(response)) {
return response.map(mapResponseItem);
} else {
return mapResponseItem(response);
}
}

export function wrapIdentifier(id, origImpl) {
// _.snakeCase removes the leading _ and places _ in front of numbers,
// mangling the temp table names.
if (id.startsWith('_knex_temp_alter')) {
return origImpl(id);
}

const parts = id.split('.', 2);
const prefix = parts.length === 2 ? parts[0] + '.' : '';
const toWrap = prefix ? parts[1] : parts[0];
return origImpl(
prefix + (toWrap.startsWith('_') ? '_' : '') + snakeCase(toWrap)
);
}

0 comments on commit 85f998e

Please sign in to comment.