diff --git a/generators/liquibase/__snapshots__/incremental-liquibase.spec.ts.snap b/generators/liquibase/__snapshots__/incremental-liquibase.spec.ts.snap index b4f6930ed8f7..f640f1ce2ace 100644 --- a/generators/liquibase/__snapshots__/incremental-liquibase.spec.ts.snap +++ b/generators/liquibase/__snapshots__/incremental-liquibase.spec.ts.snap @@ -388,7 +388,6 @@ exports[`generator - app - --incremental-changelog when adding a many-to-many re Added the relationship constraints for entity One. --> - - @@ -710,10 +709,10 @@ exports[`generator - app - --incremental-changelog when adding a relationship wi Added the relationship constraints for entity One. --> - - - - @@ -2486,10 +2485,10 @@ exports[`generator - app - --incremental-changelog when modifying fields and rel Added the relationship constraints for entity Another. --> - 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..ae73f3f84030 --- /dev/null +++ b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/add_relationship_constraints.ejs @@ -0,0 +1,79 @@ +<%# + 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 %>" + <%_ } _%> + <%_ } _%> + /> +<%_ } _%> \ No newline at end of file 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..22b2733050f1 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 }) -%> + <%_ } _%> <%_ } _%>