From 6c0ce1f91f5249ede73d9a3202a29bbac5a956c5 Mon Sep 17 00:00:00 2001 From: Aaron Schrab Date: Fri, 25 Aug 2017 01:16:11 -0400 Subject: [PATCH] Support creating indexes on table with schema When creating an index on a table where the schema is specified, the schema shouldn't be included in the index name. --- lib/operations/indexes.js | 5 ++++- test/indexes-test.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 test/indexes-test.js diff --git a/lib/operations/indexes.js b/lib/operations/indexes.js index d53e9302e..a1350a8db 100644 --- a/lib/operations/indexes.js +++ b/lib/operations/indexes.js @@ -1,7 +1,10 @@ import _ from 'lodash'; import { template } from '../utils'; -function generateIndexName(table_name, columns, options) { +function generateIndexName(table, columns, options) { + const table_name = typeof table === 'object' + ? table.name + : table; return options.name ? options.name : template`${table_name}_${_.isArray(columns) ? columns.join('_') : columns}${options.unique ? '_unique' : ''}_index`; diff --git a/test/indexes-test.js b/test/indexes-test.js new file mode 100644 index 000000000..62a88ede1 --- /dev/null +++ b/test/indexes-test.js @@ -0,0 +1,11 @@ +import { expect } from 'chai'; +import * as Indexes from '../lib/operations/indexes'; + +describe('lib/operations/indexes', () => { + describe('.create', () => { + it('check schema not included in index name', () => { + const sql = Indexes.create({ schema: 'a', name: 'b' }, ['c', 'd']); + expect(sql).to.equal('CREATE INDEX "b_c_d_index" ON "a"."b" ("c", "d");'); + }); + }); +});