Skip to content

Commit

Permalink
Unify logic for creation of foreign keys (#27608)
Browse files Browse the repository at this point in the history
and regenerated snapshots

Fix #27598
  • Loading branch information
OmarHawk authored Oct 21, 2024
1 parent fcad410 commit 2081ef1
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,11 @@ exports[`generator - app - --incremental-changelog when adding a relationship sh
Added the relationship constraints for entity One.
-->
<changeSet id="20200102000100-relationship-foreign-keys" author="jhipster">
<addForeignKeyConstraint baseColumnNames="another_id"
<addForeignKeyConstraint baseColumnNames="another_another_id"
baseTableName="one"
constraintName="fk_one__another_id"
referencedColumnNames="id"
referencedColumnNames="another_id"
referencedTableName="another"
/>
</changeSet>
Expand Down Expand Up @@ -710,10 +711,11 @@ exports[`generator - app - --incremental-changelog when adding a relationship wi
Added the relationship constraints for entity One.
-->
<changeSet id="20200102000100-relationship-foreign-keys" author="jhipster">
<addForeignKeyConstraint baseColumnNames="another_id"
<addForeignKeyConstraint baseColumnNames="another_another_id"
baseTableName="one"
constraintName="fk_one__another_id"
referencedColumnNames="id"
referencedColumnNames="another_id"
referencedTableName="another"
onDelete="CASCADE"
onUpdate="SET NULL"
Expand Down Expand Up @@ -1752,10 +1754,11 @@ exports[`generator - app - --incremental-changelog when modifying a relationship
Added the relationship constraints for entity One.
-->
<changeSet id="20200103000100-relationship-foreign-keys" author="jhipster">
<addForeignKeyConstraint baseColumnNames="another_id"
<addForeignKeyConstraint baseColumnNames="another_another_id"
baseTableName="one"
constraintName="fk_one__another_id"
referencedColumnNames="id"
referencedColumnNames="another_id"
referencedTableName="another"
onDelete="SET NULL"
onUpdate="CASCADE"
Expand Down Expand Up @@ -1854,10 +1857,11 @@ exports[`generator - app - --incremental-changelog when modifying an existing re
Added the relationship constraints for entity One.
-->
<changeSet id="20200103000100-relationship-foreign-keys" author="jhipster">
<addForeignKeyConstraint baseColumnNames="another_ent_id"
<addForeignKeyConstraint baseColumnNames="another_ent_another_id"
baseTableName="one"
constraintName="fk_one__another_ent_id"
referencedColumnNames="id"
referencedColumnNames="another_id"
referencedTableName="another"
onDelete="SET NULL"
onUpdate="CASCADE"
Expand Down Expand Up @@ -2155,10 +2159,11 @@ exports[`generator - app - --incremental-changelog when modifying default values
Added the relationship constraints for entity Two.
-->
<changeSet id="20200102000400-relationship-foreign-keys" author="jhipster">
<addForeignKeyConstraint baseColumnNames="one_id"
<addForeignKeyConstraint baseColumnNames="one_uuid"
baseTableName="two"
constraintName="fk_two__one_id"
referencedColumnNames="id"
referencedColumnNames="uuid"
referencedTableName="one"
/>
</changeSet>
Expand Down Expand Up @@ -2486,10 +2491,11 @@ exports[`generator - app - --incremental-changelog when modifying fields and rel
Added the relationship constraints for entity Another.
-->
<changeSet id="20200102000400-relationship-foreign-keys" author="jhipster">
<addForeignKeyConstraint baseColumnNames="one_id"
<addForeignKeyConstraint baseColumnNames="one_one_id"
baseTableName="another"
constraintName="fk_another__one_id"
referencedColumnNames="id"
referencedColumnNames="one_id"
referencedTableName="one"
/>
</changeSet>
Expand Down
10 changes: 9 additions & 1 deletion generators/liquibase/incremental-liquibase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,16 @@ entity Customer {
);
runResult.assertNoFileContent(`${SERVER_MAIN_RES_DIR}config/liquibase/changelog/20200102000100_updated_entity_One.xml`, 'dropColumn');
});
it('should create the entity constraint update changelog', () => {
it('should create the entity constraint update changelog with fitting column names', () => {
runResult.assertFile([`${SERVER_MAIN_RES_DIR}config/liquibase/changelog/20200102000100_updated_entity_constraints_One.xml`]);
runResult.assertFileContent(
`${SERVER_MAIN_RES_DIR}config/liquibase/changelog/20200102000100_updated_entity_constraints_One.xml`,
'baseColumnNames="another_another_id"',
);
runResult.assertFileContent(
`${SERVER_MAIN_RES_DIR}config/liquibase/changelog/20200102000100_updated_entity_constraints_One.xml`,
'referencedColumnNames="another_id"',
);
});
it('should match snapshot', () => {
expect(runResult.getSnapshot('**/src/main/resources/config/liquibase/**')).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<%#
Copyright 2013-2024 the original author or authors from the JHipster project.
This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
<%_
const relationshipType = relationship.relationshipType,
relationshipName = relationship.relationshipName,
ownerSide = relationship.ownerSide,
otherEntityTableName = relationship.otherEntityTableName,
onDelete = relationship.onDelete,
onUpdate = relationship.onUpdate;
if (relationshipType === 'many-to-one' || (relationshipType === 'one-to-one' && ownerSide)) {
const constraintName = this.getFKConstraintName(entity.entityTableName, relationshipName, prodDatabaseType);
let baseColumnNames;
let referencedColumnNames;
if (relationshipType === 'one-to-one' && ownerSide && relationship.id === true) {
baseColumnNames = relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(',');
referencedColumnNames = relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(',');
} else if (relationship.otherEntity) {
baseColumnNames = relationship.otherEntity.primaryKey.fields.map(field => relationship.columnName + '_' + field.columnName).join(',');
referencedColumnNames = relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(',');
} _%>
<addForeignKeyConstraint baseColumnNames="<%= baseColumnNames %>"
baseTableName="<%= entity.entityTableName %>"
constraintName="<%= constraintName %>"
referencedColumnNames="<%= referencedColumnNames %>"
referencedTableName="<%= otherEntityTableName %>"
<%_ if (onDelete) { _%>
onDelete="<%= onDelete %>"
<%_ } _%>
<%_ if (onUpdate) { _%>
onUpdate="<%= onUpdate %>"
<%_ } _%>
/>
<%_ } else if (relationship.shouldWriteJoinTable) { _%>
<addForeignKeyConstraint baseColumnNames="<%= entity.primaryKey.fields.map(field => entity.entityTableName + '_' + field.columnName).join(', ') %>"
baseTableName="<%= relationship.joinTable.name %>"
constraintName="<%= relationship.joinTable.constraintName %>"
referencedColumnNames="<%= entity.primaryKey.fields.map(field => field.columnName).join(', ') %>"
referencedTableName="<%= entity.entityTableName %>"
<%_ if (onDelete) { _%>
onDelete="<%= onDelete %>"
<%_ } _%>
<%_ if (onUpdate) { _%>
onUpdate="<%= onUpdate %>"
<%_ } _%>
/>
<addForeignKeyConstraint baseColumnNames="<%= relationship.otherEntity.primaryKey.fields.map(field => relationship.columnName + '_' + field.columnName).join(', ') %>"
baseTableName="<%= relationship.joinTable.name %>"
constraintName="<%= relationship.joinTable.otherConstraintName %>"
referencedColumnNames="<%= relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(', ') %>"
referencedTableName="<%= relationship.otherEntity.entityTableName %>"
<%_ if (relationship.otherRelationship) {
// User object is not supporting this right now
_%>
<%_ if (relationship.otherRelationship.onDelete) { _%>
onDelete="<%= relationship.otherRelationship.onDelete %>"
<%_ } _%>
<%_ if (relationship.otherRelationship.onUpdate) { _%>
onUpdate="<%= relationship.otherRelationship.onUpdate %>"
<%_ } _%>
<%_ } _%>
/>
<%_ } _%>
Original file line number Diff line number Diff line change
Expand Up @@ -21,68 +21,8 @@
Added the constraints for entity <%= entity.entityClass %>.
-->
<changeSet id="<%= changelogDate %>-2" author="jhipster">
<% for (relationship of relationships) {
const relationshipType = relationship.relationshipType,
relationshipName = relationship.relationshipName,
ownerSide = relationship.ownerSide,
otherEntityTableName = relationship.otherEntityTableName,
onDelete = relationship.onDelete,
onUpdate = relationship.onUpdate;
if (relationshipType === 'many-to-one' || (relationshipType === 'one-to-one' && ownerSide)) {
const constraintName = this.getFKConstraintName(entity.entityTableName, relationshipName, prodDatabaseType);
let baseColumnNames;
let referencedColumnNames;
if (relationshipType === 'one-to-one' && ownerSide && relationship.id === true) {
baseColumnNames = relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(',');
referencedColumnNames = relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(',');
} else if (relationship.otherEntity) {
baseColumnNames = relationship.otherEntity.primaryKey.fields.map(field => relationship.columnName + '_' + field.columnName).join(',');
referencedColumnNames = relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(',');
} %>
<addForeignKeyConstraint baseColumnNames="<%= baseColumnNames %>"
baseTableName="<%= entity.entityTableName %>"
constraintName="<%= constraintName %>"
referencedColumnNames="<%= referencedColumnNames %>"
referencedTableName="<%= otherEntityTableName %>"
<%_ if (onDelete) { _%>
onDelete="<%= onDelete %>"
<%_ } _%>
<%_ if (onUpdate) { _%>
onUpdate="<%= onUpdate %>"
<%_ } _%>
/>
<%_ } else if (relationship.shouldWriteJoinTable) { _%>
<addForeignKeyConstraint baseColumnNames="<%= entity.primaryKey.fields.map(field => entity.entityTableName + '_' + field.columnName).join(', ') %>"
baseTableName="<%= relationship.joinTable.name %>"
constraintName="<%= relationship.joinTable.constraintName %>"
referencedColumnNames="<%= entity.primaryKey.fields.map(field => field.columnName).join(', ') %>"
referencedTableName="<%= entity.entityTableName %>"
<%_ if (onDelete) { _%>
onDelete="<%= onDelete %>"
<%_ } _%>
<%_ if (onUpdate) { _%>
onUpdate="<%= onUpdate %>"
<%_ } _%>
/>
<addForeignKeyConstraint baseColumnNames="<%= relationship.otherEntity.primaryKey.fields.map(field => relationship.columnName + '_' + field.columnName).join(', ') %>"
baseTableName="<%= relationship.joinTable.name %>"
constraintName="<%= relationship.joinTable.otherConstraintName %>"
referencedColumnNames="<%= relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(', ') %>"
referencedTableName="<%= relationship.otherEntity.entityTableName %>"
<%_ if (relationship.otherRelationship) {
// User object is not supporting this right now
_%>
<%_ if (relationship.otherRelationship.onDelete) { _%>
onDelete="<%= relationship.otherRelationship.onDelete %>"
<%_ } _%>
<%_ if (relationship.otherRelationship.onUpdate) { _%>
onUpdate="<%= relationship.otherRelationship.onUpdate %>"
<%_ } _%>
<%_ } _%>
/>
<%_ } _%>
<%_ for (relationship of relationships) { _%>
<%- include('./add_relationship_constraints', { entity, relationship, prodDatabaseType }) -%>
<%_ } _%>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -105,66 +105,9 @@ if (hasFieldConstraint) { _%>
Added the relationship constraints for entity <%= entityClass %>.
-->
<changeSet id="<%= changelogDate %>-relationship-foreign-keys" author="jhipster">
<%_ for (idx in addedRelationships) {
const relationship = addedRelationships[idx];
onDelete = relationship.onDelete,
onUpdate = relationship.onUpdate;
if (relationship.shouldWriteRelationship) {
const constraintName = this.getFKConstraintName(entityTableName, relationship.columnName, prodDatabaseType);
let baseColumnName = relationship.columnName + '_id';
if (relationship.relationshipType === 'one-to-one' && relationship.id === true) {
baseColumnName = 'id';
}
_%>
<addForeignKeyConstraint baseColumnNames="<%= baseColumnName %>"
baseTableName="<%= entityTableName %>"
constraintName="<%= constraintName %>"
referencedColumnNames="id"
referencedTableName="<%= relationship.otherEntityTableName %>"
<%_ if (onDelete) { _%>
onDelete="<%= onDelete %>"
<%_ } _%>
<%_ if (onUpdate) { _%>
onUpdate="<%= onUpdate %>"
<%_ } _%>
/>
<%_ } else if (relationship.shouldWriteJoinTable) {
const constraintName = this.getFKConstraintName(relationship.joinTable.name, entityTableName, prodDatabaseType);
const otherEntityConstraintName = this.getFKConstraintName(relationship.joinTable.name, relationship.columnName, prodDatabaseType);
_%>
<addForeignKeyConstraint baseColumnNames="<%= entity.primaryKey.fields.map(field => entity.entityTableName + '_' + field.columnName).join(', ') %>"
baseTableName="<%= relationship.joinTable.name %>"
constraintName="<%= constraintName %>"
referencedColumnNames="<%= entity.primaryKey.fields.map(field => field.columnName).join(', ') %>"
referencedTableName="<%= entityTableName %>"
<%_ if (onDelete) { _%>
onDelete="<%= onDelete %>"
<%_ } _%>
<%_ if (onUpdate) { _%>
onUpdate="<%= onUpdate %>"
<%_ } _%>
/>
<addForeignKeyConstraint baseColumnNames="<%= relationship.otherEntity.primaryKey.fields.map(field => relationship.columnName + '_' + field.columnName).join(', ') %>"
baseTableName="<%= relationship.joinTable.name %>"
constraintName="<%= otherEntityConstraintName %>"
referencedColumnNames="<%= relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(', ') %>"
referencedTableName="<%= relationship.otherEntityTableName %>"
<%_ if (relationship.otherRelationship) {
// User object is not supporting this right now
_%>
<%_ if (relationship.otherRelationship.onDelete) { _%>
onDelete="<%= relationship.otherRelationship.onDelete %>"
<%_ } _%>
<%_ if (relationship.otherRelationship.onUpdate) { _%>
onUpdate="<%= relationship.otherRelationship.onUpdate %>"
<%_ } _%>
<%_ } _%>
/>
<%_ }
} _%>
<%_ for (relationship of addedRelationships) { _%>
<%- include('./add_relationship_constraints', { entity, relationship, prodDatabaseType }) -%>
<%_ } _%>
</changeSet>
<%_ } _%>
</databaseChangeLog>

0 comments on commit 2081ef1

Please sign in to comment.