Skip to content

Commit

Permalink
fix: creating a unique index in postgres using the index method (#5601)
Browse files Browse the repository at this point in the history
  • Loading branch information
awo00 committed Jul 4, 2023
1 parent c8da0df commit 2da5822
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
13 changes: 9 additions & 4 deletions lib/dialects/postgres/schema/pg-tablecompiler.js
Expand Up @@ -235,21 +235,26 @@ class TableCompiler_PG extends TableCompiler {
: this._indexCommand('index', this.tableNameRaw, columns);

let predicate;
let storageEngineIndexType;
let indexType;

if (isString(options)) {
indexType = options;
storageEngineIndexType = options;
} else if (isObject(options)) {
({ indexType, predicate } = options);
({ indexType, storageEngineIndexType, predicate } = options);
}

const predicateQuery = predicate
? ' ' + this.client.queryCompiler(predicate).where()
: '';

this.pushQuery(
`create index ${indexName} on ${this.tableName()}${
(indexType && ` using ${indexType}`) || ''
`create${
typeof indexType === 'string' && indexType.toLowerCase() === 'unique'
? ' unique'
: ''
} index ${indexName} on ${this.tableName()}${
(storageEngineIndexType && ` using ${storageEngineIndexType}`) || ''
}` +
' (' +
this.formatter.columnize(columns) +
Expand Down
27 changes: 21 additions & 6 deletions test/unit/schema-builder/postgres.js
Expand Up @@ -973,7 +973,7 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});

it('adding index with an index type', function () {
it('adding index with a storage engine index type', function () {
tableSql = client
.schemaBuilder()
.table('users', function (table) {
Expand All @@ -986,7 +986,7 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});

it('adding index with an index type fluently', function () {
it('adding index with a storage engine index type fluently', function () {
tableSql = client
.schemaBuilder()
.table('users', function (table) {
Expand All @@ -1002,11 +1002,11 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});

it('adding index with an index type and default name fluently', function () {
it('adding index with a storage engine index type and default name fluently', function () {
tableSql = client
.schemaBuilder()
.table('users', function (table) {
table.string('name').index(null, { indexType: 'gist' });
table.string('name').index(null, { storageEngineIndexType: 'gist' });
})
.toSQL();
equal(2, tableSql.length);
Expand All @@ -1033,12 +1033,27 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});

it('adding index with an index type and a predicate', function () {
it('adding unique index using index method', function () {
tableSql = client
.schemaBuilder()
.table('users', function (table) {
table.index(['foo', 'bar'], 'baz', {
indexType: 'gist',
indexType: 'unique',
});
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'create unique index "baz" on "users" ("foo", "bar")'
);
});

it('adding index with a storage engine index type and a predicate', function () {
tableSql = client
.schemaBuilder()
.table('users', function (table) {
table.index(['foo', 'bar'], 'baz', {
storageEngineIndexType: 'gist',
predicate: client.queryBuilder().whereRaw('email = "foo@bar"'),
});
})
Expand Down

0 comments on commit 2da5822

Please sign in to comment.