diff --git a/generators/bootstrap-application/generator.spec.ts b/generators/bootstrap-application/generator.spec.ts index fa434aa846cb..62e17212dc38 100644 --- a/generators/bootstrap-application/generator.spec.ts +++ b/generators/bootstrap-application/generator.spec.ts @@ -272,6 +272,7 @@ describe(`generator - ${generator}`, () => { "blobContentTypeText": false, "builtIn": true, "columnName": "id", + "columnRequired": false, "columnType": "\${uuidType}", "createRandexp": Any, "dynamic": false, @@ -356,6 +357,7 @@ describe(`generator - ${generator}`, () => { "blobContentTypeText": false, "builtIn": true, "columnName": "login", + "columnRequired": true, "columnType": "varchar(50)", "createRandexp": Any, "entity": Any, @@ -442,6 +444,7 @@ describe(`generator - ${generator}`, () => { "blobContentTypeText": false, "builtIn": true, "columnName": "first_name", + "columnRequired": false, "columnType": "varchar(50)", "createRandexp": Any, "entity": Any, @@ -523,6 +526,7 @@ describe(`generator - ${generator}`, () => { "blobContentTypeText": false, "builtIn": true, "columnName": "last_name", + "columnRequired": false, "columnType": "varchar(50)", "createRandexp": Any, "entity": Any, @@ -604,6 +608,7 @@ describe(`generator - ${generator}`, () => { "blobContentTypeText": false, "builtIn": true, "columnName": "email", + "columnRequired": true, "columnType": "varchar(191)", "createRandexp": Any, "entity": Any, @@ -690,6 +695,7 @@ describe(`generator - ${generator}`, () => { "blobContentTypeText": false, "builtIn": true, "columnName": "image_url", + "columnRequired": false, "columnType": "varchar(256)", "createRandexp": Any, "entity": Any, @@ -772,6 +778,7 @@ describe(`generator - ${generator}`, () => { "blobContentTypeText": false, "builtIn": true, "columnName": "activated", + "columnRequired": false, "columnType": "boolean", "createRandexp": Any, "entity": Any, @@ -845,6 +852,7 @@ describe(`generator - ${generator}`, () => { "blobContentTypeText": false, "builtIn": true, "columnName": "lang_key", + "columnRequired": false, "columnType": "varchar(10)", "createRandexp": Any, "entity": Any, @@ -1129,6 +1137,7 @@ describe(`generator - ${generator}`, () => { "blobContentTypeImage": false, "blobContentTypeText": false, "columnName": "id", + "columnRequired": false, "columnType": "\${uuidType}", "createRandexp": Any, "dynamic": false, @@ -1467,6 +1476,7 @@ describe(`generator - ${generator}`, () => { "blobContentTypeImage": false, "blobContentTypeText": false, "columnName": "id", + "columnRequired": false, "columnType": "\${uuidType}", "createRandexp": Any, "dynamic": false, diff --git a/generators/liquibase/generator.ts b/generators/liquibase/generator.ts index 99822939c9e4..9605c9b48816 100644 --- a/generators/liquibase/generator.ts +++ b/generators/liquibase/generator.ts @@ -595,7 +595,7 @@ export default class LiquibaseGenerator extends BaseEntityChangesGenerator { // fakeDataCount must be limited to the size of required unique relationships. Object.defineProperty(entity, 'fakeDataCount', { get: () => { - const uniqueRelationships = entity.relationships.filter(rel => rel.unique && (rel.relationshipRequired || rel.id)); + const uniqueRelationships = entity.relationships.filter(rel => rel.unique && (rel.columnRequired || rel.id)); return _.min([entity.liquibaseFakeData.length, ...uniqueRelationships.map(rel => rel.otherEntity.fakeDataCount)]); }, configurable: true, diff --git a/generators/liquibase/support/prepare-field.js b/generators/liquibase/support/prepare-field.js index 169388665e02..6a48b2924644 100644 --- a/generators/liquibase/support/prepare-field.js +++ b/generators/liquibase/support/prepare-field.js @@ -17,9 +17,8 @@ * limitations under the License. */ -import * as _ from 'lodash-es'; - import { databaseTypes, fieldTypes } from '../../../jdl/jhipster/index.js'; +import { mutateData } from '../../base/support/index.js'; const { MYSQL, MARIADB } = databaseTypes; const { CommonDBTypes, RelationalOnlyDBTypes, BlobTypes } = fieldTypes; @@ -140,14 +139,14 @@ function parseLiquibaseLoadColumnType(entity, field) { } export default function prepareField(entity, field) { - _.defaults(field, { - columnType: parseLiquibaseColumnType(entity, field), - shouldDropDefaultValue: field.fieldType === ZONED_DATE_TIME || field.fieldType === INSTANT, - shouldCreateContentType: field.fieldType === BYTES && field.fieldTypeBlobContent !== TEXT, - nullable: !(field.fieldValidate === true && field.fieldValidateRules.includes('required')), - }); - _.defaults(field, { - loadColumnType: parseLiquibaseLoadColumnType(entity, field), + mutateData(field, { + __override__: false, + columnType: data => parseLiquibaseColumnType(entity, data), + shouldDropDefaultValue: data => data.fieldType === ZONED_DATE_TIME || data.fieldType === INSTANT, + shouldCreateContentType: data => data.fieldType === BYTES && data.fieldTypeBlobContent !== TEXT, + columnRequired: data => data.nullable === false || (data.fieldValidate === true && data.fieldValidateRules.includes('required')), + nullable: data => !data.columnRequired, + loadColumnType: data => parseLiquibaseLoadColumnType(entity, data), }); return field; } diff --git a/generators/liquibase/support/relationship.ts b/generators/liquibase/support/relationship.ts index aa09d22bff9b..0478f991870d 100644 --- a/generators/liquibase/support/relationship.ts +++ b/generators/liquibase/support/relationship.ts @@ -16,8 +16,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import * as _ from 'lodash-es'; import { getFKConstraintName } from '../../server/support/index.js'; +import { mutateData } from '../../base/support/index.js'; function relationshipBaseDataEquals(relationshipA, relationshipB) { return ( @@ -66,12 +66,17 @@ export function prepareRelationshipForLiquibase(entity, relationship) { if (relationship.shouldWriteJoinTable) { const joinTableName = relationship.joinTable.name; const prodDatabaseType = entity.prodDatabaseType; - _.defaults(relationship.joinTable, { + mutateData(relationship.joinTable, { constraintName: getFKConstraintName(joinTableName, entity.entityTableName, { prodDatabaseType }).value, otherConstraintName: getFKConstraintName(joinTableName, relationship.columnName, { prodDatabaseType }).value, }); } - relationship.columnDataType = relationship.otherEntity.columnType; + mutateData(relationship, { + __override__: false, + columnDataType: data => data.otherEntity.columnType, + columnRequired: data => data.nullable === false || data.relationshipRequired, + }); + return relationship; } diff --git a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity.xml.ejs b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity.xml.ejs index c46358b5e1e8..d09cb31163a8 100644 --- a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity.xml.ejs +++ b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity.xml.ejs @@ -40,14 +40,14 @@ <%_ if (field.id) { _%> <%_ } else if (field.unique) { _%> - + <%_ } else { _%> - + <%_ } _%> <%_ if (field.shouldCreateContentType) { _%> - + <%_ } _%> <%_ } _%> @@ -58,7 +58,7 @@ const uniqueConstraintName = relationship.relationshipType === 'one-to-one' ? this.getUXConstraintName(entity.entityTableName, relationship.columnName + '_' + idField.columnName, entity.prodDatabaseType) : null; _%> - unique="true" uniqueConstraintName="<%= uniqueConstraintName %>"<% } %> /> + unique="true" uniqueConstraintName="<%= uniqueConstraintName %>"<% } %> /> <%_ }); } @@ -113,24 +113,17 @@ _%> separator=";" tableName="<%= entity.entityTableName %>" usePreparedStatements="true"> - <%_ for (field of fields) { _%> + <%_ for (field of fields) { _%> - <%_ if (field.fieldType === 'byte[]' && field.fieldTypeBlobContent !== 'text') { _%> + <%_ if (field.fieldType === 'byte[]' && field.fieldTypeBlobContent !== 'text') { _%> - <%_ } _%> <%_ } _%> - <%_ for (relationship of relationships) { - if (relationship.relationshipValidate === true && relationship.relationshipRequired - && (relationship.relationshipType === "many-to-one" - || (relationship.relationshipType === "one-to-one" && relationship.ownerSide === true - && (relationship.id == null || relationship.id === false)) - )) { - _%> - <%_ for (field of relationship.otherEntity.primaryKey.fields) { _%> + <%_ } _%> + <%_ for (relationship of relationships.filter(relationship => !relationship.id && relationship.columnRequired && relationship.persistableRelationship && !relationship.collection)) { _%> + <%_ for (field of relationship.otherEntity.primaryKey.fields) { _%> - <%_ } _%> - <%_ } _%> - <%_ } _%> + <%_ } _%> + <%_ } _%> 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 0ccae12ee1a3..7f1c4947f7bd 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 @@ -38,7 +38,7 @@ if (hasFieldConstraint) { _%> columnNames="<%= field.columnName %>" constraintName="<%= field.uniqueConstraintName %>"/> <%_ } - if (!field.nullable) { + if (field.columnRequired) { _%> columnNames="<%= relationshipData.columnName %>_id" constraintName="<%= uniqueConstraintName %>"/> <%_ } - if (!relationshipData.nullable) { + if (relationshipData.columnRequired) { _%> relationship.columnRequired)) { + const { joinColumnNames } = relationship; + header.push(joinColumnNames[0]); } table.push(header); @@ -50,7 +43,7 @@ for (lineNb = 0; lineNb < entity.fakeDataCount; lineNb++) { let data = rowData[field.fieldName]; // manage required if (data === undefined) { - if (field.id || (field.fieldValidateRules && field.fieldValidateRules.includes('required'))) { + if (field.id || field.columnRequired) { // Remove row; line = []; break; @@ -64,22 +57,15 @@ for (lineNb = 0; lineNb < entity.fakeDataCount; lineNb++) { } } - for (relationship of relationships) { + for (relationship of relationships.filter(relationship => relationship.columnRequired)) { const relationshipType = relationship.relationshipType; - if ( - (relationshipType === "many-to-one" - || (relationshipType === "one-to-one" && relationship.ownerSide === true - && (relationship.id == null || relationship.id === false)) - ) && (relationship.relationshipValidate === true && relationship.relationshipRequired) - ) { - const otherLiquibaseFakeData = relationship.otherEntity.liquibaseFakeData; - let relationshipRow = lineNb; - if (relationship.otherEntity.fakeDataCount > 0 && relationshipRow >= relationship.otherEntity.fakeDataCount && !relationship.unique) { - relationshipRow = entity.faker.number.int({min: 1, max: relationship.otherEntity.fakeDataCount}) - 1; - } - if (relationshipRow < relationship.otherEntity.fakeDataCount) { - line.push(otherLiquibaseFakeData[relationshipRow][relationship.otherEntity.primaryKey.name]); - } + const otherLiquibaseFakeData = relationship.otherEntity.liquibaseFakeData; + let relationshipRow = lineNb; + if (relationship.otherEntity.fakeDataCount > 0 && relationshipRow >= relationship.otherEntity.fakeDataCount && !relationship.unique) { + relationshipRow = entity.faker.number.int({min: 1, max: relationship.otherEntity.fakeDataCount}) - 1; + } + if (relationshipRow < relationship.otherEntity.fakeDataCount) { + line.push(otherLiquibaseFakeData[relationshipRow][relationship.otherEntity.primaryKey.name]); } } if (line.length === header.length) {