Skip to content

Commit 174d254

Browse files
add base implementation to support insile check constr from field level
1 parent 70524ce commit 174d254

4 files changed

Lines changed: 255 additions & 2 deletions

File tree

forward_engineering/ddlProvider/ddlHelpers/constraintsHelper.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,30 @@ const createKeyConstraint = (templates, isParentActivated) => keyData => {
100100
};
101101
};
102102

103+
/**
104+
* Cleans the check constraint expression by removing surrounding parentheses and trimming whitespace.
105+
* @param expression {string}
106+
* @returns string
107+
*/
108+
const cleanCheckConstraint = (expression = '') => _.trim(expression).replace(/^\(([\s\S]*)\)$/, '$1');
109+
110+
/**
111+
* Creates an inline check constraint statement.
112+
* @param checkConstraint {{
113+
* name?: string,
114+
* expression?: string,
115+
* noInherit?: boolean,
116+
* }}
117+
* @returns string
118+
*/
119+
const createInlineCheckConstraint = checkConstraint => {
120+
if (!checkConstraint?.expression) {
121+
return '';
122+
}
123+
124+
return ` CHECK (${cleanCheckConstraint(checkConstraint.expression)})${checkConstraint?.noInherit ? ' NO INHERIT' : ''}`;
125+
};
126+
103127
/**
104128
* @param tableName {string}
105129
* @param isParentActivated {boolean}
@@ -196,4 +220,6 @@ module.exports = {
196220
dropKeyConstraint,
197221
getConstraintsWarnings,
198222
additionalPropertiesForForeignKey,
223+
cleanCheckConstraint,
224+
createInlineCheckConstraint,
199225
};

forward_engineering/ddlProvider/ddlProvider.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const {
2626
createKeyConstraint,
2727
getConstraintsWarnings,
2828
additionalPropertiesForForeignKey,
29+
cleanCheckConstraint,
30+
createInlineCheckConstraint,
2931
} = require('./ddlHelpers/constraintsHelper');
3032
const keyHelper = require('./ddlHelpers/keyHelper');
3133
const { getFunctionsScript } = require('./ddlHelpers/functionHelper');
@@ -222,6 +224,9 @@ module.exports = (baseProvider, options, app) => {
222224
const defaultValue = !_.isUndefined(columnDefinition.default)
223225
? ' DEFAULT ' + decorateDefault(type, columnDefinition.default, isArrayType)
224226
: '';
227+
const checkConstraint = columnDefinition.checkConstraint?.expression
228+
? ' ' + this.createCheckConstraint(columnDefinition.checkConstraint).trim()
229+
: '';
225230
const generatedColumnClause =
226231
columnDefinition.dbVersion >= 12 &&
227232
columnDefinition.generatedColumn &&
@@ -241,6 +246,7 @@ module.exports = (baseProvider, options, app) => {
241246
uniqueKey,
242247
collation,
243248
defaultValue,
249+
checkConstraint,
244250
}),
245251
{
246252
isActivated: columnDefinition.isActivated,
@@ -283,7 +289,7 @@ module.exports = (baseProvider, options, app) => {
283289
createCheckConstraint(checkConstraint) {
284290
return assignTemplates(templates.checkConstraint, {
285291
name: checkConstraint.name ? `CONSTRAINT ${wrapInQuotes(checkConstraint.name)}` : '',
286-
expression: _.trim(checkConstraint.expression).replace(/^\(([\s\S]*)\)$/, '$1'),
292+
expression: cleanCheckConstraint(checkConstraint.expression),
287293
noInherit: checkConstraint.noInherit ? ' NO INHERIT' : '',
288294
});
289295
},
@@ -630,6 +636,7 @@ module.exports = (baseProvider, options, app) => {
630636
uniqueKeyOptions,
631637
nullable: columnDefinition.nullable,
632638
default: columnDefinition.default,
639+
checkConstraint: jsonSchema.checkConstraint,
633640
comment: jsonSchema.refDescription || jsonSchema.description || definitionJsonSchema.description,
634641
isActivated: columnDefinition.isActivated,
635642
scale: columnDefinition.scale,

forward_engineering/ddlProvider/templates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
generatedColumnClause: ' GENERATED ALWAYS AS (${generationExpression}) STORED',
2020

2121
columnDefinition:
22-
'${name} ${type}${collation}${generatedColumnClause}${primaryKey}${uniqueKey}${defaultValue}${notNull}',
22+
'${name} ${type}${collation}${generatedColumnClause}${primaryKey}${uniqueKey}${defaultValue}${notNull}${checkConstraint}',
2323

2424
checkConstraint: '${name} CHECK (${expression})${noInherit}',
2525

0 commit comments

Comments
 (0)