Skip to content

Commit

Permalink
Table unique constraint can be array of arrays (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
dolezel authored Oct 9, 2017
1 parent 78fa3ef commit 26c0faf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/operations/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,15 @@ const parseConstraints = (table, options) => {
constraints.push(`CONSTRAINT "${table_name}_chck" CHECK (${check})`);
}
if (unique) {
constraints.push(`CONSTRAINT "${table_name}_uniq" UNIQUE (${quote(_.isArray(unique) ? unique : [unique]).join(', ')})`);
const uniqueArray = _.isArray(unique) ? unique : [unique];
const isArrayOfArrays = uniqueArray.some(uniqueSet => _.isArray(uniqueSet));
if (isArrayOfArrays) {
uniqueArray.forEach((uniqueSet, i) =>
constraints.push(`CONSTRAINT "${table_name}_uniq_${i + 1}" UNIQUE (${quote(_.isArray(uniqueSet) ? uniqueSet : [uniqueSet]).join(', ')})`)
);
} else {
constraints.push(`CONSTRAINT "${table_name}_uniq" UNIQUE (${quote(uniqueArray).join(', ')})`);
}
}
if (primaryKey) {
constraints.push(`CONSTRAINT "${table_name}_pkey" PRIMARY KEY (${quote(_.isArray(primaryKey) ? primaryKey : [primaryKey]).join(', ')})`);
Expand Down
53 changes: 52 additions & 1 deletion test/tables-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ describe('lib/operations/tables', () => {
});

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' } } });
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" MATCH SIMPLE
);`);
Expand Down Expand Up @@ -72,6 +77,52 @@ describe('lib/operations/tables', () => {
"a" integer,
"b" varchar,
FOREIGN KEY ("a", "b") REFERENCES otherTable (A, B) MATCH SIMPLE
);`);
});

it('check table unique constraint work correctly', () => {
const sql = Tables.create()(
'my_table_name',
{
a: { type: 'integer' },
b: { type: 'varchar' },
},
{
constraints: {
unique: ['a', 'b'],
},
}
);
expect(sql).to.equal(`CREATE TABLE "my_table_name" (
"a" integer,
"b" varchar,
CONSTRAINT "my_table_name_uniq" UNIQUE ("a", "b")
);`);
});

it('check table unique constraint work correctly for array of arrays', () => {
const sql = Tables.create()(
'my_table_name',
{
a: { type: 'integer' },
b: { type: 'varchar' },
c: { type: 'varchar' },
},
{
constraints: {
unique: [
['a', 'b'],
'c',
],
},
}
);
expect(sql).to.equal(`CREATE TABLE "my_table_name" (
"a" integer,
"b" varchar,
"c" varchar,
CONSTRAINT "my_table_name_uniq_1" UNIQUE ("a", "b"),
CONSTRAINT "my_table_name_uniq_2" UNIQUE ("c")
);`);
});
});
Expand Down

0 comments on commit 26c0faf

Please sign in to comment.