Skip to content

Commit

Permalink
use unique name for backing index of unique constraints (#555)
Browse files Browse the repository at this point in the history
* use unique name for backing index of unique constraints


* There may be edge cases where the index name could be null, so in this case we still using the table name + random identifier.

---------

Co-authored-by: filipe <flautert@liquibase.org>
  • Loading branch information
chrstnbrn and filipelautert committed Nov 8, 2023
1 parent a3d52c1 commit 0b7b52e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ protected Index getBackingIndex(UniqueConstraint uniqueConstraint, org.hibernate
index.setRelation(uniqueConstraint.getRelation());
index.setColumns(uniqueConstraint.getColumns());
index.setUnique(true);
index.setName(String.format("%s_%s_IX",hibernateTable.getName(), StringUtil.randomIdentifer(4)));
if (StringUtil.isNotEmpty(uniqueConstraint.getName())) {
index.setName(uniqueConstraint.getName() + "_IX");
} else {
index.setName(String.format("%s_%s_IX",hibernateTable.getName(), StringUtil.randomIdentifer(4)));
}

return index;
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/example/ejb3/auction/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class Item {
@Column(unique = true)
private String name;

@Column(unique = true)
private String key;

public long getId() {
return id;
}
Expand All @@ -28,4 +31,7 @@ public void setName(String name) {
this.name = name;
}

public String getKey() { return key; }

public void setKey(String key) { this.key = key; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public void runGeneratedChangeLog() throws Exception {

assertEquals(differences, 0, diffResult.getMissingObjects().size());
assertEquals(differences, 0, diffResult.getUnexpectedObjects().size());
assertEquals(differences, 0, diffResult.getChangedObjects(UniqueConstraint.class).size());
// assertEquals(differences, 0, diffResult.getChangedObjects().size()); //unimportant differences in schema name and datatypes causing test to fail

}
Expand Down

0 comments on commit 0b7b52e

Please sign in to comment.