Skip to content

Commit

Permalink
mysql warn .returning() does not have any effect. (knex#3039)
Browse files Browse the repository at this point in the history
* mysql warn `.returning()` does not have any effect.

* add test if warning is emitted or not
  • Loading branch information
HurSungYun authored and elhigu committed Mar 3, 2019
1 parent 15ac75c commit dfe0129
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/dialects/mysql/query/compiler.js
Expand Up @@ -7,6 +7,14 @@ import { assign, identity } from 'lodash';

function QueryCompiler_MySQL(client, builder) {
QueryCompiler.call(this, client, builder);

const { returning } = this.single;

if (returning) {
this.client.logger.warn(
'.returning() is not supported by mysql and will not have any effect.'
);
}
}
inherits(QueryCompiler_MySQL, QueryCompiler);

Expand Down
74 changes: 74 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -8677,6 +8677,80 @@ describe('QueryBuilder', function() {
});
});

it('should warn to user when use `.returning()` function in MySQL', function() {
var loggerConfigForTestingWarnings = {
log: {
warn: function(message) {
if (
message ===
'.returning() is not supported by mysql and will not have any effect.'
) {
throw new Error(message);
}
},
},
};

var mysqlClientForWarnings = new MySQL_Client(
Object.assign({ client: 'mysql' }, loggerConfigForTestingWarnings)
);

expect(function() {
testsql(
qb()
.into('users')
.insert({ email: 'foo' })
.returning('id'),
{
mysql: {
sql: 'insert into `users` (`email`) values (?)',
bindings: ['foo'],
},
},
{
mysql: mysqlClientForWarnings,
}
);
}).to.throw(Error);
});

it('should warn to user when use `.returning()` function in SQLite3', function() {
var loggerConfigForTestingWarnings = {
log: {
warn: function(message) {
if (
message ===
'.returning() is not supported by sqlite3 and will not have any effect.'
) {
throw new Error(message);
}
},
},
};

var sqlite3ClientForWarnings = new SQLite3_Client(
Object.assign({ client: 'sqlite3' }, loggerConfigForTestingWarnings)
);

expect(function() {
testsql(
qb()
.into('users')
.insert({ email: 'foo' })
.returning('id'),
{
sqlite3: {
sql: 'insert into `users` (`email`) values (?)',
bindings: ['foo'],
},
},
{
sqlite3: sqlite3ClientForWarnings,
}
);
}).to.throw(Error);
});

it('join with subquery using .withSchema', function() {
testsql(
qb()
Expand Down

0 comments on commit dfe0129

Please sign in to comment.