Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support object with schema and table name in more places #105

Merged
merged 3 commits into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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