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

Commit a3dbf684 by Jan Aagaard Meier

Cleaning up indexes code a bit

1 parent d87330ef
...@@ -257,16 +257,11 @@ module.exports = (function() { ...@@ -257,16 +257,11 @@ module.exports = (function() {
} else { } else {
options.indicesType = options.indicesType || options.type; options.indicesType = options.indicesType || options.type;
} }
if (options.name) {
options.indexName = options.name;
}
if (options.method) {
options.indexType = options.method;
}
options = Utils._.extend({ options = Utils._.extend({
indexType: options.method || undefined,
indicesType: null, indicesType: null,
indexName: Utils.inflection.underscore(tableName + '_' + onlyAttributeNames.join('_')), indexName: options.name || Utils.inflection.underscore(tableName + '_' + onlyAttributeNames.join('_')),
parser: null parser: null
}, options); }, options);
......
...@@ -61,7 +61,7 @@ module.exports = (function() { ...@@ -61,7 +61,7 @@ module.exports = (function() {
acc[item.Key_name].fields[item.Seq_in_index - 1] = { acc[item.Key_name].fields[item.Seq_in_index - 1] = {
attribute: item.Column_name, attribute: item.Column_name,
length: item.Sub_part || undefined, length: item.Sub_part || undefined,
order: item.Collation === 'A' ? 'ASC' : null order: item.Collation === 'A' ? 'ASC' : undefined
}; };
delete item.column_name; delete item.column_name;
......
...@@ -382,20 +382,14 @@ module.exports = (function() { ...@@ -382,20 +382,14 @@ module.exports = (function() {
return (typeof attribute === 'string') ? attribute : attribute.attribute; return (typeof attribute === 'string') ? attribute : attribute.attribute;
}.bind(this)); }.bind(this));
if (options.name) {
options.indexName = options.name;
}
if (options.method) {
options.indexType = options.method;
}
var indexTable = tableName.split('.'); var indexTable = tableName.split('.');
options = Utils._.extend({ options = Utils._.defaults(options, {
indexType: options.method || undefined,
indicesType: '', indicesType: '',
type: '', type: '',
indexName: Utils.inflection.underscore(indexTable[indexTable.length - 1] + '_' + onlyAttributeNames.join('_')), indexName: options.name || Utils.inflection.underscore(indexTable[indexTable.length - 1] + '_' + onlyAttributeNames.join('_')),
parser: null parser: null
}, options); });
if (options.unique || options.indicesType.toLowerCase() === 'unique' || options.type.toLowerCase() === 'unique') { if (options.unique || options.indicesType.toLowerCase() === 'unique' || options.type.toLowerCase() === 'unique') {
options.indicesType = 'UNIQUE'; options.indicesType = 'UNIQUE';
......
...@@ -112,7 +112,7 @@ module.exports = (function() { ...@@ -112,7 +112,7 @@ module.exports = (function() {
attribute = attributes[index]; attribute = attributes[index];
return { return {
attribute: field, attribute: field,
collation: attribute.match(/COLLATE "(.*?)"/) ? /COLLATE "(.*?)"/.exec(attribute)[1] : undefined, collate: attribute.match(/COLLATE "(.*?)"/) ? /COLLATE "(.*?)"/.exec(attribute)[1] : undefined,
order: attribute.indexOf('DESC') !== -1 ? 'DESC' : attribute.indexOf('ASC') !== -1 ? 'ASC': undefined, order: attribute.indexOf('DESC') !== -1 ? 'DESC' : attribute.indexOf('ASC') !== -1 ? 'ASC': undefined,
length: undefined, length: undefined,
}; };
...@@ -222,7 +222,7 @@ module.exports = (function() { ...@@ -222,7 +222,7 @@ module.exports = (function() {
}; };
Query.prototype.isShowIndexesQuery = function () { Query.prototype.isShowIndexesQuery = function () {
return this.sql.indexOf('pg_get_indexdef') || this.sql.indexOf('pg_index'); return this.sql.indexOf('pg_get_indexdef') !== -1;
}; };
Query.prototype.getInsertIdField = function() { Query.prototype.getInsertIdField = function() {
......
...@@ -337,14 +337,10 @@ module.exports = (function() { ...@@ -337,14 +337,10 @@ module.exports = (function() {
return (typeof attribute === 'string') ? attribute : attribute.attribute; return (typeof attribute === 'string') ? attribute : attribute.attribute;
}.bind(this)); }.bind(this));
if (options.name) {
options.indexName = options.name;
}
options = Utils._.extend({ options = Utils._.extend({
indicesType: '', indicesType: '',
type: '', type: '',
indexName: Utils.inflection.underscore(tableName + '_' + onlyAttributeNames.join('_')), indexName: options.name || Utils.inflection.underscore(tableName + '_' + onlyAttributeNames.join('_')),
parser: null parser: null
}, options); }, options);
......
...@@ -176,7 +176,7 @@ module.exports = (function() { ...@@ -176,7 +176,7 @@ module.exports = (function() {
Query.prototype.handleShowIndexesQuery = function (data) { Query.prototype.handleShowIndexesQuery = function (data) {
var self = this; var self = this;
// Sqlite returns indexes so the last one that was defined last is returned first. Lets reverse that! // Sqlite returns indexes so the one that was defined last is returned first. Lets reverse that!
return this.sequelize.Promise.map(data.reverse(), function (item) { return this.sequelize.Promise.map(data.reverse(), function (item) {
item.fields = []; item.fields = [];
item.primary = false; item.primary = false;
......
...@@ -383,14 +383,14 @@ module.exports = (function() { ...@@ -383,14 +383,14 @@ module.exports = (function() {
var self = this var self = this
, attributes = this.tableAttributes; , attributes = this.tableAttributes;
return Promise.resolve().bind(this).then(function () { return Promise.resolve().then(function () {
if (options.force) { if (options.force) {
return this.drop(options); return self.drop(options);
} }
}).then(function () { }).then(function () {
return this.QueryInterface.createTable(this.getTableName(options), attributes, options); return self.QueryInterface.createTable(self.getTableName(options), attributes, options);
}).then(function () { }).then(function () {
return Promise.map(this.options.indexes, function (index) { return Promise.map(self.options.indexes, function (index) {
return self.QueryInterface.addIndex(self.getTableName(options), index.fields, index); return self.QueryInterface.addIndex(self.getTableName(options), index.fields, index);
}); });
}).return(this); }).return(this);
......
...@@ -199,7 +199,6 @@ module.exports = (function() { ...@@ -199,7 +199,6 @@ module.exports = (function() {
}; };
var skip = options.skip || []; var skip = options.skip || [];
return self.showAllTables().then(function(tableNames) { return self.showAllTables().then(function(tableNames) {
if (self.sequelize.options.dialect === 'sqlite') { if (self.sequelize.options.dialect === 'sqlite') {
return self.sequelize.query('PRAGMA foreign_keys;').then(function(result) { return self.sequelize.query('PRAGMA foreign_keys;').then(function(result) {
......
...@@ -340,7 +340,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -340,7 +340,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
name: 'a_b_uniq', name: 'a_b_uniq',
unique: true, unique: true,
method: 'BTREE', method: 'BTREE',
fields: ['fieldB', {attribute:'fieldA', collate: 'en_US', order: 'DESC', length: 5}] fields: ['fieldB', {attribute:'fieldA', collate: dialect === 'sqlite' ? 'RTRIM' : 'en_US', order: 'DESC', length: 5}]
}, },
{ {
type: 'FULLTEXT', type: 'FULLTEXT',
...@@ -376,12 +376,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -376,12 +376,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
idx2 = arguments[1]; idx2 = arguments[1];
expect(idx1.fields).to.deep.equal([ expect(idx1.fields).to.deep.equal([
{ attribute: 'fieldB', length: undefined, order: undefined}, { attribute: 'fieldB', length: undefined, order: undefined, collate: undefined},
{ attribute: 'fieldA', length: undefined, order: 'DESC', collate: 'en_US'}, { attribute: 'fieldA', length: undefined, order: 'DESC', collate: 'en_US'},
]); ]);
expect(idx2.fields).to.deep.equal([ expect(idx2.fields).to.deep.equal([
{ attribute: 'fieldC', length: undefined, order: null} { attribute: 'fieldC', length: undefined, order: undefined, collate: undefined}
]); ]);
} else { } else {
// And finally mysql returns the primary first, and then the rest in the order they were defined // And finally mysql returns the primary first, and then the rest in the order they were defined
...@@ -400,7 +400,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -400,7 +400,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
]); ]);
expect(idx2.fields).to.deep.equal([ expect(idx2.fields).to.deep.equal([
{ attribute: 'fieldC', length: undefined, order: null} { attribute: 'fieldC', length: undefined, order: undefined}
]); ]);
} }
......
...@@ -836,12 +836,12 @@ if (dialect.match(/^postgres/)) { ...@@ -836,12 +836,12 @@ if (dialect.match(/^postgres/)) {
'isAdmin' 'isAdmin'
] ]
], ],
expectation: "CREATE INDEX \"user_username_is_admin\" ON \"User\" (\"username\"(10) ASC, \"isAdmin\")" expectation: "CREATE INDEX \"user_username_is_admin\" ON \"User\" (\"username\" ASC, \"isAdmin\")"
}, { }, {
arguments: [ arguments: [
'User', ['username', 'isAdmin'], { indicesType: 'FULLTEXT', indexName: 'bar'} 'User', ['username', 'isAdmin'], { indexName: 'bar'}
], ],
expectation: "CREATE FULLTEXT INDEX \"bar\" ON \"User\" (\"username\", \"isAdmin\")" expectation: "CREATE INDEX \"bar\" ON \"User\" (\"username\", \"isAdmin\")"
}, { }, {
arguments: ['mySchema.User', ['username', 'isAdmin']], arguments: ['mySchema.User', ['username', 'isAdmin']],
expectation: 'CREATE INDEX \"user_username_is_admin\" ON \"mySchema\".\"User\" (\"username\", \"isAdmin\")' expectation: 'CREATE INDEX \"user_username_is_admin\" ON \"mySchema\".\"User\" (\"username\", \"isAdmin\")'
...@@ -859,13 +859,13 @@ if (dialect.match(/^postgres/)) { ...@@ -859,13 +859,13 @@ if (dialect.match(/^postgres/)) {
'isAdmin' 'isAdmin'
] ]
], ],
expectation: "CREATE INDEX user_username_is_admin ON User (username(10) ASC, isAdmin)", expectation: "CREATE INDEX user_username_is_admin ON User (username ASC, isAdmin)",
context: {options: {quoteIdentifiers: false}} context: {options: {quoteIdentifiers: false}}
}, { }, {
arguments: [ arguments: [
'User', ['username', 'isAdmin'], { indicesType: 'FULLTEXT', indexName: 'bar'} 'User', ['username', 'isAdmin'], { indexName: 'bar'}
], ],
expectation: "CREATE FULLTEXT INDEX bar ON User (username, isAdmin)", expectation: "CREATE INDEX bar ON User (username, isAdmin)",
context: {options: {quoteIdentifiers: false}} context: {options: {quoteIdentifiers: false}}
}, { }, {
arguments: ['mySchema.User', ['username', 'isAdmin']], arguments: ['mySchema.User', ['username', 'isAdmin']],
......
"use strict";
/* jshint multistr: true */ /* jshint multistr: true */
var chai = require('chai') var chai = require('chai')
, expect = chai.expect , expect = chai.expect
, Support = require(__dirname + '/support') , Support = require(__dirname + '/support')
, DataTypes = require(__dirname + "/../lib/data-types") , DataTypes = require(__dirname + "/../lib/data-types")
, dialect = Support.getTestDialect() , dialect = Support.getTestDialect()
, _ = require('lodash') , _ = require('lodash');
chai.config.includeStack = true chai.config.includeStack = true;
describe(Support.getTestDialectTeaser("QueryGenerators"), function () { describe(Support.getTestDialectTeaser("QueryGenerators"), function () {
describe("comments", function() { describe("comments", function() {
it("should create a comment for a column", function(done) { it("should create a comment for a column", function() {
var self = this var self = this
, User = this.sequelize.define('User', { , User = this.sequelize.define('User', {
username: {type: DataTypes.STRING, comment: 'Some lovely info for my DBA'} username: {type: DataTypes.STRING, comment: 'Some lovely info for my DBA'}
}) });
User.sync({ force: true }).success(function() { return User.sync({ force: true }).then(function() {
var sql = '' var sql = '';
if (Support.dialectIsMySQL()) { if (Support.dialectIsMySQL()) {
sql = 'SELECT COLUMN_COMMENT as cmt FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = \'' + self.sequelize.config.database + '\' AND TABLE_NAME = \'Users\' AND COLUMN_NAME = \'username\''; sql = 'SELECT COLUMN_COMMENT as cmt FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = \'' + self.sequelize.config.database + '\' AND TABLE_NAME = \'Users\' AND COLUMN_NAME = \'username\'';
} }
...@@ -30,19 +32,14 @@ describe(Support.getTestDialectTeaser("QueryGenerators"), function () { ...@@ -30,19 +32,14 @@ describe(Support.getTestDialectTeaser("QueryGenerators"), function () {
} }
else if (dialect === "sqlite") { else if (dialect === "sqlite") {
// sqlite doesn't support comments except for explicit comments in the file // sqlite doesn't support comments except for explicit comments in the file
expect(true).to.be.true expect(true).to.be.true;
return done() return;
} else {
console.log('FIXME: This dialect is not supported :(');
expect(true).to.be.true
return done()
} }
self.sequelize.query(sql, null, {raw: true}).success(function(result) { return self.sequelize.query(sql, null, {raw: true}).then(function(result) {
expect(result[0].cmt).to.equal('Some lovely info for my DBA'); expect(result[0].cmt).to.equal('Some lovely info for my DBA');
done() });
}) });
}) });
}) });
}) });
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!