From 2081ef1cf460062ca2663cb06c50ea323098fac9 Mon Sep 17 00:00:00 2001 From: Jan Date: Mon, 21 Oct 2024 17:00:25 +0200 Subject: [PATCH] Unify logic for creation of foreign keys (#27608) and regenerated snapshots Fix #27598 --- .../incremental-liquibase.spec.ts.snap | 30 ++++--- .../liquibase/incremental-liquibase.spec.ts | 10 ++- .../add_relationship_constraints.ejs | 81 +++++++++++++++++++ .../added_entity_constraints.xml.ejs | 64 +-------------- .../updated_entity_constraints.xml.ejs | 63 +-------------- 5 files changed, 113 insertions(+), 135 deletions(-) create mode 100644 generators/liquibase/templates/src/main/resources/config/liquibase/changelog/add_relationship_constraints.ejs diff --git a/generators/liquibase/__snapshots__/incremental-liquibase.spec.ts.snap b/generators/liquibase/__snapshots__/incremental-liquibase.spec.ts.snap index b4f6930ed8f7..5e336e7d6676 100644 --- a/generators/liquibase/__snapshots__/incremental-liquibase.spec.ts.snap +++ b/generators/liquibase/__snapshots__/incremental-liquibase.spec.ts.snap @@ -557,10 +557,11 @@ exports[`generator - app - --incremental-changelog when adding a relationship sh Added the relationship constraints for entity One. --> - @@ -710,10 +711,11 @@ exports[`generator - app - --incremental-changelog when adding a relationship wi Added the relationship constraints for entity One. --> - - - - @@ -2486,10 +2491,11 @@ exports[`generator - app - --incremental-changelog when modifying fields and rel Added the relationship constraints for entity Another. --> - diff --git a/generators/liquibase/incremental-liquibase.spec.ts b/generators/liquibase/incremental-liquibase.spec.ts index 7b3cd00aed2a..c2c275268602 100644 --- a/generators/liquibase/incremental-liquibase.spec.ts +++ b/generators/liquibase/incremental-liquibase.spec.ts @@ -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(); diff --git a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/add_relationship_constraints.ejs b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/add_relationship_constraints.ejs new file mode 100644 index 000000000000..eb5c2e60d568 --- /dev/null +++ b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/add_relationship_constraints.ejs @@ -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(','); + } _%> + + + onDelete="<%= onDelete %>" + <%_ } _%> + <%_ if (onUpdate) { _%> + onUpdate="<%= onUpdate %>" + <%_ } _%> + /> +<%_ } else if (relationship.shouldWriteJoinTable) { _%> + + + onDelete="<%= onDelete %>" + <%_ } _%> + <%_ if (onUpdate) { _%> + onUpdate="<%= onUpdate %>" + <%_ } _%> + /> + + + <%_ if (relationship.otherRelationship.onDelete) { _%> + onDelete="<%= relationship.otherRelationship.onDelete %>" + <%_ } _%> + <%_ if (relationship.otherRelationship.onUpdate) { _%> + onUpdate="<%= relationship.otherRelationship.onUpdate %>" + <%_ } _%> + <%_ } _%> + /> +<%_ } _%> diff --git a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity_constraints.xml.ejs b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity_constraints.xml.ejs index d6840075d11b..82cce4f2419c 100644 --- a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity_constraints.xml.ejs +++ b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity_constraints.xml.ejs @@ -21,68 +21,8 @@ Added the constraints for entity <%= entity.entityClass %>. --> -<% 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(','); - } %> - - onDelete="<%= onDelete %>" - <%_ } _%> - <%_ if (onUpdate) { _%> - onUpdate="<%= onUpdate %>" - <%_ } _%> - /> - <%_ } else if (relationship.shouldWriteJoinTable) { _%> - - - onDelete="<%= onDelete %>" - <%_ } _%> - <%_ if (onUpdate) { _%> - onUpdate="<%= onUpdate %>" - <%_ } _%> - /> - - - <%_ 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 }) -%> <%_ } _%> diff --git a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/updated_entity_constraints.xml.ejs b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/updated_entity_constraints.xml.ejs index b522a24a9383..3ed246724759 100644 --- a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/updated_entity_constraints.xml.ejs +++ b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/updated_entity_constraints.xml.ejs @@ -105,66 +105,9 @@ if (hasFieldConstraint) { _%> Added the relationship constraints for entity <%= entityClass %>. --> - <%_ 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'; - } - _%> - - 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); - _%> - - - onDelete="<%= onDelete %>" - <%_ } _%> - <%_ if (onUpdate) { _%> - onUpdate="<%= onUpdate %>" - <%_ } _%> - /> - - - <%_ 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 }) -%> + <%_ } _%> <%_ } _%>