Skip to content

Commit

Permalink
Supports Hibernate 6.2.x and 6.3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
filipelautert committed Sep 20, 2023
1 parent 8047e55 commit 5588576
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 62 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
strategy:
fail-fast: false
matrix:
java: [ 17, 19 ]
java: [ 17, 21 ]

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -100,7 +100,7 @@ jobs:
strategy:
fail-fast: false
matrix:
hibernate: [ "6.0.2.Final", "6.1.7.Final" ]
hibernate: [ "6.0.2.Final", "6.1.7.Final", "6.2.7.Final", "6.3.1.Final" ]

steps:
- uses: actions/checkout@v4
Expand Down
21 changes: 2 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>6.3.0.Final</hibernate.version>
<hibernate.version>6.3.1.Final</hibernate.version>
<spring.version>6.0.12</spring.version>
<liquibase.version>4.23.2</liquibase.version>
<!--suppress UnresolvedMavenProperty -->
Expand Down Expand Up @@ -226,23 +226,6 @@
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.codehaus.gmavenplus</groupId>-->
<!-- <artifactId>gmavenplus-plugin</artifactId>-->
<!-- <version>1.13.1</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <goals>-->
<!-- <goal>addSources</goal>-->
<!-- <goal>addTestSources</goal>-->
<!-- <goal>compile</goal>-->
<!-- <goal>compileTests</goal>-->
<!-- <goal>removeStubs</goal>-->
<!-- <goal>removeTestStubs</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
Expand All @@ -256,7 +239,7 @@
<configuration>
<rules>
<requireJavaVersion>
<version>1.8</version>
<version>11</version>
</requireJavaVersion>
</rules>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.*;

import java.util.Iterator;
import java.util.Map;

