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

Commit c749746e by Mick Hansen

[refactor] do UUID to CHAR/BINARY transformation for MySQL on datatype only, not…

… on entire SQL, closes #1475
1 parent a6d8d0fb
......@@ -36,7 +36,7 @@ module.exports = (function() {
for (var attr in attributes) {
if (attributes.hasOwnProperty(attr)) {
var dataType = this.mysqlDataTypeMapping(tableName, attr, attributes[attr])
var dataType = attributes[attr]
, match;
if (Utils._.includes(dataType, 'PRIMARY KEY')) {
......@@ -240,72 +240,77 @@ module.exports = (function() {
return Utils._.template(sql)({ tableName: this.quoteIdentifiers(tableName), indexName: indexName });
},
attributesToSQL: function(attributes) {
var result = {};
for (var name in attributes) {
var dataType = attributes[name];
var fieldName = dataType.field || name;
attributeToSQL: function(attribute, options) {
if (!Utils._.isPlainObject(attribute)) {
attribute = {
type: attribute
};
}
if (Utils._.isPlainObject(dataType)) {
var template;
if (dataType.type.toString() === DataTypes.ENUM.toString()) {
template = 'ENUM(' + Utils._.map(dataType.values, function(value) {
if (attribute.type.toString() === DataTypes.ENUM.toString()) {
template = 'ENUM(' + Utils._.map(attribute.values, function(value) {
return this.escape(value);
}.bind(this)).join(', ') + ')';
} else {
template = dataType.type.toString();
template = this.dataTypeMapping(null, null, attribute.type.toString());
}
if (dataType.allowNull === false) {
if (attribute.allowNull === false) {
template += ' NOT NULL';
}
if (dataType.autoIncrement) {
if (attribute.autoIncrement) {
template += ' auto_increment';
}
// Blobs/texts cannot have a defaultValue
if (dataType.type !== 'TEXT' && dataType.type._binary !== true && Utils.defaultValueSchemable(dataType.defaultValue)) {
template += ' DEFAULT ' + this.escape(dataType.defaultValue);
if (attribute.type !== 'TEXT' && attribute.type._binary !== true && Utils.defaultValueSchemable(attribute.defaultValue)) {
template += ' DEFAULT ' + this.escape(attribute.defaultValue);
}
if (dataType.unique === true) {
if (attribute.unique === true) {
template += ' UNIQUE';
}
if (dataType.primaryKey) {
if (attribute.primaryKey) {
template += ' PRIMARY KEY';
}
if (dataType.comment && Utils._.isString(dataType.comment) && dataType.comment.length) {
template += ' COMMENT ' + this.escape(dataType.comment);
if (attribute.comment && Utils._.isString(attribute.comment) && attribute.comment.length) {
template += ' COMMENT ' + this.escape(attribute.comment);
}
if (dataType.references) {
template += ' REFERENCES ' + this.quoteTable(dataType.references);
if (attribute.references) {
template += ' REFERENCES ' + this.quoteTable(attribute.references);
if (dataType.referencesKey) {
template += ' (' + this.quoteIdentifier(dataType.referencesKey) + ')';
if (attribute.referencesKey) {
template += ' (' + this.quoteIdentifier(attribute.referencesKey) + ')';
} else {
template += ' (' + this.quoteIdentifier('id') + ')';
}
if (dataType.onDelete) {
template += ' ON DELETE ' + dataType.onDelete.toUpperCase();
if (attribute.onDelete) {
template += ' ON DELETE ' + attribute.onDelete.toUpperCase();
}
if (dataType.onUpdate) {
template += ' ON UPDATE ' + dataType.onUpdate.toUpperCase();
if (attribute.onUpdate) {
template += ' ON UPDATE ' + attribute.onUpdate.toUpperCase();
}
}
result[fieldName] = template;
} else {
result[fieldName] = dataType;
}
return template;
},
attributesToSQL: function(attributes, options) {
var result = {}
, key
, attribute;
for (key in attributes) {
attribute = attributes[key];
result[attribute.field || key] = this.attributeToSQL(attribute, options);
}
return result;
......@@ -368,6 +373,9 @@ module.exports = (function() {
},
mysqlDataTypeMapping: function(tableName, attr, dataType) {
return this.dataTypeMapping(tableName, attr, dataType);
},
dataTypeMapping: function(tableName, attr, dataType) {
if (Utils._.includes(dataType, 'UUID')) {
dataType = dataType.replace(/UUID/, 'CHAR(36) BINARY');
}
......
......@@ -731,6 +731,10 @@ module.exports = (function() {
},
pgDataTypeMapping: function(tableName, attr, dataType) {
return this.dataTypeMapping(tableName, attr, dataType);
},
dataTypeMapping: function(tableName, attr, dataType) {
if (Utils._.includes(dataType, 'PRIMARY KEY')) {
primaryKeys[tableName].push(attr);
dataType = dataType.replace(/PRIMARY KEY/, '');
......@@ -771,6 +775,8 @@ module.exports = (function() {
return dataType;
},
quoteIdentifier: function(identifier, force) {
if (identifier === '*') return identifier;
if (!force && this.options && this.options.quoteIdentifiers === false) { // default is `true`
......
......@@ -133,7 +133,9 @@ module.exports = (function() {
}
}
attributes = self.QueryGenerator.attributesToSQL(attributes);
attributes = self.QueryGenerator.attributesToSQL(attributes, {
context: 'createTable'
});
sql = self.QueryGenerator.createTableQuery(tableName, attributes, options);
return Promise.all(promises).then(function() {
......@@ -141,7 +143,9 @@ module.exports = (function() {
});
});
} else {
attributes = self.QueryGenerator.attributesToSQL(attributes);
attributes = self.QueryGenerator.attributesToSQL(attributes, {
context: 'createTable'
});
sql = self.QueryGenerator.createTableQuery(tableName, attributes, options);
return self.sequelize.query(sql, null, options);
......
......@@ -391,7 +391,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}).then(function (monkey) {
expect(monkey.get('monkeyId')).to.be.ok;
});
})
});
it('is possible to use functions as default values', function (done) {
var self = this
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!