Skip to content

Commit 512d526

Browse files
HCK-12141: Add support of non check (#145)
* HCK-12141: Improve no check constraint --------- Co-authored-by: Serhii Filonenko <91055067+serhii-filonenko@users.noreply.github.com>
1 parent fda39d7 commit 512d526

4 files changed

Lines changed: 19 additions & 5 deletions

File tree

forward_engineering/configs/templates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = {
2323

2424
spatialIndex: 'CREATE SPATIAL INDEX ${name} ON ${table} (${column})${using}\n${options}${terminator}\n',
2525

26-
checkConstraint: 'CONSTRAINT [${name}] CHECK${notForReplication} (${expression})',
26+
checkConstraint: 'CONSTRAINT [${name}] ${check}${notForReplication} (${expression})',
2727

2828
createForeignKeyConstraint:
2929
'CONSTRAINT ${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable} (${primaryKey}) ${onDelete}${onUpdate}',

forward_engineering/ddlProvider.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ module.exports = (baseProvider, options, app) => {
258258
createCheckConstraint(checkConstraint) {
259259
return assignTemplates(templates.checkConstraint, {
260260
name: checkConstraint.name,
261+
check: checkConstraint.check ? 'CHECK' : 'NO CHECK',
261262
notForReplication: checkConstraint.enforceForReplication ? '' : ' NOT FOR REPLICATION',
262263
expression: _.trim(checkConstraint.expression).replace(/^\(([\s\S]*)\)$/, '$1'),
263264
terminator,
@@ -533,6 +534,7 @@ module.exports = (baseProvider, options, app) => {
533534
hydrateCheckConstraint(checkConstraint) {
534535
return {
535536
name: checkConstraint.chkConstrName,
537+
check: checkConstraint.constrCheck,
536538
expression: checkConstraint.constrExpression,
537539
existingData: checkConstraint.constrCheck,
538540
enforceForUpserts: checkConstraint.constrEnforceUpserts,

forward_engineering/helpers/alterScriptHelpers/columnHelpers/defaultValueColumnHelper.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
module.exports = (app, ddlProvider) => {
2+
const _ = app.require('lodash');
23
const { AlterScriptDto } = require('../types/AlterScriptDto');
3-
const { sanitizeConstraintName } = require('../../../helpers/general')(app);
4+
const { sanitizeConstraintName, hasType, getTableName } = require('../../../helpers/general')(app);
5+
const { decorateDefault } = require('../../columnDefinitionHelper')(app);
46

57
const getDefaultValueChangeDto = (collection, fullName) => {
68
const scripts = [];
79

810
const getDefaultConstraintName = columnName => sanitizeConstraintName(`DF_${fullName}_${columnName}`);
911

1012
Object.entries(collection?.properties ?? []).forEach(([columnName, collectionSchema]) => {
13+
const type = hasType(collectionSchema.type)
14+
? _.toUpper(collectionSchema.type)
15+
: getTableName(collectionSchema.type, collectionSchema.schemaName);
16+
1117
const newDefaultValue = collectionSchema.default;
1218
const newConstraintName = collectionSchema.defaultConstraintName;
1319
const oldDefaultValue = collection.role.properties[columnName]?.default;
@@ -24,6 +30,7 @@ module.exports = (app, ddlProvider) => {
2430
!!oldConstraintName &&
2531
!!newConstraintName &&
2632
oldConstraintName !== newConstraintName;
33+
const decoratedValue = decorateDefault(type, newDefaultValue);
2734

2835
switch (true) {
2936
case defaultValueWasRemoved: {
@@ -40,7 +47,7 @@ module.exports = (app, ddlProvider) => {
4047
{
4148
constraintName,
4249
columnName,
43-
value: newDefaultValue,
50+
value: decoratedValue,
4451
},
4552
fullName,
4653
);
@@ -58,7 +65,7 @@ module.exports = (app, ddlProvider) => {
5865
{
5966
constraintName,
6067
columnName,
61-
value: newDefaultValue,
68+
value: decoratedValue,
6269
},
6370
fullName,
6471
);
@@ -72,7 +79,7 @@ module.exports = (app, ddlProvider) => {
7279
{
7380
constraintName: newConstraintName,
7481
columnName,
75-
value: newDefaultValue,
82+
value: decoratedValue,
7683
},
7784
fullName,
7885
);

forward_engineering/helpers/columnDefinitionHelper.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ module.exports = app => {
6363

6464
const isString = type => ['CHAR', 'VARCHAR', 'NCHAR', 'NVARCHAR', 'TEXT', 'NTEXT'].includes(_.toUpper(type));
6565

66+
/**
67+
* Escape only inner single quotes.
68+
* @param {string} str
69+
* @returns {string}
70+
*/
6671
const escapeQuotes = str => _.trim(str).replace(/(\')+/g, "'$1");
6772

6873
const decorateDefault = (type, defaultValue) => {

0 commit comments

Comments
 (0)