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

Fix queryContext not being passed to raw queries #3111

Merged
merged 2 commits into from Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/formatter.js
Expand Up @@ -138,6 +138,12 @@ export default class Formatter {
}
if (value instanceof Raw) {
value.client = this.client;
if (this.builder._queryContext) {
value.queryContext = () => {
return this.builder._queryContext;
};
}

query = value.toSQL();
if (query.bindings) {
this.bindings = this.bindings.concat(query.bindings);
Expand Down
78 changes: 78 additions & 0 deletions test/unit/knex.js
Expand Up @@ -185,6 +185,84 @@ describe('knex', () => {
).to.equal(null);
});

it('passes queryContext to wrapIdentifier in raw query', () => {
const knex = Knex(
Object.assign({}, sqliteConfig, {
wrapIdentifier: (str, origImpl, queryContext) => {
if (!queryContext) {
throw Error('We should have queryContext here right?');
}

if (str === 'iAmGoingToBeConvertedToId') {
str = 'id';
}
return origImpl(str);
},
})
);

return knex.schema
.queryContext({ someStuff: true })
.dropTableIfExists('test')
.then(() => {
return knex.schema
.queryContext({ someStuff: true })
.createTable('test', (table) => {
table.increments('id');
table.string('text');
});
})
.then(() => {
return knex('test')
.queryContext({ someStuff: true })
.select('id')
.whereRaw('id = ??', 'iAmGoingToBeConvertedToId');
})
.then(() => {
return knex.schema.queryContext({ someStuff: true }).dropTable('test');
});
});

it('passes queryContext to wrapIdentifier in raw query in transaction', () => {
const knex = Knex(
Object.assign({}, sqliteConfig, {
wrapIdentifier: (str, origImpl, queryContext) => {
if (!queryContext) {
throw Error('We should have queryContext here right?');
}

if (str === 'iAmGoingToBeConvertedToId') {
str = 'id';
}
return origImpl(str);
},
})
);

return knex.transaction((trx) => {
return trx.schema
.queryContext({ someStuff: true })
.dropTableIfExists('test')
.then(() => {
return trx.schema
.queryContext({ someStuff: true })
.createTable('test', (table) => {
table.increments('id');
table.string('text');
});
})
.then(() => {
return trx('test')
.queryContext({ someStuff: true })
.select('id')
.whereRaw('id = ??', 'iAmGoingToBeConvertedToId');
})
.then(() => {
return trx.schema.queryContext({ someStuff: true }).dropTable('test');
});
});
});

it('sets correct postProcessResponse for chained builders', () => {
const knex = Knex({
client: 'sqlite',
Expand Down