From c55aaf9b0e833c4cf3931837a6591d1beae542aa Mon Sep 17 00:00:00 2001 From: Aaron Schrab Date: Fri, 25 Aug 2017 00:44:31 -0400 Subject: [PATCH 1/3] Support references to other schemas --- lib/operations/tables.js | 2 +- test/tables-test.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/operations/tables.js b/lib/operations/tables.js index 1b1686dd..2d966eb9 100644 --- a/lib/operations/tables.js +++ b/lib/operations/tables.js @@ -35,7 +35,7 @@ function parseColumns(columns, table_name, extending_type_shorthands = {}) { constraints.push(`CHECK (${options.check})`); } if (options.references) { - constraints.push(`REFERENCES ${options.references}`); + constraints.push(template`REFERENCES "${options.references}"`); if (options.onDelete) { constraints.push(`ON DELETE ${options.onDelete}`); } diff --git a/test/tables-test.js b/test/tables-test.js index e74f3c93..88ee7bbe 100644 --- a/test/tables-test.js +++ b/test/tables-test.js @@ -21,6 +21,13 @@ describe('lib/operations/tables', () => { const sql = Tables.create({ id: { type: 'uuid', primaryKey: true } })('my_table_name', { id: 'id' }); expect(sql).to.equal(`CREATE TABLE "my_table_name" ( "id" uuid PRIMARY KEY +);`); + }); + + it('check schemas can be used for foreign keys', () => { + const sql = Tables.create()('my_table_name', { parent_id: { type: 'integer', references: { schema: 'a', name: 'b' } } }); + expect(sql).to.equal(`CREATE TABLE "my_table_name" ( + "parent_id" integer REFERENCES "a"."b" );`); }); }); From 73ed8ccd924301a6a3fe03a2a8cf0e655ed3111a Mon Sep 17 00:00:00 2001 From: Aaron Schrab Date: Fri, 25 Aug 2017 00:59:10 -0400 Subject: [PATCH 2/3] Don't include schema in primary key constraint name --- lib/operations/tables.js | 4 +++- test/tables-test.js | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/operations/tables.js b/lib/operations/tables.js index 2d966eb9..b2293a18 100644 --- a/lib/operations/tables.js +++ b/lib/operations/tables.js @@ -1,12 +1,14 @@ import _ from 'lodash'; import { escapeValue, template, quote, applyType, applyTypeAdapters } from '../utils'; -function parseColumns(columns, table_name, extending_type_shorthands = {}) { +function parseColumns(columns, table, extending_type_shorthands = {}) { let columnsWithOptions = _.mapValues( columns, column => applyType(column, extending_type_shorthands) ); + const table_name = typeof table === 'object' ? table.name : table; + const primaryColumns = _.chain(columnsWithOptions) .map((options, column_name) => (options.primaryKey ? column_name : null)) .filter() diff --git a/test/tables-test.js b/test/tables-test.js index 88ee7bbe..e1263ec0 100644 --- a/test/tables-test.js +++ b/test/tables-test.js @@ -28,6 +28,18 @@ describe('lib/operations/tables', () => { const sql = Tables.create()('my_table_name', { parent_id: { type: 'integer', references: { schema: 'a', name: 'b' } } }); expect(sql).to.equal(`CREATE TABLE "my_table_name" ( "parent_id" integer REFERENCES "a"."b" +);`); + }); + + it('check multicolumn primary key name does not include schema', () => { + const sql = Tables.create()({ schema: 's', name: 'my_table_name' }, { + a: { type: 'integer', primaryKey: true }, + b: { type: 'varchar', primaryKey: true }, + }); + expect(sql).to.equal(`CREATE TABLE "s"."my_table_name" ( + "a" integer, + "b" varchar, + CONSTRAINT "my_table_name_pkey" PRIMARY KEY ("a", "b") );`); }); }); From 8d04852b9550c4dcf1ef94837669e0680bffe2bf Mon Sep 17 00:00:00 2001 From: Aaron Schrab Date: Fri, 25 Aug 2017 01:16:11 -0400 Subject: [PATCH 3/3] 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 d53e9302..a1350a8d 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 00000000..62a88ede --- /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");'); + }); + }); +});