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

allow defining onDelete and onUpdate in relationships #21070

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 28 additions & 5 deletions generators/base-application/support/prepare-relationship.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
import _ from 'lodash';
import pluralize from 'pluralize';

import { databaseTypes, entityOptions, reservedKeywords, validations } from '../../../jdl/jhipster/index.mjs';
import {
databaseTypes,
entityOptions,
reservedKeywords,
validations,
checkAndReturnRelationshipOnValue,
} from '../../../jdl/jhipster/index.mjs';
import { stringifyApplicationData } from './debug.mjs';

const { isReservedTableName } = reservedKeywords;
Expand All @@ -41,6 +47,11 @@ function _derivedProperties(relationship) {
});
}

function _defineOnUpdateAndOnDelete(relationship, generator) {
relationship.onDelete = checkAndReturnRelationshipOnValue(relationship.options?.onDelete, generator);
relationship.onUpdate = checkAndReturnRelationshipOnValue(relationship.options?.onUpdate, generator);
}

export default function prepareRelationship(entityWithConfig, relationship, generator, ignoreMissingRequiredRelationship) {
const entityName = entityWithConfig.name;
const otherEntityName = relationship.otherEntityName;
Expand Down Expand Up @@ -78,11 +89,19 @@ export default function prepareRelationship(entityWithConfig, relationship, gene
return otherSideRelationship.relationshipName === relationship.otherEntityRelationshipName;
});
if (!otherRelationship) {
// TODO throw error at v8.
generator.logger.warn(
`Error at '${entityName}' definitions: 'otherEntityRelationshipName' is set with value '${relationship.otherEntityRelationshipName}' at relationship '${relationship.relationshipName}' but no back-reference was found at '${otherEntityName}'`
);
if (!relationship.otherEntity.builtIn) {
// TODO throw error at v8.
generator.logger.warn(
`Error at '${entityName}' definitions: 'otherEntityRelationshipName' is set with value '${relationship.otherEntityRelationshipName}' at relationship '${relationship.relationshipName}' but no back-reference was found at '${otherEntityName}'`
);
} else {
generator.logger.info(
`Ignoring '${entityName}' definitions as it is using a built-in Entity '${otherEntityName}': 'otherEntityRelationshipName' is set with value '${relationship.otherEntityRelationshipName}' at relationship '${relationship.relationshipName}' but no back-reference was found`
);
}
} else if (
// renaming a relationship could cause trouble here - old relationship needs to be removed
!ignoreMissingRequiredRelationship &&
OmarHawk marked this conversation as resolved.
Show resolved Hide resolved
otherRelationship &&
otherRelationship.otherEntityRelationshipName &&
otherRelationship.otherEntityRelationshipName !== relationship.relationshipName
Expand Down Expand Up @@ -273,7 +292,11 @@ export default function prepareRelationship(entityWithConfig, relationship, gene
}

relationship.reference = relationshipToReference(entityWithConfig, relationship);

_defineOnUpdateAndOnDelete(relationship, generator);

_derivedProperties(relationship);

return relationship;
}

Expand Down
1,422 changes: 1,359 additions & 63 deletions generators/liquibase-changelogs/__snapshots__/incremental-liquibase.spec.mts.snap

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions generators/liquibase-changelogs/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export default class DatabaseChangelogLiquibase extends BaseApplication {
})
.map(relationship => prepareRelationship(entity, relationship, this, true))
.map(relationship => this._prepareRelationship(entity, relationship));
entityChanges.relationshipsToRecreateForeignKeysOnly = databaseChangelog.relationshipsToRecreateForeignKeysOnly;
}
postPrepareEntity({ application, entity });
},
Expand Down Expand Up @@ -339,6 +340,7 @@ export default class DatabaseChangelogLiquibase extends BaseApplication {
hasFieldConstraint,
hasRelationshipConstraint,
shouldWriteAnyRelationship,
relationshipsToRecreateForeignKeysOnly,
} = entityChanges;

const context = {
Expand All @@ -354,6 +356,7 @@ export default class DatabaseChangelogLiquibase extends BaseApplication {
relationships: addedRelationships,
hasRelationshipConstraint,
shouldWriteAnyRelationship,
relationshipsToRecreateForeignKeysOnly,
};

const promises = [];
Expand Down