Skip to content

Commit

Permalink
Support object with schema and table name in more places (#105)
Browse files Browse the repository at this point in the history
* Support references to other schemas

* Don't include schema in primary key constraint name

* 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.
  • Loading branch information
aschrab authored and dolezel committed Aug 28, 2017
1 parent 44a6689 commit 85bb309
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/operations/indexes.js
Original file line number Diff line number Diff line change
@@ -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`;
Expand Down
6 changes: 4 additions & 2 deletions lib/operations/tables.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -35,7 +37,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}`);
}
Expand Down
11 changes: 11 additions & 0 deletions test/indexes-test.js
Original file line number Diff line number Diff line change
@@ -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");');
});
});
});
19 changes: 19 additions & 0 deletions test/tables-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ 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"
);`);
});

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")
);`);
});
});
Expand Down

0 comments on commit 85bb309

Please sign in to comment.