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

Commit 1fed4a32 by Sorin Neacsu

Fix Postgres showIndex not returning functional indices

1 parent c9c00b45
# Next
- [FIXED] Show indexes query on Postgres fails to return functional indexes [#3911](https://github.com/sequelize/sequelize/issues/3911)
- [FIXED] Custom field names in json queries
- [FIXED] JSON cast key using the equality operator. [#3824](https://github.com/sequelize/sequelize/issues/3824)
- [FIXED] Map column names with `.field` in scopes with includes. [#4210](https://github.com/sequelize/sequelize/issues/4210)
......
......@@ -439,7 +439,7 @@ var QueryGenerator = {
var query = 'SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, ' +
'array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) ' +
'AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a ' +
'WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND a.attnum = ANY(ix.indkey) AND '+
'WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND '+
"t.relkind = 'r' and t.relname = '<%= tableName %>' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;";
return Utils._.template(query)({ tableName: tableName });
......
......@@ -170,6 +170,10 @@ Query.prototype.run = function(sql) {
// Indkey is the order of attributes in the index, specified by a string of attribute indexes
result.fields = result.indkey.split(' ').map(function (indKey, index) {
field = columns[indKey];
// for functional indices indKey = 0
if(!field) {
return null;
}
attribute = attributes[index];
return {
attribute: field,
......@@ -177,7 +181,7 @@ Query.prototype.run = function(sql) {
order: attribute.indexOf('DESC') !== -1 ? 'DESC' : attribute.indexOf('ASC') !== -1 ? 'ASC': undefined,
length: undefined
};
});
}).filter(function(n){ return n !== null; });
delete result.columns;
});
return results;
......
'use strict';
/* jshint -W030 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../../support')
, dialect = Support.getTestDialect()
, DataTypes = require(__dirname + '/../../../../lib/data-types')
, _ = require('lodash');
if (dialect.match(/^postgres/)) {
describe('[POSTGRES Specific] QueryInterface', function () {
beforeEach(function () {
this.sequelize.options.quoteIdenifiers = true;
this.queryInterface = this.sequelize.getQueryInterface();
});
describe('indexes', function () {
beforeEach(function () {
var self = this;
return this.queryInterface.dropTable('Group').then(function () {
return self.queryInterface.createTable('Group', {
username: DataTypes.STRING,
isAdmin: DataTypes.BOOLEAN,
from: DataTypes.STRING
});
});
});
it('adds, reads and removes a named functional index to the table', function () {
var self = this;
return this.queryInterface.addIndex('Group', [this.sequelize.fn('lower', this.sequelize.col('username'))], {
name: 'group_username_lower'
}).then(function () {
return self.queryInterface.showIndex('Group').then(function (indexes) {
var indexColumns = _.uniq(indexes.map(function (index) {
return index.name;
}));
expect(indexColumns).to.include('group_username_lower');
return self.queryInterface.removeIndex('Group', 'group_username_lower').then(function () {
return self.queryInterface.showIndex('Group').then(function (indexes) {
indexColumns = _.uniq(indexes.map(function (index) {
return index.name;
}));
expect(indexColumns).to.be.empty;
});
});
});
});
});
});
});
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!