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

Commit a3dbf684 by Jan Aagaard Meier

Cleaning up indexes code a bit

1 parent d87330ef
......@@ -257,16 +257,11 @@ module.exports = (function() {
} else {
options.indicesType = options.indicesType || options.type;
}
if (options.name) {
options.indexName = options.name;
}
if (options.method) {
options.indexType = options.method;
}
options = Utils._.extend({
indexType: options.method || undefined,
indicesType: null,
indexName: Utils.inflection.underscore(tableName + '_' + onlyAttributeNames.join('_')),
indexName: options.name || Utils.inflection.underscore(tableName + '_' + onlyAttributeNames.join('_')),
parser: null
}, options);
......
......@@ -61,7 +61,7 @@ module.exports = (function() {
acc[item.Key_name].fields[item.Seq_in_index - 1] = {
attribute: item.Column_name,
length: item.Sub_part || undefined,
order: item.Collation === 'A' ? 'ASC' : null
order: item.Collation === 'A' ? 'ASC' : undefined
};
delete item.column_name;
......
......@@ -382,20 +382,14 @@ module.exports = (function() {
return (typeof attribute === 'string') ? attribute : attribute.attribute;
}.bind(this));
if (options.name) {
options.indexName = options.name;
}
if (options.method) {
options.indexType = options.method;
}
var indexTable = tableName.split('.');
options = Utils._.extend({
options = Utils._.defaults(options, {
indexType: options.method || undefined,
indicesType: '',
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
}, options);
});
if (options.unique || options.indicesType.toLowerCase() === 'unique' || options.type.toLowerCase() === 'unique') {
options.indicesType = 'UNIQUE';
......
......@@ -112,7 +112,7 @@ module.exports = (function() {
attribute = attributes[index];
return {
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,
length: undefined,
};
......@@ -222,7 +222,7 @@ module.exports = (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() {
......
......@@ -337,14 +337,10 @@ module.exports = (function() {
return (typeof attribute === 'string') ? attribute : attribute.attribute;
}.bind(this));
if (options.name) {
options.indexName = options.name;
}
options = Utils._.extend({
indicesType: '',
type: '',
indexName: Utils.inflection.underscore(tableName + '_' + onlyAttributeNames.join('_')),
indexName: options.name || Utils.inflection.underscore(tableName + '_' + onlyAttributeNames.join('_')),
parser: null
}, options);
......
......@@ -176,7 +176,7 @@ module.exports = (function() {
Query.prototype.handleShowIndexesQuery = function (data) {
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) {
item.fields = [];
item.primary = false;
......
......@@ -383,14 +383,14 @@ module.exports = (function() {
var self = this
, attributes = this.tableAttributes;
return Promise.resolve().bind(this).then(function () {
return Promise.resolve().then(function () {
if (options.force) {
return this.drop(options);
return self.drop(options);
}
}).then(function () {
return this.QueryInterface.createTable(this.getTableName(options), attributes, options);
return self.QueryInterface.createTable(self.getTableName(options), attributes, options);
}).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(this);
......
......@@ -199,7 +199,6 @@ module.exports = (function() {
};
var skip = options.skip || [];
return self.showAllTables().then(function(tableNames) {
if (self.sequelize.options.dialect === 'sqlite') {
return self.sequelize.query('PRAGMA foreign_keys;').then(function(result) {
......
......@@ -340,7 +340,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
name: 'a_b_uniq',
unique: true,
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',
......@@ -376,12 +376,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
idx2 = arguments[1];
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'},
]);
expect(idx2.fields).to.deep.equal([
{ attribute: 'fieldC', length: undefined, order: null}
{ attribute: 'fieldC', length: undefined, order: undefined, collate: undefined}
]);
} else {
// 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 () {
]);
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/)) {
'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: [
'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']],
expectation: 'CREATE INDEX \"user_username_is_admin\" ON \"mySchema\".\"User\" (\"username\", \"isAdmin\")'
......@@ -859,13 +859,13 @@ if (dialect.match(/^postgres/)) {
'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}}
}, {
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}}
}, {
arguments: ['mySchema.User', ['username', 'isAdmin']],
......
"use strict";
/* jshint multistr: true */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/support')
, DataTypes = require(__dirname + "/../lib/data-types")
, dialect = Support.getTestDialect()
, _ = require('lodash')
, _ = require('lodash');
chai.config.includeStack = true
chai.config.includeStack = true;
describe(Support.getTestDialectTeaser("QueryGenerators"), 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
, User = this.sequelize.define('User', {
username: {type: DataTypes.STRING, comment: 'Some lovely info for my DBA'}
})
});
User.sync({ force: true }).success(function() {
var sql = ''
return User.sync({ force: true }).then(function() {
var sql = '';
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\'';
}
......@@ -30,19 +32,14 @@ describe(Support.getTestDialectTeaser("QueryGenerators"), function () {
}
else if (dialect === "sqlite") {
// sqlite doesn't support comments except for explicit comments in the file
expect(true).to.be.true
return done()
} else {
console.log('FIXME: This dialect is not supported :(');
expect(true).to.be.true
return done()
expect(true).to.be.true;
return;
}
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');
done()
})
})
})
})
})
});
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!