Skip to content
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
3 changes: 2 additions & 1 deletion forward_engineering/configs/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ module.exports = {

addColumn: 'ADD ${script}',

addCheckConstraint: 'ALTER TABLE ${tableName} ADD CONSTRAINT ${constraintName} CHECK (${expression})${terminator}',
addCheckConstraint:
'ALTER TABLE ${tableName} ADD CONSTRAINT ${constraintName} ${check} (${expression})${terminator}',

addNotNullConstraint: 'ALTER TABLE ${tableName} ALTER COLUMN ${columnName} ${columnType} NOT NULL${terminator}',

Expand Down
9 changes: 5 additions & 4 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ module.exports = (baseProvider, options, app) => {
return createTableIndex(terminator, tableName, index, isActivated && isParentActivated);
},

createCheckConstraint(checkConstraint) {
createCheckConstraint(checkConstraint, isInline = true) {
return assignTemplates(templates.checkConstraint, {
name: checkConstraint.name,
check: checkConstraint.check ? 'CHECK' : 'NO CHECK',
check: checkConstraint.check || isInline ? 'CHECK' : 'NO CHECK',
notForReplication: checkConstraint.enforceForReplication ? '' : ' NOT FOR REPLICATION',
expression: _.trim(checkConstraint.expression).replace(/^\(([\s\S]*)\)$/, '$1'),
terminator,
Expand Down Expand Up @@ -717,7 +717,7 @@ module.exports = (baseProvider, options, app) => {
alterTableAddCheckConstraint(fullTableName, checkConstraint) {
return assignTemplates(templates.alterTableAddConstraint, {
tableName: fullTableName,
constraint: this.createCheckConstraint(checkConstraint),
constraint: this.createCheckConstraint(checkConstraint, false),
terminator,
});
},
Expand Down Expand Up @@ -971,12 +971,13 @@ module.exports = (baseProvider, options, app) => {
});
},

addCheckConstraint(tableName, constraintName, expression) {
addCheckConstraint(tableName, constraintName, expression, check) {
const templateConfig = {
tableName,
constraintName,
expression,
terminator,
check: check ? 'CHECK' : 'NO CHECK',
};
return assignTemplates(templates.addCheckConstraint, templateConfig);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = (app, options) => {
}),
);
const checkConstraints = (jsonSchema.chkConstr || []).map(check =>
ddlProvider.createCheckConstraint(ddlProvider.hydrateCheckConstraint(check)),
ddlProvider.createCheckConstraint(ddlProvider.hydrateCheckConstraint(check), true),
);
const tableData = {
name: tableName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ const getAddCheckConstraintScriptDtos = (_, ddlProvider) => (constraintHistory,
return constraintHistory
.filter(historyEntry => historyEntry.new && !historyEntry.old)
.map(historyEntry => {
const { chkConstrName, constrExpression } = historyEntry.new;
return ddlProvider.addCheckConstraint(fullTableName, wrapInBrackets(chkConstrName), constrExpression);
const { chkConstrName, constrCheck, constrExpression } = historyEntry.new;
return ddlProvider.addCheckConstraint(
fullTableName,
wrapInBrackets(chkConstrName),
constrExpression,
constrCheck,
);
})
.map(script => AlterScriptDto.getInstance([script], true, false));
};
Expand All @@ -94,12 +99,16 @@ const getUpdateCheckConstraintScriptDtos = (_, ddlProvider) => (constraintHistor
.map(historyEntry => {
const { chkConstrName: oldConstrainName } = historyEntry.old;
const dropConstraintScript = ddlProvider.dropConstraint(fullTableName, wrapInBrackets(oldConstrainName));

const { chkConstrName: newConstrainName, constrExpression: newConstraintExpression } = historyEntry.new;
const {
chkConstrName: newConstrainName,
constrCheck,
constrExpression: newConstraintExpression,
} = historyEntry.new;
const addConstraintScript = ddlProvider.addCheckConstraint(
fullTableName,
wrapInBrackets(newConstrainName),
newConstraintExpression,
constrCheck,
);

return [
Expand Down