Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct issue with invalid contraint being generated for ManyToMany relationships #26911

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions generators/liquibase/__snapshots__/incremental-liquibase.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,174 @@ ROLE_USER
}
`;

exports[`generator - app - --incremental-changelog when adding a many-to-many relationship should match snapshot 1`] = `
{
"src/main/resources/config/liquibase/changelog/20200102000100_updated_entity_One.xml": {
"contents": "<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

<!--
Added relationships to the entity One.
-->
<changeSet id="20200102000100-1-add-relationships" author="jhipster">


<createTable tableName="rel_one__another">
<column name="another_another_id" type="bigint">
<constraints nullable="false"/>
</column>
<column name="one_one_id" type="bigint">
<constraints nullable="false"/>
</column>
</createTable>

<addPrimaryKey columnNames="one_one_id, another_another_id" tableName="rel_one__another"/>
</changeSet>

<!-- jhipster-needle-liquibase-add-changeset - JHipster will add changesets here -->

</databaseChangeLog>
",
"stateCleared": "modified",
},
"src/main/resources/config/liquibase/changelog/20200102000100_updated_entity_constraints_One.xml": {
"contents": "<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">


<!--
Added the relationship constraints for entity One.
-->
<changeSet id="20200102000100-relationship-foreign-keys" author="jhipster">

<addForeignKeyConstraint baseColumnNames="one_one_id"
baseTableName="rel_one__another"
constraintName="fk_rel_one__another__one_id"
referencedColumnNames="one_id"
referencedTableName="one"
/>

<addForeignKeyConstraint baseColumnNames="another_another_id"
baseTableName="rel_one__another"
constraintName="fk_rel_one__another__another_id"
referencedColumnNames="another_id"
referencedTableName="another"
/>
</changeSet>
</databaseChangeLog>
",
"stateCleared": "modified",
},
"src/main/resources/config/liquibase/changelog/20200102000100_updated_entity_migrate_One.xml": {
"contents": "<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

<changeSet id="20200102000100-columns-faker" author="jhipster" context="faker">
<!--
Load sample data generated with Faker.js
- This data can be easily edited using a CSV editor (or even MS Excel) and
is located in the 'src/main/resources/config/liquibase/fake-data' directory
- By default this data is applied when running with the JHipster 'dev' profile.
This can be customized by adding or removing 'faker' in the 'spring.liquibase.contexts'
Spring Boot configuration key.
-->
<loadUpdateData
file="config/liquibase/fake-data/20200102000100_entity_one.csv"
separator=";"
onlyUpdate="true"
primaryKey="id"
tableName="one">
<column name="id" type="numeric"/>
<!-- jhipster-needle-liquibase-add-loadcolumn - JHipster (and/or extensions) can add load columns here -->
</loadUpdateData>
</changeSet>
<!-- jhipster-needle-liquibase-add-changeset - JHipster will add changesets here -->

</databaseChangeLog>
",
"stateCleared": "modified",
},
"src/main/resources/config/liquibase/data/authority.csv": {
"contents": "name
ROLE_ADMIN
ROLE_USER
",
"stateCleared": "modified",
},
"src/main/resources/config/liquibase/data/user.csv": {
"contents": "id;login;password_hash;first_name;last_name;email;image_url;activated;lang_key;created_by;last_modified_by
1;admin;$2a$10$gSAhZrxMllrbgj/kkK9UceBPpChGWJA7SYIb1Mqo.n5aNLq1/oRrC;Administrator;Administrator;admin@localhost;;true;en;system;system
2;user;$2a$10$VEjxo0jq2YG9Rbk2HmX9S.k1uZBGYUHdUcid3g/vfiEl7lwWgOH/K;User;User;user@localhost;;true;en;system;system
",
"stateCleared": "modified",
},
"src/main/resources/config/liquibase/data/user_authority.csv": {
"contents": "user_id;authority_name
1;ROLE_ADMIN
1;ROLE_USER
2;ROLE_USER
",
"stateCleared": "modified",
},
"src/main/resources/config/liquibase/fake-data/20200102000100_entity_one.csv": {
"contents": "one_id
1
2
3
4
5
6
7
8
9
10
",
"stateCleared": "modified",
},
"src/main/resources/config/liquibase/master.xml": {
"contents": "<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<property name="now" value="current_timestamp" dbms="postgresql"/>
<property name="floatType" value="float4" dbms="postgresql"/>
<property name="clobType" value="clob" dbms="postgresql"/>
<property name="blobType" value="blob" dbms="postgresql"/>
<property name="uuidType" value="uuid" dbms="postgresql"/>
<property name="datetimeType" value="datetime" dbms="postgresql"/>

<include file="config/liquibase/changelog/00000000000000_initial_schema.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/20200101000100_added_entity_One.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/20200101000200_added_entity_Another.xml" relativeToChangelogFile="false"/>
<!-- jhipster-needle-liquibase-add-changelog - JHipster will add liquibase changelogs here -->
<!-- jhipster-needle-liquibase-add-constraints-changelog - JHipster will add liquibase constraints changelogs here -->
<include file="config/liquibase/changelog/20200102000100_updated_entity_One.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/20200102000100_updated_entity_migrate_One.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/20200102000100_updated_entity_constraints_One.xml" relativeToChangelogFile="false"/>
<!-- jhipster-needle-liquibase-add-incremental-changelog - JHipster will add incremental liquibase changelogs here -->
</databaseChangeLog>
",
"stateCleared": "modified",
},
}
`;

exports[`generator - app - --incremental-changelog when adding a relationship should match snapshot 1`] = `
{
"src/main/resources/config/liquibase/changelog/20200102000100_updated_entity_One.xml": {
Expand Down
87 changes: 87 additions & 0 deletions generators/liquibase/incremental-liquibase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ relationship ManyToOne {
}
`;

const jdlApplicationWithEntitiesAndAddedNewMnyToManyRelationship = `
${jdlApplicationWithEntities}
relationship ManyToMany {
One to Another
}`;

const generatorPath = join(__dirname, '../server/index.js');
const mockedGenerators = ['jhipster:common'];

Expand Down Expand Up @@ -730,6 +736,87 @@ entity Customer {
expect(runResult.getSnapshot('**/src/main/resources/config/liquibase/**')).toMatchSnapshot();
});
});

describe('when adding a many-to-many relationship', () => {
let runResult;
before(async () => {
const baseName = 'JhipsterApp';
const initialState = createImporterFromContent(jdlApplicationWithEntities, {
...options,
creationTimestampConfig: options.creationTimestamp,
}).import();
const applicationWithEntities = initialState.exportedApplicationsWithEntities[baseName];
expect(applicationWithEntities).toBeTruthy();
expect(applicationWithEntities.entities.length).toBe(2);
runResult = await helpers
.create(generatorPath)
.withJHipsterConfig(config)
.withOptions({ ...options, applicationWithEntities })
.run();

const state = createImporterFromContent(jdlApplicationWithEntitiesAndAddedNewMnyToManyRelationship, {
...options,
}).import();
runResult = await runResult
.create(generatorPath)
.withOptions({
...options,
applicationWithEntities: state.exportedApplicationsWithEntities[baseName],
creationTimestamp: '2020-01-02',
})
.run();
});

after(() => runResult.cleanup());

it('should create application', () => {
runResult.assertFile(['.yo-rc.json']);
});
it('should create entity config file', () => {
runResult.assertFile([join('.jhipster', 'One.json'), join('.jhipster', 'Another.json')]);
});
it('should create entity initial changelog', () => {
runResult.assertFile([
`${SERVER_MAIN_RES_DIR}config/liquibase/changelog/20200101000100_added_entity_One.xml`,
`${SERVER_MAIN_RES_DIR}config/liquibase/changelog/20200101000200_added_entity_Another.xml`,
]);
});
it('should create entity initial fake data', () => {
runResult.assertFile([`${SERVER_MAIN_RES_DIR}config/liquibase/fake-data/20200101000100_entity_one.csv`]);
});
it('should create relationship table', () => {
runResult.assertFile([`${SERVER_MAIN_RES_DIR}config/liquibase/changelog/20200102000100_updated_entity_One.xml`]);
runResult.assertFileContent(
`${SERVER_MAIN_RES_DIR}config/liquibase/changelog/20200102000100_updated_entity_One.xml`,
'createTable tableName="rel_one__another"',
);
runResult.assertFileContent(
`${SERVER_MAIN_RES_DIR}config/liquibase/changelog/20200102000100_updated_entity_One.xml`,
'column name="another_another_id" type="bigint"',
);
runResult.assertFileContent(
`${SERVER_MAIN_RES_DIR}config/liquibase/changelog/20200102000100_updated_entity_One.xml`,
'column name="one_one_id" type="bigint"',
);
});
it('should create the entity constraint update changelog referencing both columns of the join table', () => {
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="one_one_id"',
);

runResult.assertFileContent(
`${SERVER_MAIN_RES_DIR}config/liquibase/changelog/20200102000100_updated_entity_constraints_One.xml`,
'baseColumnNames="another_another_id"',
);
});
it('should match snapshot', () => {
expect(runResult.getSnapshot('**/src/main/resources/config/liquibase/**')).toMatchSnapshot();
});
});

describe('when adding a relationship with on handlers', () => {
let runResult;
before(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ if (hasFieldConstraint) { _%>
const otherEntityConstraintName = this.getFKConstraintName(relationship.joinTable.name, relationship.columnName, prodDatabaseType);
_%>

<addForeignKeyConstraint baseColumnNames="<%= entity.primaryKey.fields.map(field => relationship.columnName + '_' + field.columnName).join(', ') %>"
<addForeignKeyConstraint baseColumnNames="<%= entity.primaryKey.fields.map(field => entity.entityTableName + '_' + field.columnName).join(', ') %>"
baseTableName="<%= relationship.joinTable.name %>"
constraintName="<%= constraintName %>"
referencedColumnNames="<%= entity.primaryKey.fields.map(field => field.columnName).join(', ') %>"
Expand Down
Loading