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

Commit fcd07efb by Sushant Committed by GitHub

refactor: cleanup integer option removal (#10021)

1 parent b04c7900
......@@ -5,12 +5,13 @@ const inherits = require('./utils/inherits');
const _ = require('lodash');
const wkx = require('wkx');
const sequelizeErrors = require('./errors');
const warnings = {};
const Validator = require('./utils/validator-extras').validator;
const momentTz = require('moment-timezone');
const moment = require('moment');
const logger = require('./utils/logger');
const warnings = {};
function ABSTRACT() {}
ABSTRACT.prototype.dialectTypes = '';
......@@ -24,7 +25,7 @@ ABSTRACT.prototype.toSql = function toSql() {
ABSTRACT.warn = function warn(link, text) {
if (!warnings[text]) {
warnings[text] = true;
logger.warn(`${text}, '\n>> Check:', ${link}`);
logger.warn(`${text} \n>> Check: ${link}`);
}
};
ABSTRACT.prototype.stringify = function stringify(value, options) {
......
......@@ -8,6 +8,21 @@ module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://msdn.microsoft.com/en-us/library/ms187752%28v=sql.110%29.aspx');
/**
* Removes unsupported MSSQL options, i.e., LENGTH, UNSIGNED and ZEROFILL, for the integer data types.
* @param dataType The base integer data type.
* @private
*/
function removeUnsupportedIntegerOptions(dataType) {
if (dataType._length || dataType.options.length || dataType._unsigned || dataType._zerofill) {
warn(`MSSQL does not support '${dataType.key}' with options. Plain '${dataType.key}' will be used instead.`);
dataType._length = undefined;
dataType.options.length = undefined;
dataType._unsigned = undefined;
dataType._zerofill = undefined;
}
}
/**
* types: [hex, ...]
* @see hex here https://github.com/tediousjs/tedious/blob/master/src/data-type.js
*/
......@@ -155,14 +170,7 @@ module.exports = BaseTypes => {
if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments);
// MSSQL does not support any options for integer
if (this._length || this.options.length || this._unsigned || this._zerofill) {
warn('MSSQL does not support INTEGER with options. Plain `INTEGER` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
removeUnsupportedIntegerOptions(this);
}
inherits(INTEGER, BaseTypes.INTEGER);
......@@ -170,29 +178,14 @@ module.exports = BaseTypes => {
if (!(this instanceof TINYINT)) return new TINYINT(length);
BaseTypes.TINYINT.apply(this, arguments);
// MSSQL does not support any options for tinyint
if (this._length || this.options.length || this._unsigned || this._zerofill) {
warn('MSSQL does not support TINYINT with options. Plain `TINYINT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
removeUnsupportedIntegerOptions(this);
}
inherits(TINYINT, BaseTypes.TINYINT);
function SMALLINT(length) {
if (!(this instanceof SMALLINT)) return new SMALLINT(length);
BaseTypes.SMALLINT.apply(this, arguments);
// MSSQL does not support any options for smallint
if (this._length || this.options.length || this._unsigned || this._zerofill) {
warn('MSSQL does not support SMALLINT with options. Plain `SMALLINT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
removeUnsupportedIntegerOptions(this);
}
inherits(SMALLINT, BaseTypes.SMALLINT);
......@@ -200,14 +193,7 @@ module.exports = BaseTypes => {
if (!(this instanceof BIGINT)) return new BIGINT(length);
BaseTypes.BIGINT.apply(this, arguments);
// MSSQL does not support any options for bigint
if (this._length || this.options.length || this._unsigned || this._zerofill) {
warn('MSSQL does not support BIGINT with options. Plain `BIGINT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
removeUnsupportedIntegerOptions(this);
}
inherits(BIGINT, BaseTypes.BIGINT);
......
......@@ -10,10 +10,11 @@ module.exports = BaseTypes => {
/**
* Removes unsupported Postgres options, i.e., LENGTH, UNSIGNED and ZEROFILL, for the integer data types.
* @param dataType The base integer data type.
* @private
*/
function removeUnsupportedIntegerOptions(dataType) {
if (dataType._length || dataType.options.length || dataType._unsigned || dataType._zerofill) {
warn('PostgresSQL does not support ' + dataType._name + ' with LENGTH, UNSIGNED or ZEROFILL. Plain `' + dataType._name + '` will be used instead.');
warn(`PostgresSQL does not support '${dataType.key}' with LENGTH, UNSIGNED or ZEROFILL. Plain '${dataType.key}' will be used instead.`);
dataType._length = undefined;
dataType.options.length = undefined;
dataType._unsigned = undefined;
......
......@@ -9,10 +9,11 @@ module.exports = BaseTypes => {
/**
* Removes unsupported SQLite options, i.e., UNSIGNED and ZEROFILL, for the integer data types.
* @param dataType The base integer data type.
* @private
*/
function removeUnsupportedIntegerOptions(dataType) {
if (dataType._zerofill || dataType._unsigned) {
warn('SQLite does not support ' + dataType._name + ' with UNSIGNED or ZEROFILL. Plain `' + dataType._name + '` will be used instead.');
warn(`SQLite does not support '${dataType.key}' with UNSIGNED or ZEROFILL. Plain '${dataType.key}' will be used instead.`);
dataType._unsigned = undefined;
dataType._zerofill = undefined;
}
......
......@@ -22,8 +22,8 @@ describe('[MYSQL Specific] Query', () => {
it('check iterable', () => {
const validWarning = [];
const invalidWarning = {};
const warnings = [validWarning, undefined, invalidWarning];
const warnings = [validWarning, undefined, invalidWarning];
const query = new Query({}, current, {});
const stub = sinon.stub(query, 'run');
stub.onFirstCall().resolves(warnings);
......
......@@ -4,7 +4,9 @@ const chai = require('chai'),
expect = chai.expect,
Support = require('../support'),
DataTypes = require('../../../lib/data-types'),
current = Support.sequelize;
sinon = require('sinon'),
current = Support.sequelize,
dialect = Support.getTestDialect();
describe(Support.getTestDialectTeaser('Model'), () => {
describe('define', () => {
......@@ -98,5 +100,30 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
}).to.throw('Invalid definition for "part.name", "notNull" validator is only allowed with "allowNull:false"');
});
describe('datatype warnings', () => {
beforeEach(() => {
sinon.spy(console, 'warn');
});
afterEach(() => {
console.warn.restore();
});
it('warn for unsupported INTEGER options', () => {
current.define('A', {
age: {
type: DataTypes.TINYINT.UNSIGNED
}
});
if (dialect === 'postgres' || dialect === 'sqlite' || dialect === 'mssql') {
expect(true).to.equal(console.warn.calledOnce);
expect(console.warn.args[0][0]).to.contain("does not support 'TINYINT'");
} else {
expect(false).to.equal(console.warn.calledOnce);
}
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!