public class IndexSnapshotGenerator extends HibernateSnapshotGenerator {

Expand All @@ -29,16 +29,13 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot
if (hibernateTable == null) {
return example;
}
Iterator<org.hibernate.mapping.Index> indexIterator = hibernateTable.getIndexIterator();
while (indexIterator.hasNext()) {
org.hibernate.mapping.Index hibernateIndex = indexIterator.next();
Map<String, org.hibernate.mapping.Index> indexes = hibernateTable.getIndexes();
for (org.hibernate.mapping.Index hibernateIndex : indexes.values()) {
Index index = new Index();
index.setRelation(table);
index.setName(hibernateIndex.getName());
index.setUnique(isUniqueIndex(hibernateIndex));
Iterator<org.hibernate.mapping.Column> columnIterator = hibernateIndex.getColumnIterator();
while (columnIterator.hasNext()) {
org.hibernate.mapping.Column hibernateColumn = columnIterator.next();
for (var hibernateColumn : hibernateIndex.getColumns()) {
String hibernateOrder = hibernateIndex.getColumnOrderMap().get(hibernateColumn);
Boolean descending = HIBERNATE_ORDER_ASC.equals(hibernateOrder)
? Boolean.FALSE
Expand Down Expand Up @@ -67,16 +64,13 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
if (hibernateTable == null) {
return;
}
Iterator<org.hibernate.mapping.Index> indexIterator = hibernateTable.getIndexIterator();
while (indexIterator.hasNext()) {
org.hibernate.mapping.Index hibernateIndex = indexIterator.next();
for (var hibernateIndex : hibernateTable.getIndexes().values()) {
Index index = new Index();
index.setRelation(table);
index.setName(hibernateIndex.getName());
index.setUnique(isUniqueIndex(hibernateIndex));
Iterator<org.hibernate.mapping.Column> columnIterator = hibernateIndex.getColumnIterator();
while (columnIterator.hasNext()) {
org.hibernate.mapping.Column hibernateColumn = columnIterator.next();

for (var hibernateColumn : hibernateIndex.getColumns()) {
String hibernateOrder = hibernateIndex.getColumnOrderMap().get(hibernateColumn);
Boolean descending = HIBERNATE_ORDER_ASC.equals(hibernateOrder)
? Boolean.FALSE
Expand All @@ -95,7 +89,7 @@ private Boolean isUniqueIndex(org.hibernate.mapping.Index hibernateIndex) {
actual diff in certain non-unique indexes
*/
if (hibernateIndex.getColumnSpan() == 1) {
org.hibernate.mapping.Column col = hibernateIndex.getColumnIterator().next();
var col = hibernateIndex.getColumns().get(0);
return col.isUnique();
} else {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,15 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
if (hibernateTable == null) {
return;
}
Iterator uniqueIterator = hibernateTable.getUniqueKeyIterator();
while (uniqueIterator.hasNext()) {
org.hibernate.mapping.UniqueKey hibernateUnique = (org.hibernate.mapping.UniqueKey) uniqueIterator.next();

for (var hibernateUnique : hibernateTable.getUniqueKeys().values()) {
UniqueConstraint uniqueConstraint = new UniqueConstraint();
uniqueConstraint.setName(hibernateUnique.getName());
uniqueConstraint.setRelation(table);
uniqueConstraint.setClustered(false); // No way to set true via Hibernate
Iterator columnIterator = hibernateUnique.getColumnIterator();

int i = 0;
while (columnIterator.hasNext()) {
org.hibernate.mapping.Column hibernateColumn = (org.hibernate.mapping.Column) columnIterator.next();
uniqueConstraint.addColumn(i, new Column(hibernateColumn.getName()).setRelation(table));
i++;
for (var hibernateColumn : hibernateUnique.getColumns()) {
uniqueConstraint.addColumn(i++, new Column(hibernateColumn.getName()).setRelation(table));
}

Index index = getBackingIndex(uniqueConstraint, hibernateTable, snapshot);
Expand All @@ -62,9 +57,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
Scope.getCurrentScope().getLog(getClass()).info("Found unique constraint " + uniqueConstraint.toString());
table.getUniqueConstraints().add(uniqueConstraint);
}
Iterator columnIterator = hibernateTable.getColumnIterator();
while (columnIterator.hasNext()) {
org.hibernate.mapping.Column column = (org.hibernate.mapping.Column) columnIterator.next();
for (var column : hibernateTable.getColumns()) {
if (column.isUnique()) {
UniqueConstraint uniqueConstraint = new UniqueConstraint();
uniqueConstraint.setRelation(table);
Expand All @@ -84,9 +77,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
}
}

Iterator<UniqueConstraint> ucIter = table.getUniqueConstraints().iterator();
while (ucIter.hasNext()) {
UniqueConstraint uc = ucIter.next();
for (UniqueConstraint uc : table.getUniqueConstraints()) {
if (uc.getName() == null || uc.getName().isEmpty()) {
String name = table.getName() + uc.getColumnNames();
name = "UCIDX" + hashedName(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.Test;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashSet;
Expand All @@ -34,8 +35,7 @@
import java.util.Map.Entry;
import java.util.Set;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.*;

public class SpringPackageScanningIntegrationTest {
private static final String PACKAGES = "com.example.ejb3.auction";
Expand All @@ -52,7 +52,7 @@ public void setUp() throws Exception {
database = new HsqlDatabase();
database.setConnection(new JdbcConnection(connection));

Set<Class<? extends DatabaseObject>> typesToInclude = new HashSet<Class<? extends DatabaseObject>>();
Set<Class<? extends DatabaseObject>> typesToInclude = new HashSet<>();
typesToInclude.add(Table.class);
typesToInclude.add(Column.class);
typesToInclude.add(PrimaryKey.class);
Expand Down Expand Up @@ -95,16 +95,14 @@ public void runGeneratedChangeLog() throws Exception {

DiffResult diffResult = liquibase.diff(hibernateDatabase, database, compareControl);
boolean isTablePrefixWithZZ_ = diffResult.getMissingObjects().stream()
.filter(e -> e.getName().equals("zz_AuditedItem_AUD"))
.findAny()
.isPresent();
.anyMatch(e -> e.getName().equals("zz_AuditedItem_AUD"));
assertTrue(isTablePrefixWithZZ_);
assertTrue(diffResult.getMissingObjects().size() > 0);
assertFalse(diffResult.getMissingObjects().isEmpty());

File outFile = File.createTempFile("lb-test", ".xml");
OutputStream outChangeLog = new FileOutputStream(outFile);
String changeLogString = toChangeLog(diffResult);
outChangeLog.write(changeLogString.getBytes("UTF-8"));
outChangeLog.write(changeLogString.getBytes(StandardCharsets.UTF_8));
outChangeLog.close();

Scope.getCurrentScope().getLog(getClass()).info("Changelog:\n" + changeLogString);
Expand Down Expand Up @@ -287,21 +285,21 @@ private void hibernateSchemaExport(boolean enhancedId) throws Exception {

private String toString(DiffResult diffResult) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(out, true, "UTF-8");
PrintStream printStream = new PrintStream(out, true, StandardCharsets.UTF_8);
DiffToReport diffToReport = new DiffToReport(diffResult, printStream);
diffToReport.print();
printStream.close();
return out.toString("UTF-8");
return out.toString(StandardCharsets.UTF_8);
}

private String toChangeLog(DiffResult diffResult) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(out, true, "UTF-8");
PrintStream printStream = new PrintStream(out, true, StandardCharsets.UTF_8);
DiffToChangeLog diffToChangeLog = new DiffToChangeLog(diffResult,
new DiffOutputControl().setIncludeCatalog(false).setIncludeSchema(false));
diffToChangeLog.print(printStream);
printStream.close();
return out.toString("UTF-8");
return out.toString(StandardCharsets.UTF_8);
}

private void ignoreDatabaseChangeLogTable(DiffResult diffResult)
Expand Down

0 comments on commit 5588576

Please sign in to comment.