Merge pull request #517 from briandela/master

Add support for specifying the postgres index type
This commit is contained in:
Tim Griesser 2014-10-03 11:04:55 -04:00
commit c0070891a3
2 changed files with 29 additions and 3 deletions

View File

@ -67,9 +67,9 @@ TableCompiler_PG.prototype.unique = function(columns, indexName) {
this.pushQuery('alter table ' + this.tableName() + ' add constraint ' + indexName +
' unique (' + this.formatter.columnize(columns) + ')');
};
TableCompiler_PG.prototype.index = function(columns, indexName) {
TableCompiler_PG.prototype.index = function(columns, indexName, indexType) {
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery('create index ' + indexName + ' on ' + this.tableName() +
this.pushQuery('create index ' + indexName + ' on ' + this.tableName() + (indexType && (' using ' + indexType) || '') +
' (' + this.formatter.columnize(columns) + ')');
};
TableCompiler_PG.prototype.dropPrimary = function() {
@ -91,4 +91,4 @@ TableCompiler_PG.prototype.dropForeign = function(columns, indexName) {
client.TableBuilder = TableBuilder_PG;
client.TableCompiler = TableCompiler_PG;
};
};

View File

@ -213,6 +213,32 @@ module.exports = function(client) {
expect(tableSql[1].sql).to.equal('create index users_name_index on "users" ("name")');
});
it("adding index with an index type", function() {
tableSql = new SchemaBuilder().table('users', function(table) {
table.index(['foo', 'bar'], 'baz', 'gist');
}).toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('create index baz on "users" using gist ("foo", "bar")');
});
it("adding index with an index type fluently", function() {
tableSql = new SchemaBuilder().table('users', function(table) {
table.string('name').index('baz', 'gist');
}).toSQL();
equal(2, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table "users" add column "name" varchar(255)');
expect(tableSql[1].sql).to.equal('create index baz on "users" using gist ("name")');
});
it("adding index with an index type and default name fluently", function() {
tableSql = new SchemaBuilder().table('users', function(table) {
table.string('name').index(null, 'gist');
}).toSQL();
equal(2, tableSql.length);
expect(tableSql[0].sql).to.equal('alter table "users" add column "name" varchar(255)');
expect(tableSql[1].sql).to.equal('create index users_name_index on "users" using gist ("name")');
});
it("adding incrementing id", function() {
tableSql = new SchemaBuilder().table('users', function(table) {
table.increments('id');