Skip to content

Commit

Permalink
add columnRequired property for liquibase generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 5, 2024
1 parent 2f540ab commit 873eaaa
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 60 deletions.
10 changes: 10 additions & 0 deletions generators/bootstrap-application/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ describe(`generator - ${generator}`, () => {
"blobContentTypeText": false,
"builtIn": true,
"columnName": "id",
"columnRequired": false,
"columnType": "\${uuidType}",
"createRandexp": Any<Function>,
"dynamic": false,
Expand Down Expand Up @@ -356,6 +357,7 @@ describe(`generator - ${generator}`, () => {
"blobContentTypeText": false,
"builtIn": true,
"columnName": "login",
"columnRequired": true,
"columnType": "varchar(50)",
"createRandexp": Any<Function>,
"entity": Any<Object>,
Expand Down Expand Up @@ -442,6 +444,7 @@ describe(`generator - ${generator}`, () => {
"blobContentTypeText": false,
"builtIn": true,
"columnName": "first_name",
"columnRequired": false,
"columnType": "varchar(50)",
"createRandexp": Any<Function>,
"entity": Any<Object>,
Expand Down Expand Up @@ -523,6 +526,7 @@ describe(`generator - ${generator}`, () => {
"blobContentTypeText": false,
"builtIn": true,
"columnName": "last_name",
"columnRequired": false,
"columnType": "varchar(50)",
"createRandexp": Any<Function>,
"entity": Any<Object>,
Expand Down Expand Up @@ -604,6 +608,7 @@ describe(`generator - ${generator}`, () => {
"blobContentTypeText": false,
"builtIn": true,
"columnName": "email",
"columnRequired": true,
"columnType": "varchar(191)",
"createRandexp": Any<Function>,
"entity": Any<Object>,
Expand Down Expand Up @@ -690,6 +695,7 @@ describe(`generator - ${generator}`, () => {
"blobContentTypeText": false,
"builtIn": true,
"columnName": "image_url",
"columnRequired": false,
"columnType": "varchar(256)",
"createRandexp": Any<Function>,
"entity": Any<Object>,
Expand Down Expand Up @@ -772,6 +778,7 @@ describe(`generator - ${generator}`, () => {
"blobContentTypeText": false,
"builtIn": true,
"columnName": "activated",
"columnRequired": false,
"columnType": "boolean",
"createRandexp": Any<Function>,
"entity": Any<Object>,
Expand Down Expand Up @@ -845,6 +852,7 @@ describe(`generator - ${generator}`, () => {
"blobContentTypeText": false,
"builtIn": true,
"columnName": "lang_key",
"columnRequired": false,
"columnType": "varchar(10)",
"createRandexp": Any<Function>,
"entity": Any<Object>,
Expand Down Expand Up @@ -1129,6 +1137,7 @@ describe(`generator - ${generator}`, () => {
"blobContentTypeImage": false,
"blobContentTypeText": false,
"columnName": "id",
"columnRequired": false,
"columnType": "\${uuidType}",
"createRandexp": Any<Function>,
"dynamic": false,
Expand Down Expand Up @@ -1467,6 +1476,7 @@ describe(`generator - ${generator}`, () => {
"blobContentTypeImage": false,
"blobContentTypeText": false,
"columnName": "id",
"columnRequired": false,
"columnType": "\${uuidType}",
"createRandexp": Any<Function>,
"dynamic": false,
Expand Down
2 changes: 1 addition & 1 deletion generators/liquibase/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 9 additions & 10 deletions generators/liquibase/support/prepare-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
11 changes: 8 additions & 3 deletions generators/liquibase/support/relationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
<%_ if (field.id) { _%>
<constraints primaryKey="true" nullable="false"/>
<%_ } else if (field.unique) { _%>
<constraints nullable="<%= field.nullable %>" unique="true" uniqueConstraintName="<%= field.uniqueConstraintName %>" />
<constraints nullable="<%= !field.columnRequired %>" unique="true" uniqueConstraintName="<%= field.uniqueConstraintName %>" />
<%_ } else { _%>
<constraints nullable="<%= field.nullable %>" />
<constraints nullable="<%= !field.columnRequired %>" />
<%_ } _%>
</column>
<%_ if (field.shouldCreateContentType) { _%>
<column name="<%= field.columnName %>_content_type" type="varchar(255)">
<constraints nullable="<%= field.nullable %>" />
<constraints nullable="<%= !field.columnRequired %>" />
</column>
<%_ } _%>
<%_ } _%>
Expand All @@ -58,7 +58,7 @@
const uniqueConstraintName = relationship.relationshipType === 'one-to-one' ? this.getUXConstraintName(entity.entityTableName, relationship.columnName + '_' + idField.columnName, entity.prodDatabaseType) : null;
_%>
<column name="<%= relationship.columnName %>_<%= idField.columnName %>" type="<%= idField.columnType %>">
<constraints nullable="<%= relationship.nullable %>"<% if (uniqueConstraintName) { %> unique="true" uniqueConstraintName="<%= uniqueConstraintName %>"<% } %> />
<constraints nullable="<%= !relationship.columnRequired %>"<% if (uniqueConstraintName) { %> unique="true" uniqueConstraintName="<%= uniqueConstraintName %>"<% } %> />
</column>
<%_ });
}
Expand Down Expand Up @@ -113,24 +113,17 @@ _%>
separator=";"
tableName="<%= entity.entityTableName %>"
usePreparedStatements="true">
<%_ for (field of fields) { _%>
<%_ for (field of fields) { _%>
<column name="<%= field.columnName %>" type="<%= field.loadColumnType %>"/>
<%_ if (field.fieldType === 'byte[]' && field.fieldTypeBlobContent !== 'text') { _%>
<%_ if (field.fieldType === 'byte[]' && field.fieldTypeBlobContent !== 'text') { _%>
<column name="<%= field.columnName %>_content_type" type="string"/>
<%_ } _%>
<%_ } _%>
<%_ 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) { _%>
<column name="<%= relationship.columnName %>_<%= field.columnName %>" type="<%= field.loadColumnType %>"/>
<%_ } _%>
<%_ } _%>
<%_ } _%>
<%_ } _%>
<%_ } _%>
<!-- jhipster-needle-liquibase-add-loadcolumn - JHipster (and/or extensions) can add load columns here -->
</loadData>
</changeSet>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if (hasFieldConstraint) { _%>
columnNames="<%= field.columnName %>"
constraintName="<%= field.uniqueConstraintName %>"/>
<%_ }
if (!field.nullable) {
if (field.columnRequired) {
_%>
<addNotNullConstraint tableName="<%= entityTableName %>"
columnName="<%= field.columnName %>"
Expand Down Expand Up @@ -69,7 +69,7 @@ if (hasFieldConstraint) { _%>
columnNames="<%= relationshipData.columnName %>_id"
constraintName="<%= uniqueConstraintName %>"/>
<%_ }
if (!relationshipData.nullable) {
if (relationshipData.columnRequired) {
_%>
<addNotNullConstraint tableName="<%= entityTableName %>"
columnName="<%= relationshipData.columnName %>_id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,9 @@ for (field of fieldsToGenerate) {
header.push(field.columnName + '_content_type');
}
}
for (relationship of relationships) {
const { relationshipType, joinColumnNames } = relationship;
if (
(relationshipType === "many-to-one"
|| (relationshipType === "one-to-one" && relationship.ownerSide === true
&& (relationship.id == null || relationship.id === false))
) && (relationship.relationshipValidate === true && relationship.relationshipRequired)
) {
header.push(joinColumnNames[0]);
}
for (relationship of relationships.filter(relationship => relationship.columnRequired)) {
const { joinColumnNames } = relationship;
header.push(joinColumnNames[0]);
}
table.push(header);
Expand All @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 873eaaa

Please sign in to comment.