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

Fixing addConstraint method with object expression #176

Merged
merged 2 commits into from
Feb 6, 2018
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
38 changes: 19 additions & 19 deletions lib/operations/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const parseColumns = (columns, extendingTypeShorthands = {}) => {
};
};

const parseConstraints = (table, options) => {
const parseConstraints = (table, options, genName) => {
const {
check,
unique,
Expand All @@ -118,39 +118,37 @@ const parseConstraints = (table, options) => {
const tableName = typeof table === 'object' ? table.name : table;
const constraints = [];
if (check) {
constraints.push(`CONSTRAINT "${tableName}_chck" CHECK (${check})`);
constraints.push(`${genName ? `CONSTRAINT "${tableName}_chck" ` : ''}CHECK (${check})`);
}
if (unique) {
const uniqueArray = _.isArray(unique) ? unique : [unique];
const isArrayOfArrays = uniqueArray.some(uniqueSet => _.isArray(uniqueSet));
if (isArrayOfArrays) {
uniqueArray.forEach((uniqueSet, i) =>
constraints.push(`CONSTRAINT "${tableName}_uniq_${i + 1}" UNIQUE (${quote(_.isArray(uniqueSet) ? uniqueSet : [uniqueSet]).join(', ')})`)
);
} else {
constraints.push(`CONSTRAINT "${tableName}_uniq" UNIQUE (${quote(uniqueArray).join(', ')})`);
}
(isArrayOfArrays ? uniqueArray : [uniqueArray])
.forEach((uniqueSet) => {
const cols = _.isArray(uniqueSet) ? uniqueSet : [uniqueSet];
constraints.push(`${genName ? `CONSTRAINT "${tableName}_uniq_${cols.join('_')}" ` : ''}UNIQUE (${quote(cols).join(', ')})`);
});
}
if (primaryKey) {
constraints.push(`CONSTRAINT "${tableName}_pkey" PRIMARY KEY (${quote(_.isArray(primaryKey) ? primaryKey : [primaryKey]).join(', ')})`);
constraints.push(`${genName ? `CONSTRAINT "${tableName}_pkey" ` : ''}PRIMARY KEY (${quote(_.isArray(primaryKey) ? primaryKey : [primaryKey]).join(', ')})`);
}
if (foreignKeys) {
(_.isArray(foreignKeys) ? foreignKeys : [foreignKeys])
.forEach((fk) => {
const {
columns,
} = fk;
constraints.push(`FOREIGN KEY (${quote(_.isArray(columns) ? columns : [columns]).join(', ')}) ${parseReferences(fk)}`);
const cols = _.isArray(columns) ? columns : [columns];
constraints.push(`${genName ? `CONSTRAINT "${tableName}_fk_${cols.join('_')}" ` : ''}FOREIGN KEY (${quote(cols).join(', ')}) ${parseReferences(fk)}`);
});
}
if (exclude) {
constraints.push(`CONSTRAINT "${tableName}_excl" EXCLUDE ${exclude}`);
}
if (deferrable) {
constraints.push(parseDeferrable(options));
constraints.push(`${genName ? `CONSTRAINT "${tableName}_excl" ` : ''}EXCLUDE ${exclude}`);
}

return constraints;
return deferrable
? constraints.map(constraint => `${constraint} ${parseDeferrable(options)}`)
: constraints;
};

// TABLE
Expand All @@ -176,7 +174,7 @@ export const create = (typeShorthands) => {
}

const constraints = { ...optionsConstraints, ...columnsConstraints };
const constraintLines = parseConstraints(tableName, constraints);
const constraintLines = parseConstraints(tableName, constraints, true);
const tableDefinition = [
...columnLines,
...constraintLines,
Expand Down Expand Up @@ -258,9 +256,11 @@ export const renameConstraint = (tableName, constraintName, newName) =>
export const undoRenameConstraint = (tableName, constraintName, newName) =>
renameConstraint(tableName, newName, constraintName);

// CONSTRAINTS -- only supports named check constraints
export const addConstraint = (tableName, constraintName, expression) =>
template`ALTER TABLE "${tableName}" ADD${constraintName ? ` CONSTRAINT "${constraintName}"` : ''} ${typeof expression === 'string' ? expression : parseConstraints(tableName, expression)};`;
template`ALTER TABLE "${tableName}"\n${formatLines(
typeof expression === 'string' ? [expression] : parseConstraints(tableName, expression, false),
` ADD CONSTRAINT "${constraintName}" `
)};`;

export const dropConstraint = (tableName, constraintName, { ifExists, cascade } = {}) =>
template`ALTER TABLE "${tableName}" DROP CONSTRAINT${ifExists ? ' IF EXISTS' : ''} "${constraintName}"${cascade ? ' CASCADE' : ''};`;
Expand Down
8 changes: 4 additions & 4 deletions test/tables-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('lib/operations/tables', () => {
expect(sql).to.equal(`CREATE TABLE "my_table_name" (
"a" integer,
"b" varchar,
FOREIGN KEY ("a", "b") REFERENCES otherTable (A, B)
CONSTRAINT "my_table_name_fk_a_b" FOREIGN KEY ("a", "b") REFERENCES otherTable (A, B)
);`);
});

Expand All @@ -109,7 +109,7 @@ describe('lib/operations/tables', () => {
expect(sql).to.equal(`CREATE TABLE "my_table_name" (
"a" integer,
"b" varchar,
CONSTRAINT "my_table_name_uniq" UNIQUE ("a", "b")
CONSTRAINT "my_table_name_uniq_a_b" UNIQUE ("a", "b")
);`);
});

Expand All @@ -134,8 +134,8 @@ describe('lib/operations/tables', () => {
"a" integer,
"b" varchar,
"c" varchar,
CONSTRAINT "my_table_name_uniq_1" UNIQUE ("a", "b"),
CONSTRAINT "my_table_name_uniq_2" UNIQUE ("c")
CONSTRAINT "my_table_name_uniq_a_b" UNIQUE ("a", "b"),
CONSTRAINT "my_table_name_uniq_c" UNIQUE ("c")
);`);
});
});
Expand Down