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

diff changelog is removing unique constraint since 4.21.0 (hibernate6 + postgresql) #479

Closed
amazing4u opened this issue Apr 16, 2023 · 9 comments · Fixed by #480
Closed
Assignees

Comments

@amazing4u
Copy link

amazing4u commented Apr 16, 2023

Hi,

with version 4.21.0 and 4.21.1 of liquibase-hibernate6 there is a new bug.
The diff changeLog creates a change which is removing the unique constraint.

Here is a sample to reproduce:

Entity:

@Entity
@Builder
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Setter
@Table(name = "users")
public class UserEntity implements UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Setter(AccessLevel.NONE)
    @Column(name = "id")
    private long id;

    @Column(name = "username", unique = true, nullable = false)
    private String username;

    @Column(name = "password", nullable = false)
    private String password;
    }
}
  1. mvn:liquibase:generateChangeLog is working fine and creates following sql:
-- changeset Neo:1681571634046-1
CREATE TABLE "users" ("id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, "password" VARCHAR(255) NOT NULL, "username" VARCHAR(255) NOT NULL, CONSTRAINT "users_pkey" PRIMARY KEY ("id"));

-- changeset Neo:1681571634046-2
ALTER TABLE "users" ADD CONSTRAINT "uc_usersusername_col" UNIQUE ("username");

  1. mvn liquibase:update to apply the change and create the table

  2. mvn liquibase:diff to create a diff. Expecting no changes. But it creates following change:

-- changeset Neo:1681630676734-2
ALTER TABLE users DROP CONSTRAINT uc_usersusername_col;

I am using liquibase-maven-plugin version 4.21.1 + liquibase-hibernate6 version 4.21.1 + postgresql 15.2

application.properties

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver-class-name=org.postgresql.Driver

pom.xml:

<referenceDriver>liquibase.ext.hibernate.database.connection.HibernateDriver</referenceDriver>
<referenceUrl>
       hibernate:spring:net.myclient.myclient.model?dialect=org.hibernate.dialect.PostgreSQLDialect
</referenceUrl>


@amazing4u amazing4u changed the title diff changelog is removing unique constraint since 4.21 (hibernate6 + postgresql) diff changelog is removing unique constraint since 4.21.0 (hibernate6 + postgresql) Apr 16, 2023
@EggOxygen
Copy link

Same here. It looks like it won't handle any DDL operations related to unique constraint now.

@fleboulch
Copy link
Contributor

fleboulch commented Apr 19, 2023

It is linked to #436.
I tried to generate the diff changelog with the new liquibase version and the bug reported is still present

@filipelautert
Copy link
Collaborator

So @fleboulch - #436 says that it dropped and recreated the constraints. So now it is just dropping...
@amazing4u thanks for the example code, let's see if we can create some tests around it to fix this.

@filipelautert
Copy link
Collaborator

filipelautert commented Apr 20, 2023

Hello @amazing4u @javaboy79 - would you be able to test if it works using the jars from https://github.com/liquibase/liquibase-hibernate/actions/runs/4756616505?pr=480 ?

@filipelautert filipelautert moved this from Development to In Development PR Issues in Liquibase Open Source Apr 20, 2023
@wslaghekke
Copy link

@filipelautert I had issues with constraints being dropped on 4.21.0 as well, a few of the drop issues are gone with the new jar, but it is still dropping a few.
The dropped constraints are always in tables that have more than 1 constraint, could that be caused by them having the same name? (because you use the table name as the name of the index)

    protected Index getBackingIndex(UniqueConstraint uniqueConstraint, org.hibernate.mapping.Table hibernateTable, DatabaseSnapshot snapshot) {
        Index index = new Index();
        index.setRelation(uniqueConstraint.getRelation());
        index.setColumns(uniqueConstraint.getColumns());
        index.setUnique(true);
        index.setName(hibernateTable.getName() + "_IX");    <-- here

        return index;
    }

https://github.com/liquibase/liquibase-hibernate/pull/480/files#diff-60252904a8a1921f432e9a194d85c9eed4ffbe3a15ef94660e2c2a27e6c6cd3aR119

@filipelautert
Copy link
Collaborator

Nice catch @wslaghekke ! As I don't have it setup yet, I created a simple fix and built it: https://github.com/liquibase/liquibase-hibernate/actions/runs/4756901871?pr=480 . Could you help to test again? If it works I'll think of a more elegant solution then just adding 4 random chars to the idx name.

@wslaghekke
Copy link

@filipelautert I've just tested that version and my changelog is empty now so that has fixed the issue.

@filipelautert filipelautert moved this from In Development PR Issues to Code Review in Liquibase Open Source Apr 20, 2023
@filipelautert filipelautert moved this from Code Review to In Development PR Issues in Liquibase Open Source Apr 20, 2023
@github-project-automation github-project-automation bot moved this from In Development PR Issues to Done in Liquibase Open Source Apr 24, 2023
@wslaghekke
Copy link

@filipelautert Any idea when the fixed version will be released?

@filipelautert
Copy link
Collaborator

Hello @wslaghekke -maybe this or next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
5 participants