Skip to content

Commit

Permalink
Merge pull request #4 from DrMyroslav/HCK-1630-oracle-fk-constraints
Browse files Browse the repository at this point in the history
add to fk additional properties
  • Loading branch information
DrMyroslav authored Sep 1, 2022
2 parents f361c2a + 3cc7b49 commit abf393b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions forward_engineering/configs/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ module.exports = {

createKeyConstraint: '${constraintName}${keyType}${columns}',

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

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

createForeignKey:
'ALTER TABLE ${foreignTable} ADD CONSTRAINT ${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable}(${primaryKey});\n',
'ALTER TABLE ${foreignTable} ADD CONSTRAINT ${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable} (${primaryKey})${onDelete};',

createIndex: 'CREATE${indexType} INDEX ${name} ON ${tableName}${keys}${options};\n',

Expand Down
11 changes: 10 additions & 1 deletion forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module.exports = (baseProvider, options, app) => {
foreignKeysToString,
foreignActiveKeysToString,
createKeyConstraint,
customPropertiesForForeignKey
} = require('./helpers/tableHelper')({
_,
checkAllKeysDeactivated,
Expand Down Expand Up @@ -196,6 +197,7 @@ module.exports = (baseProvider, options, app) => {
foreignTableActivated,
foreignSchemaName,
primarySchemaName,
customProperties,
},
dbData,
schemaData
Expand All @@ -211,11 +213,14 @@ module.exports = (baseProvider, options, app) => {
const foreignKeys = toArray(foreignKey);
const primaryKeys = toArray(primaryKey);

const { foreignOnDelete } = customPropertiesForForeignKey(customProperties);

const foreignKeyStatement = assignTemplates(templates.createForeignKeyConstraint, {
primaryTable: getNamePrefixedWithSchemaName(primaryTable, primarySchemaName || schemaData.schemaName),
name: name ? `CONSTRAINT ${wrapInQuotes(name)}` : '',
foreignKey: isActivated ? foreignKeysToString(foreignKeys) : foreignActiveKeysToString(foreignKeys),
primaryKey: isActivated ? foreignKeysToString(primaryKeys) : foreignActiveKeysToString(primaryKeys),
onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '',
});

return {
Expand All @@ -235,6 +240,7 @@ module.exports = (baseProvider, options, app) => {
foreignTableActivated,
foreignSchemaName,
primarySchemaName,
customProperties,
},
dbData,
schemaData
Expand All @@ -250,16 +256,19 @@ module.exports = (baseProvider, options, app) => {
const foreignKeys = toArray(foreignKey);
const primaryKeys = toArray(primaryKey);

const { foreignOnDelete } = customPropertiesForForeignKey(customProperties);

const foreignKeyStatement = assignTemplates(templates.createForeignKey, {
primaryTable: getNamePrefixedWithSchemaName(primaryTable, primarySchemaName || schemaData.schemaName),
foreignTable: getNamePrefixedWithSchemaName(foreignTable, foreignSchemaName || schemaData.schemaName),
name: name ? wrapInQuotes(name) : '',
foreignKey: isActivated ? foreignKeysToString(foreignKeys) : foreignActiveKeysToString(foreignKeys),
primaryKey: isActivated ? foreignKeysToString(primaryKeys) : foreignActiveKeysToString(primaryKeys),
onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '',
});

return {
statement: _.trim(foreignKeyStatement),
statement: _.trim(foreignKeyStatement) + '\n',
isActivated,
};
},
Expand Down
6 changes: 6 additions & 0 deletions forward_engineering/helpers/tableHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,18 @@ module.exports = ({ _, getColumnsList, checkAllKeysDeactivated, commentIfDeactiv
};
};

const customPropertiesForForeignKey = relationship => {
const foreignOnDelete = _.get(relationship, 'relationshipOnDelete', '');
return { foreignOnDelete };
};

return {
getTableOptions,
getTableType,
generateConstraintsString,
foreignKeysToString,
foreignActiveKeysToString,
createKeyConstraint,
customPropertiesForForeignKey,
};
};
15 changes: 15 additions & 0 deletions properties_pane/model_level/modelLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,20 @@ making sure that you maintain a proper JSON format.
"template": "textarea"
}
]
},
{
"lowerTab": "Relationships",
"structure": [
{
"propertyName": "On Delete",
"propertyKeyword": "relationshipOnDelete",
"propertyType": "select",
"options": [
"",
"CASCADE",
"SET NULL"
]
}
]
}
]

0 comments on commit abf393b

Please sign in to comment.