jhipster liquibase validation error after modify entity
Asked Answered
A

5

11

I was trying to add an field to my entity as a CLOB. When using the JHipster CLI it was no problem to add it.

Now, when i trying to start my application i get the following validation error from liquibase:

liquibase.exception.ValidationFailedException: Validation Failed:
     1 change sets check sum
          config/liquibase/changelog/20170221193921_xxxxxxxx.xml::20170221193921-1::jhipster was: 7:d8b3f42d8d4d523c7b14f93b4c7657c7 but is now: 7:a2a365179a0d231c2771ebd79f51b1fc

i also tried the following:

./mvnw liquibase:clearCheckSums

The result was BUILD SUCCESS.

i also tried ./mvnw liquibase:update and updateSQL, same result.

Can anyone tell me what my problem is with JHipster?

Appetizing answered 4/4, 2017 at 12:23 Comment(4)
You shouldn't update your executed changeset and have to create another one or just simple to wipe your db and recreated it from scratch.Lyallpur
facing the same issue and not sure what is the correct approach? have you found the solutionPrisca
In Addition to Anton's comment: Disk-persistent h2 db can be re-created from scratch by setting spring.liquibase.drop-first: true in application.ymlAmimia
liquibase:clearCheckSums helped me. ThanksLuminance
M
24

When we use liquibase all entity changes that happen later should be captured as separate changelogs(For eg. altering a table like adding a new column). Jhipster cli always seems to overwrite the entities(whichever changed) corresponding liquibase files(usually of the pattern 'config/liquibase/changelog/20180607110114_added_entity_Employee.xml'). Therefore the entities liquibase file's checksum changes as it has new content now. And in your database, there is a table called DATABASECHANGELOG which stores which all changeLogs were applied and this has checksum data.

Now when you start your application you will get error because your latest liquibase changeLog of modified entity's checksum is different from the last time(database will have a checksum for this liquibase file for previous verison) you ran.

mvn liquibase:clearCheckSums is not the right approach most of the time unless needed. This actually clears all checksums in the database. You lose track of the changes that had happened which is usually not intended. These features of liquibase makes sense for eg like when you want to rollback the new changes you have applied. If you clear checksums and run the application it will compute new checksums you lose the track and can get you into trouble if not enough attention is paid.

Proper solution:

  1. Make changes to your entities using jhipster entity sub-generator or jdl import or manual changes to your entities directly.
  2. Now check if the entity's corresponding liquibase file was changed or not. Usually the name contains '.._added_entity_...' . For eg. 'config/liquibase/changelog/20180607110114_added_entity_Employee.xml'.
  3. Revert that file back to what it was in case it was overwritten. Git is helpful here for reverting.
  4. If you start the application now you will not get validation checksum error as checksum of the file matches.
  5. But the change we made to entity is not captured in liquibase. For this you have to run mvn liquibase:diff. This will generate a changeLog file. Check it manually once and add it to liquibase's master.xml file. Adding to master file is must as liquibase refers to this file for all changeLogs.
  6. If you run the application now, if the changeLogs were not applied, then liquibase will try to apply these new changes on the database.

To summarize, jhipster cli overwrites the entities liquibase files. Revert them and run mvn liquibase:diff to capture the new changes to the entites in a new changeLog instead of overwriting the previously generated liquibase file. We can see that checksum error is resolved and also database changes are captured in liquibase as well.

References:

Mannes answered 8/6, 2018 at 21:26 Comment(1)
Yes this solves the entity problem but I still get data problem for dev environment, after reverting fake data csv this problem is also being solved.Seleucid
P
4

Try Executing the following query in your DB: UPDATE DATABASECHANGELOG SET MD5SUM = null WHERE ID='YOUR TABLE ID';

YOUR TABLE ID in this case seems to be = 20170221193921-1.

Pejorative answered 4/4, 2017 at 14:35 Comment(5)
It is a bad approach and it is not a liquibase way, please read conception of doing database migrations.Lyallpur
Agreed, the liquibase way is to clear checksums by liquibase:clearCheckSums so liquibase would recompute them in the next run, but it doesn't clear'em, when working with Jhipster. This is a work around I came upon.Pejorative
I have a similar situation as @krypto88.liquibase:clearCheckSums does not work in clearing the liquibase.exception.ValidationFailedException: Validation Failed. So how to get rid of it?Avenge
@Avenge Have you tried the solution in the answer?Pejorative
This indeed resolves the error, but it doesn't re-generate the DB, unless you use in-memory db. So any query referencing one of the changed / added fields, will fail. Disk-persistent db can be re-created by setting spring.liquibase.drop-first: true in application.ymlAmimia
T
1

When we add a column in Jhipster using the command jhpster entity xxxx it is not added as a seperate changeset, instead the column is added in the existing change set for create table and as there is a change in the file new checksum is generated and on startup it is different from the DB so validation fails

<changeSet id="20181209164925-1" author="jhipster" >    
    <createTable tableName="xxxxxx">
        <column name="id" type="bigint" autoIncrement="${autoIncrement}">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="xxxxx" type="integer">

            <constraints nullable="true" />
        </column>
        <column name="xxxxx" type="integer">
            <constraints nullable="true" />
        </column>
        <column name="xxxxxx" type="varchar(140)">
            <constraints nullable="true" />
        </column> 
        <column name="xxxx" type="bigint">
            <constraints nullable="true" />
        </column>
        <!-- jhipster-needle-liquibase-add-column - JHipster will add columns here, do not remove-->
    </createTable>

To fix this issue add a new changeset with add column as tag ..shown below

<changeSet id="20181209164925-2" author="jhipster" runOnChange="true">      
    <addColumn tableName="xxxxxx">
         <column name="xxxxxx" type="date"></column>
    </addColumn>

Refer https://www.liquibase.org/documentation/changes/add_column.html

Todtoday answered 9/12, 2018 at 20:9 Comment(0)
I
1

The quick solution is to run:

./mvnw clean
./mvnw liquibase:clearCheckSums

This will work, however you're missing the point of using Liquibase to track changes. For development purposes it's OK.

The correct way to fix that is to run ./mvnw liquibaseDiffChangeLog to create a diff changelog of the changes made to your database. This can then be added to the master.xml file as a new entry which can be applied next time you fire your app.

Intercellular answered 20/4, 2020 at 16:28 Comment(0)
S
0

All the above answers are valid but since JHipster 7.0.0 you can use jhipster --incremental-changelog to generate your app to avoid modifying initial creation changelog by creating changelog only for what has changed.

It supports:

  • creating/removing fields.
  • creating/removing relationships.

but it's not perfect because it does not support any attribute change like type and constraints

Also you can't enable it after project generation, it should be used from beginning.

Hopefully, this should be the default behavior some day.

Soaring answered 3/9, 2022 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.