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

Table unique constraint can be array of arrays #126

Merged
merged 1 commit into from
Oct 9, 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
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