Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
filipelautert committed Sep 20, 2023
1 parent c2c6598 commit 00680a2
Showing 1 changed file with 19 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.*;

import java.util.Map;

public class IndexSnapshotGenerator extends HibernateSnapshotGenerator {

private static final String HIBERNATE_ORDER_ASC = "asc";
Expand All @@ -25,24 +23,12 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot
return example;
}
Relation table = ((Index) example).getRelation();
org.hibernate.mapping.Table hibernateTable = findHibernateTable(table, snapshot);
var hibernateTable = findHibernateTable(table, snapshot);
if (hibernateTable == null) {
return example;
}
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));
for (var hibernateColumn : hibernateIndex.getColumns()) {
String hibernateOrder = hibernateIndex.getColumnOrderMap().get(hibernateColumn);
Boolean descending = HIBERNATE_ORDER_ASC.equals(hibernateOrder)
? Boolean.FALSE
: (HIBERNATE_ORDER_DESC.equals(hibernateOrder) ? Boolean.TRUE : null);
index.getColumns().add(new Column(hibernateColumn.getName()).setRelation(table).setDescending(descending));
}

for (var hibernateIndex : hibernateTable.getIndexes().values()) {
Index index = handleHibernateIndex(table, hibernateIndex);
if (index.getColumnNames().equalsIgnoreCase(((Index) example).getColumnNames())) {
Scope.getCurrentScope().getLog(getClass()).info("Found index " + index.getName());
table.getIndexes().add(index);
Expand All @@ -65,24 +51,28 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
return;
}
for (var hibernateIndex : hibernateTable.getIndexes().values()) {
Index index = new Index();
index.setRelation(table);
index.setName(hibernateIndex.getName());
index.setUnique(isUniqueIndex(hibernateIndex));

for (var hibernateColumn : hibernateIndex.getColumns()) {
String hibernateOrder = hibernateIndex.getColumnOrderMap().get(hibernateColumn);
Boolean descending = HIBERNATE_ORDER_ASC.equals(hibernateOrder)
? Boolean.FALSE
: (HIBERNATE_ORDER_DESC.equals(hibernateOrder) ? Boolean.TRUE : null);
index.getColumns().add(new Column(hibernateColumn.getName()).setRelation(table).setDescending(descending));
}
Index index = handleHibernateIndex(table, hibernateIndex);
Scope.getCurrentScope().getLog(getClass()).info("Found index " + index.getName());
table.getIndexes().add(index);
}
}
}

private Index handleHibernateIndex(Relation table, org.hibernate.mapping.Index hibernateIndex) {
Index index = new Index();
index.setRelation(table);
index.setName(hibernateIndex.getName());
index.setUnique(isUniqueIndex(hibernateIndex));
for (var hibernateColumn : hibernateIndex.getColumns()) {
String hibernateOrder = hibernateIndex.getColumnOrderMap().get(hibernateColumn);
Boolean descending = HIBERNATE_ORDER_ASC.equals(hibernateOrder)
? Boolean.FALSE
: (HIBERNATE_ORDER_DESC.equals(hibernateOrder) ? Boolean.TRUE : null);
index.getColumns().add(new Column(hibernateColumn.getName()).setRelation(table).setDescending(descending));
}
return index;
}

private Boolean isUniqueIndex(org.hibernate.mapping.Index hibernateIndex) {
/*
This seems to be necessary to explicitly tell liquibase that there's no
Expand Down

0 comments on commit 00680a2

Please sign in to comment.