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

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'); ...@@ -5,12 +5,13 @@ const inherits = require('./utils/inherits');
const _ = require('lodash'); const _ = require('lodash');
const wkx = require('wkx'); const wkx = require('wkx');
const sequelizeErrors = require('./errors'); const sequelizeErrors = require('./errors');
const warnings = {};
const Validator = require('./utils/validator-extras').validator; const Validator = require('./utils/validator-extras').validator;
const momentTz = require('moment-timezone'); const momentTz = require('moment-timezone');
const moment = require('moment'); const moment = require('moment');
const logger = require('./utils/logger'); const logger = require('./utils/logger');
const warnings = {};
function ABSTRACT() {} function ABSTRACT() {}
ABSTRACT.prototype.dialectTypes = ''; ABSTRACT.prototype.dialectTypes = '';
...@@ -24,7 +25,7 @@ ABSTRACT.prototype.toSql = function toSql() { ...@@ -24,7 +25,7 @@ ABSTRACT.prototype.toSql = function toSql() {
ABSTRACT.warn = function warn(link, text) { ABSTRACT.warn = function warn(link, text) {
if (!warnings[text]) { if (!warnings[text]) {
warnings[text] = true; warnings[text] = true;
logger.warn(`${text}, '\n>> Check:', ${link}`); logger.warn(`${text} \n>> Check: ${link}`);
} }
}; };
ABSTRACT.prototype.stringify = function stringify(value, options) { ABSTRACT.prototype.stringify = function stringify(value, options) {
......
...@@ -8,6 +8,21 @@ module.exports = BaseTypes => { ...@@ -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'); 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, ...] * types: [hex, ...]
* @see hex here https://github.com/tediousjs/tedious/blob/master/src/data-type.js * @see hex here https://github.com/tediousjs/tedious/blob/master/src/data-type.js
*/ */
...@@ -155,14 +170,7 @@ module.exports = BaseTypes => { ...@@ -155,14 +170,7 @@ module.exports = BaseTypes => {
if (!(this instanceof INTEGER)) return new INTEGER(length); if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments); BaseTypes.INTEGER.apply(this, arguments);
// MSSQL does not support any options for integer removeUnsupportedIntegerOptions(this);
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;
}
} }
inherits(INTEGER, BaseTypes.INTEGER); inherits(INTEGER, BaseTypes.INTEGER);
...@@ -170,29 +178,14 @@ module.exports = BaseTypes => { ...@@ -170,29 +178,14 @@ module.exports = BaseTypes => {
if (!(this instanceof TINYINT)) return new TINYINT(length); if (!(this instanceof TINYINT)) return new TINYINT(length);
BaseTypes.TINYINT.apply(this, arguments); BaseTypes.TINYINT.apply(this, arguments);
// MSSQL does not support any options for tinyint removeUnsupportedIntegerOptions(this);
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;
}
} }
inherits(TINYINT, BaseTypes.TINYINT); inherits(TINYINT, BaseTypes.TINYINT);
function SMALLINT(length) { function SMALLINT(length) {
if (!(this instanceof SMALLINT)) return new SMALLINT(length); if (!(this instanceof SMALLINT)) return new SMALLINT(length);
BaseTypes.SMALLINT.apply(this, arguments); BaseTypes.SMALLINT.apply(this, arguments);
removeUnsupportedIntegerOptions(this);
// 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;
}
} }
inherits(SMALLINT, BaseTypes.SMALLINT); inherits(SMALLINT, BaseTypes.SMALLINT);
...@@ -200,14 +193,7 @@ module.exports = BaseTypes => { ...@@ -200,14 +193,7 @@ module.exports = BaseTypes => {
if (!(this instanceof BIGINT)) return new BIGINT(length); if (!(this instanceof BIGINT)) return new BIGINT(length);
BaseTypes.BIGINT.apply(this, arguments); BaseTypes.BIGINT.apply(this, arguments);
// MSSQL does not support any options for bigint removeUnsupportedIntegerOptions(this);
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;
}
} }
inherits(BIGINT, BaseTypes.BIGINT); inherits(BIGINT, BaseTypes.BIGINT);
......
...@@ -10,10 +10,11 @@ module.exports = BaseTypes => { ...@@ -10,10 +10,11 @@ module.exports = BaseTypes => {
/** /**
* Removes unsupported Postgres options, i.e., LENGTH, UNSIGNED and ZEROFILL, for the integer data types. * Removes unsupported Postgres options, i.e., LENGTH, UNSIGNED and ZEROFILL, for the integer data types.
* @param dataType The base integer data type. * @param dataType The base integer data type.
* @private
*/ */
function removeUnsupportedIntegerOptions(dataType) { function removeUnsupportedIntegerOptions(dataType) {
if (dataType._length || dataType.options.length || dataType._unsigned || dataType._zerofill) { 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._length = undefined;
dataType.options.length = undefined; dataType.options.length = undefined;
dataType._unsigned = undefined; dataType._unsigned = undefined;
......
...@@ -9,10 +9,11 @@ module.exports = BaseTypes => { ...@@ -9,10 +9,11 @@ module.exports = BaseTypes => {
/** /**
* Removes unsupported SQLite options, i.e., UNSIGNED and ZEROFILL, for the integer data types. * Removes unsupported SQLite options, i.e., UNSIGNED and ZEROFILL, for the integer data types.
* @param dataType The base integer data type. * @param dataType The base integer data type.
* @private
*/ */
function removeUnsupportedIntegerOptions(dataType) { function removeUnsupportedIntegerOptions(dataType) {
if (dataType._zerofill || dataType._unsigned) { 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._unsigned = undefined;
dataType._zerofill = undefined; dataType._zerofill = undefined;
} }
......
...@@ -4,7 +4,9 @@ const chai = require('chai'), ...@@ -4,7 +4,9 @@ const chai = require('chai'),
expect = chai.expect, expect = chai.expect,
Support = require('../support'), Support = require('../support'),
DataTypes = require('../../../lib/data-types'), DataTypes = require('../../../lib/data-types'),
current = Support.sequelize; sinon = require('sinon'),
current = Support.sequelize,
dialect = Support.getTestDialect();
describe(Support.getTestDialectTeaser('Model'), () => { describe(Support.getTestDialectTeaser('Model'), () => {
describe('define', () => { describe('define', () => {
...@@ -98,5 +100,30 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -98,5 +100,30 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}).to.throw('Invalid definition for "part.name", "notNull" validator is only allowed with "allowNull:false"'); }).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!