不要怂,就是干,撸起袖子干!

Commit a2dcfa07 by Sushant Committed by GitHub

fix(query): ensure correct return signature for QueryTypes.RAW (#12305)

1 parent 0769aea3
...@@ -150,29 +150,15 @@ class Query extends AbstractQuery { ...@@ -150,29 +150,15 @@ class Query extends AbstractQuery {
* ]) * ])
*/ */
formatResults(data, rowCount) { formatResults(data, rowCount) {
let result = this.instance;
if (this.isInsertQuery(data)) { if (this.isInsertQuery(data)) {
this.handleInsertQuery(data); this.handleInsertQuery(data);
return [this.instance || data, rowCount];
if (!this.instance) {
if (this.options.plain) {
// NOTE: super contrived. This just passes the newly added query-interface
// test returning only the PK. There isn't a way in MSSQL to identify
// that a given return value is the PK, and we have no schema information
// because there was no calling Model.
const record = data[0];
result = record[Object.keys(record)[0]];
} else {
result = data;
}
} }
}
if (this.isShowTablesQuery()) { if (this.isShowTablesQuery()) {
return this.handleShowTablesQuery(data); return this.handleShowTablesQuery(data);
} }
if (this.isDescribeQuery()) { if (this.isDescribeQuery()) {
result = {}; const result = {};
for (const _result of data) { for (const _result of data) {
if (_result.Default) { if (_result.Default) {
_result.Default = _result.Default.replace("('", '').replace("')", '').replace(/'/g, ''); _result.Default = _result.Default.replace("('", '').replace("')", '').replace(/'/g, '');
...@@ -197,8 +183,8 @@ class Query extends AbstractQuery { ...@@ -197,8 +183,8 @@ class Query extends AbstractQuery {
result[_result.Name].type += `(${_result.Length})`; result[_result.Name].type += `(${_result.Length})`;
} }
} }
} }
return result;
} }
if (this.isSelectQuery()) { if (this.isSelectQuery()) {
return this.handleSelectQuery(data); return this.handleSelectQuery(data);
...@@ -222,20 +208,19 @@ class Query extends AbstractQuery { ...@@ -222,20 +208,19 @@ class Query extends AbstractQuery {
return data; return data;
} }
if (this.isUpsertQuery()) { if (this.isUpsertQuery()) {
return [result, data[0].$action === 'INSERT']; this.handleInsertQuery(data);
return [this.instance || data, data[0].$action === 'INSERT'];
} }
if (this.isInsertQuery() || this.isUpdateQuery()) { if (this.isUpdateQuery()) {
return [result, rowCount]; return [this.instance || data, rowCount];
} }
if (this.isShowConstraintsQuery()) { if (this.isShowConstraintsQuery()) {
return this.handleShowConstraintsQuery(data); return this.handleShowConstraintsQuery(data);
} }
if (this.isRawQuery()) { if (this.isRawQuery()) {
// MSSQL returns row data and metadata (affected rows etc) in a single object - let's standarize it, sorta return [data, rowCount];
return [data, data];
} }
return data;
return result;
} }
handleShowTablesQuery(results) { handleShowTablesQuery(results) {
......
...@@ -33,6 +33,7 @@ describe(Support.getTestDialectTeaser('Warning'), () => { ...@@ -33,6 +33,7 @@ describe(Support.getTestDialectTeaser('Warning'), () => {
// last log is warning message // last log is warning message
expect(logger.args[logger.args.length - 1][0]).to.be.match(/^MySQL Warnings \(default\):.*/m); expect(logger.args[logger.args.length - 1][0]).to.be.match(/^MySQL Warnings \(default\):.*/m);
logger.restore();
}); });
}); });
} }
......
...@@ -16,7 +16,6 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -16,7 +16,6 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
await Support.dropTestSchemas(this.sequelize); await Support.dropTestSchemas(this.sequelize);
}); });
// FIXME: These tests should make assertions against the created table using describeTable
describe('createTable', () => { describe('createTable', () => {
it('should create a auto increment primary key', async function() { it('should create a auto increment primary key', async function() {
await this.queryInterface.createTable('TableWithPK', { await this.queryInterface.createTable('TableWithPK', {
...@@ -27,8 +26,13 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -27,8 +26,13 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
} }
}); });
const [response] = await this.queryInterface.insert(null, 'TableWithPK', {}, { raw: true, returning: true, plain: true }); const result = await this.queryInterface.describeTable('TableWithPK');
expect(response.table_id || typeof response !== 'object' && response).to.be.ok;
if (dialect === 'mssql' || dialect === 'mysql' || dialect === 'mariadb') {
expect(result.table_id.autoIncrement).to.be.true;
} else if (dialect === 'postgres') {
expect(result.table_id.defaultValue).to.equal('nextval("TableWithPK_table_id_seq"::regclass)');
}
}); });
it('should create unique constraint with uniqueKeys', async function() { it('should create unique constraint with uniqueKeys', async function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!