Skip to content

Commit

Permalink
Add support for opclass option in createIndex (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
uLan08 authored and dolezel committed May 17, 2018
1 parent 35fbee1 commit 668d54b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* `unique` _[boolean]_ - set to true if this is a unique index
* `where` _[string]_ - raw sql for where clause of index
* `concurrently` _[boolean]_ - create this index concurrently
* `opclass` _[string]_ - name of an operator class to use
* `method` _[string]_ - btree | hash | gist | spgist | gin

**Aliases:** `addIndex`
Expand Down
4 changes: 3 additions & 1 deletion lib/operations/indexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const createIndex = (tableName, columns, options = {}) => {
unique - is this a unique index
where - where clause
concurrently -
opclass - name of an operator class
options.method - [ btree | hash | gist | spgist | gin ]
*/
const indexName = generateIndexName(tableName, columns, options);
Expand All @@ -44,8 +45,9 @@ export const createIndex = (tableName, columns, options = {}) => {
const concurrently = options.concurrently ? " CONCURRENTLY " : "";
const method = options.method ? ` USING ${options.method}` : "";
const where = options.where ? ` WHERE ${options.where}` : "";
const opclass = options.opclass ? ` ${options.opclass}` : "";

return template`CREATE ${unique} INDEX ${concurrently} "${indexName}" ON "${tableName}"${method} (${columnsString})${where};`;
return template`CREATE ${unique} INDEX ${concurrently} "${indexName}" ON "${tableName}"${method} (${columnsString})${opclass}${where};`;
};

export const dropIndex = (tableName, columns, options = {}) => {
Expand Down
12 changes: 12 additions & 0 deletions test/indexes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,17 @@ describe("lib/operations/indexes", () => {
'CREATE INDEX "b_c_d_index" ON "a"."b" ("c", "d");'
);
});

it("add opclass option", () => {
const sql = Indexes.createIndex("x", ["y"], {
method: "gist",
name: "z",
opclass: "some_opclass",
where: "some condition"
});
expect(sql).to.equal(
'CREATE INDEX "z" ON "x" USING gist ("y") some_opclass WHERE some condition;'
);
});
});
});

0 comments on commit 668d54b

Please sign in to comment.