Skip to content

Commit

Permalink
Merge pull request #12979 from mshima/skip_ci_relationship
Browse files Browse the repository at this point in the history
Change relationship table name.
  • Loading branch information
pascalgrimaud committed Nov 8, 2020
2 parents 607cee3 + e917d84 commit 6de6754
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 70 deletions.
5 changes: 5 additions & 0 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ module.exports = class JHipsterAppGenerator extends BaseBlueprintGenerator {
type: Boolean,
});

this.option('legacy-db-names', {
desc: 'Generate database names with jhipster 6 compatibility.',
type: Boolean,
});

// Just constructing help, stop here
if (this.options.help) {
return;
Expand Down
29 changes: 0 additions & 29 deletions generators/generator-base-private.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,35 +691,6 @@ module.exports = class JHipsterBasePrivateGenerator extends Generator {
return optionValue;
}

/**
* get hibernate SnakeCase in JHipster preferred style.
*
* @param {string} value - table column name or table name string
* @see org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy
* @returns hibernate SnakeCase in JHipster preferred style
*/
hibernateSnakeCase(value) {
let res = '';
if (value) {
value = value.replace('.', '_');
res = value[0];
for (let i = 1, len = value.length - 1; i < len; i++) {
if (
value[i - 1] !== value[i - 1].toUpperCase() &&
value[i] !== value[i].toLowerCase() &&
value[i + 1] !== value[i + 1].toUpperCase()
) {
res += `_${value[i]}`;
} else {
res += value[i];
}
}
res += value[value.length - 1];
res = res.toLowerCase();
}
return res;
}

/**
* Function to issue a https get request, and process the result
*
Expand Down
50 changes: 27 additions & 23 deletions generators/generator-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const PrivateBase = require('./generator-base-private');
const NeedleApi = require('./needle-api');
const { defaultConfig } = require('./generator-defaults');
const { formatDateForChangelog } = require('../utils/liquibase');
const { calculateDbNameWithLimit, hibernateSnakeCase } = require('../utils/db');
const defaultApplicationOptions = require('../jdl/jhipster/default-application-options');
const databaseTypes = require('../jdl/jhipster/database-types');

Expand Down Expand Up @@ -1620,7 +1621,7 @@ module.exports = class JHipsterBaseGenerator extends PrivateBase {
* @param {string} value - table name string
*/
getTableName(value) {
return this.hibernateSnakeCase(value);
return hibernateSnakeCase(value);
}

/**
Expand All @@ -1629,7 +1630,7 @@ module.exports = class JHipsterBaseGenerator extends PrivateBase {
* @param {string} value - table column name string
*/
getColumnName(value) {
return this.hibernateSnakeCase(value);
return hibernateSnakeCase(value);
}

/**
Expand All @@ -1640,7 +1641,10 @@ module.exports = class JHipsterBaseGenerator extends PrivateBase {
* @param {string} prodDatabaseType - database type
*/
getJoinTableName(entityName, relationshipName, prodDatabaseType) {
const joinTableName = `${this.getTableName(entityName)}_${this.getTableName(relationshipName)}`;
const legacyDbNames = this.jhipsterConfig && this.jhipsterConfig.legacyDbNames;
const separator = legacyDbNames ? '_' : '__';
const prefix = legacyDbNames ? '' : 'rel_';
const joinTableName = `${prefix}${this.getTableName(entityName)}${separator}${this.getTableName(relationshipName)}`;
let limit = 0;
if (prodDatabaseType === 'oracle' && joinTableName.length > 30 && !this.skipCheckLengthOfIdentifier) {
this.warning(
Expand All @@ -1667,13 +1671,9 @@ module.exports = class JHipsterBaseGenerator extends PrivateBase {

limit = 64;
}
if (limit > 0) {
const halfLimit = Math.floor(limit / 2);
const entityTable = this.getTableName(entityName).substring(0, halfLimit);
const relationTable = this.getTableName(relationshipName).substring(0, limit - entityTable.length - 1);
return `${entityTable}_${relationTable}`;
}
return joinTableName;
return limit === 0
? joinTableName
: calculateDbNameWithLimit(entityName, relationshipName, limit, { prefix, separator, appendHash: !legacyDbNames });
}

/**
Expand All @@ -1683,14 +1683,16 @@ module.exports = class JHipsterBaseGenerator extends PrivateBase {
* @param {string} columnOrRelationName - name of the column or related entity
* @param {string} prodDatabaseType - database type
* @param {boolean} noSnakeCase - do not convert names to snakecase
* @param {string} constraintNamePrefix - constraintName prefix for the constraintName
* @param {string} prefix - constraintName prefix for the constraintName
*/
getConstraintNameWithLimit(entityName, columnOrRelationName, prodDatabaseType, noSnakeCase, constraintNamePrefix = '') {
getConstraintNameWithLimit(entityName, columnOrRelationName, prodDatabaseType, noSnakeCase, prefix = '') {
let constraintName;
const legacyDbNames = this.jhipsterConfig && this.jhipsterConfig.legacyDbNames;
const separator = legacyDbNames ? '_' : '__';
if (noSnakeCase) {
constraintName = `${constraintNamePrefix}${entityName}_${columnOrRelationName}`;
constraintName = `${prefix}${entityName}${separator}${columnOrRelationName}`;
} else {
constraintName = `${constraintNamePrefix}${this.getTableName(entityName)}_${this.getTableName(columnOrRelationName)}`;
constraintName = `${prefix}${this.getTableName(entityName)}${separator}${this.getTableName(columnOrRelationName)}`;
}
let limit = 0;
if (prodDatabaseType === databaseTypes.ORACLE && constraintName.length >= 27 && !this.skipCheckLengthOfIdentifier) {
Expand Down Expand Up @@ -1718,15 +1720,14 @@ module.exports = class JHipsterBaseGenerator extends PrivateBase {

limit = 62;
}
if (limit > 0) {
const halfLimit = Math.floor(limit / 2);
const entityTable = noSnakeCase ? entityName.substring(0, halfLimit) : this.getTableName(entityName).substring(0, halfLimit);
const otherTable = noSnakeCase
? columnOrRelationName.substring(0, limit - entityTable.length - 2)
: this.getTableName(columnOrRelationName).substring(0, limit - entityTable.length - 2);
return `${entityTable}_${otherTable}`;
}
return constraintName;
return limit === 0
? constraintName
: calculateDbNameWithLimit(entityName, columnOrRelationName, limit - 1, {
separator,
noSnakeCase,
prefix,
appendHash: !legacyDbNames,
});
}

/**
Expand Down Expand Up @@ -2412,6 +2413,9 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`;
if (options.testFrameworks) {
this.jhipsterConfig.testFrameworks = options.testFrameworks;
}
if (options.legacyDbNames !== undefined) {
this.jhipsterConfig.legacyDbNames = options.legacyDbNames;
}

if (options.creationTimestamp) {
const creationTimestamp = this.parseCreationTimestamp(options.creationTimestamp);
Expand Down
6 changes: 5 additions & 1 deletion test-integration/samples/jdl-default/app.jdl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ application {
entities *
}

entity Bank {
bankNumber Integer
}
entity BankAccount {
name String required
bankNumber Integer
Expand Down Expand Up @@ -166,7 +169,8 @@ relationship ManyToMany {
JobHistory{department} to Department{history},
JobHistory{job} to Job{history},
JobHistory{emp(firstName)} to Employee{history},
Job{chore(title)} to Task{linkedJob(title)}
Job{chore(title)} to Task{linkedJob(title)},
Bank{account} to BankAccount{bank}
}

dto BankAccount, Employee, Department, Location, Country, Region, SilverBadge, GoldenBadge, Identifier with mapstruct
Expand Down
45 changes: 28 additions & 17 deletions test/generator-base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,43 @@ describe('Generator Base', () => {
describe('when called with a value', () => {
it('returns a join table name', () => {
expect(BaseGenerator.getJoinTableName('entityName', 'relationshipName', 'postgresql')).to.equal(
'entity_name_relationship_name'
'rel_entity_name__relationship_name'
);
});
});
describe('when called with a long name', () => {
it('returns a proper join table name', () => {
expect(BaseGenerator.getJoinTableName('entityNameLonger', 'relationshipName', 'oracle')).to.have.length(30);
expect(BaseGenerator.getJoinTableName('entityNameLonger', 'relationshipName', 'oracle')).to.equal(
'entity_name_lon_relationship_n'
'rel_entity_name_l__relation_be'
);
expect(BaseGenerator.getJoinTableName('entityNameLonger', 'relationshipName', 'oracle')).to.have.length(30);
});
});
describe('when legacyRelationshipTableName is set', () => {
it('returns a proper join table name', () => {
function TestClass() {}
TestClass.prototype = Object.create(Base.prototype);
TestClass.prototype.jhipsterConfig = { legacyRelationshipTableName: true };
expect(TestClass.prototype.getJoinTableName('entityNameLonger', 'relationshipName', 'oracle')).to.equal(
'rel_entity_name_l__relation_be'
);
expect(TestClass.prototype.getJoinTableName('entityNameLonger', 'relationshipName', 'oracle')).to.have.length(30);
});
});
});
describe('getFKConstraintName', () => {
describe('when called with a value', () => {
it('returns a constraint name', () => {
expect(BaseGenerator.getFKConstraintName('entityName', 'relationshipName', 'postgresql')).to.equal(
'fk_entity_name_relationship_name_id'
'fk_entity_name__relationship_name_id'
);
});
});
describe('when called with a long name and oracle', () => {
it('returns a proper constraint name', () => {
expect(BaseGenerator.getFKConstraintName('entityNameLongerName', 'relationshipLongerName', 'oracle')).to.have.length(30);
expect(BaseGenerator.getFKConstraintName('entityNameLongerName', 'relationshipLongerName', 'oracle')).to.equal(
'entity_name_lo_relationship_id'
'fk_entity_name__relation_03_id'
);
});
});
Expand All @@ -120,7 +131,7 @@ describe('Generator Base', () => {
'relationshipLongerNameWithPaginationAndDTO',
'postgresql'
)
).to.equal('entity_longer_name_with_pagina_relationship_longer_name_with_id');
).to.equal('fk_entity_longer_name_with_pagi__relationship_longer_name_b6_id');
});
});
describe('when called with a long name that is near limit and postgresql', () => {
Expand All @@ -129,13 +140,13 @@ describe('Generator Base', () => {
BaseGenerator.getFKConstraintName('testCustomTableName', 'userManyToManyUserManyToMany', 'postgresql').length
).to.be.lessThan(64);
expect(BaseGenerator.getFKConstraintName('testCustomTableName', 'userManyToManyUserManyToMany', 'postgresql')).to.equal(
'test_custom_table_name_user_many_to_many_user_many_to_many_id'
'fk_test_custom_table_name__user_many_to_many_user_many_to_8c_id'
);
expect(
BaseGenerator.getFKConstraintName('testCustomTableName', 'userManyToManyUserManyToManies', 'postgresql').length
).to.be.lessThan(64);
expect(BaseGenerator.getFKConstraintName('testCustomTableName', 'userManyToManyUserManyToManies', 'postgresql')).to.equal(
'test_custom_table_name_user_many_to_many_user_many_to_manies_id'
'fk_test_custom_table_name__user_many_to_many_user_many_to_72_id'
);
});
});
Expand All @@ -145,7 +156,7 @@ describe('Generator Base', () => {
BaseGenerator.getFKConstraintName('testCustomTableNames', 'userManyToManyUserManyToManies', 'postgresql')
).to.have.length(63);
expect(BaseGenerator.getFKConstraintName('testCustomTableNames', 'userManyToManyUserManyToManies', 'postgresql')).to.equal(
'test_custom_table_names_user_many_to_many_user_many_to_manie_id'
'fk_test_custom_table_names__user_many_to_many_user_many_t_50_id'
);
});
});
Expand All @@ -155,29 +166,29 @@ describe('Generator Base', () => {
30
);
expect(BaseGenerator.getFKConstraintName('entityNameLongerName', 'relationshipLongerName', 'oracle', true)).to.equal(
'entityNameLong_relationship_id'
'fk_entityNameL__relation_03_id'
);
});
});
});
describe('getUXConstraintName', () => {
describe('when called with a value', () => {
it('returns a constraint name', () => {
expect(BaseGenerator.getUXConstraintName('entityName', 'columnName', 'postgresql')).to.equal('ux_entity_name_column_name');
expect(BaseGenerator.getUXConstraintName('entityName', 'columnName', 'postgresql')).to.equal('ux_entity_name__column_name');
});
});
describe('when called with a value and no snake case', () => {
it('returns a constraint name', () => {
expect(BaseGenerator.getUXConstraintName('entityName', 'columnName', 'postgresql', true)).to.equal(
'ux_entityName_columnName'
'ux_entityName__columnName'
);
});
});
describe('when called with a long name and oracle', () => {
it('returns a proper constraint name', () => {
expect(BaseGenerator.getUXConstraintName('entityNameLongerName', 'columnLongerName', 'oracle')).to.have.length(30);
expect(BaseGenerator.getUXConstraintName('entityNameLongerName', 'columnLongerName', 'oracle')).to.equal(
'ux_entity_name_lo_column_longe'
'ux_entity_name__column_long_29'
);
});
});
Expand All @@ -196,7 +207,7 @@ describe('Generator Base', () => {
'columnLongerNameWithPaginationAndDTO',
'postgresql'
)
).to.equal('ux_entity_longer_name_with_pagina_column_longer_name_with_pagin');
).to.equal('ux_entity_longer_name_with_pagi__column_longer_name_with_pag_8b');
});
});
describe('when called with a long name that is near limit and postgresql', () => {
Expand All @@ -205,7 +216,7 @@ describe('Generator Base', () => {
BaseGenerator.getUXConstraintName('testCustomTableName', 'userManyToManyUserManyToManies', 'postgresql').length
).to.be.lessThan(64);
expect(BaseGenerator.getUXConstraintName('testCustomTableName', 'userManyToManyUserManyToManies', 'postgresql')).to.equal(
'ux_test_custom_table_name_user_many_to_many_user_many_to_manies'
'ux_test_custom_table_name__user_many_to_many_user_many_to_ma_72'
);
});
});
Expand All @@ -215,7 +226,7 @@ describe('Generator Base', () => {
BaseGenerator.getUXConstraintName('testCustomTableNames', 'userManyToManyUserManyToManies', 'postgresql')
).to.have.length(63);
expect(BaseGenerator.getUXConstraintName('testCustomTableNames', 'userManyToManyUserManyToManies', 'postgresql')).to.equal(
'ux_test_custom_table_names_user_many_to_many_user_many_to_manie'
'ux_test_custom_table_names__user_many_to_many_user_many_to_m_50'
);
});
});
Expand All @@ -236,7 +247,7 @@ describe('Generator Base', () => {
'postgresql',
true
)
).to.equal('ux_entityLongerNameWithPagination_columnLongerNameWithPaginatio');
).to.equal('ux_entityLongerNameWithPaginati__columnLongerNameWithPaginat_8b');
});
});
});
Expand Down
Loading

0 comments on commit 6de6754

Please sign in to comment.