Skip to content

Commit

Permalink
merged 4.0beta into main
Browse files Browse the repository at this point in the history
  • Loading branch information
molivasdat committed Jul 16, 2020
2 parents 7350d8f + e98873a commit 5cef3d1
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 45 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate5</artifactId>
<version>3.10.1-SNAPSHOT</version>
<version>4.0.0-SNAPSHOT</version>

<name>Liquibase Hibernate Integration</name>
<description>Liquibase extension for hibernate integration including generating changesets based on changed hibernate mapping files </description>
Expand Down Expand Up @@ -133,7 +133,7 @@
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.10.1</version>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package liquibase.ext.hibernate.database;

import liquibase.Scope;
import liquibase.database.AbstractJdbcDatabase;
import liquibase.database.DatabaseConnection;
import liquibase.database.jvm.JdbcConnection;
Expand All @@ -11,7 +12,6 @@
import liquibase.logging.LogFactory;
import liquibase.logging.LogService;
import liquibase.logging.Logger;
import liquibase.logging.LoggerFactory;
import org.hibernate.annotations.common.reflection.ClassLoaderDelegate;
import org.hibernate.annotations.common.reflection.ClassLoadingException;
import org.hibernate.boot.Metadata;
Expand Down Expand Up @@ -42,8 +42,6 @@
*/
public abstract class HibernateDatabase extends AbstractJdbcDatabase {

protected static final Logger LOG = LogService.getLog(HibernateDatabase.class);

private Metadata metadata;
protected Dialect dialect;

Expand Down Expand Up @@ -80,7 +78,7 @@ public void setConnection(DatabaseConnection conn) {
super.setConnection(conn);

try {
LOG.info("Reading hibernate configuration " + getConnection().getURL());
Scope.getCurrentScope().getLog(getClass()).info("Reading hibernate configuration " + getConnection().getURL());

this.metadata = buildMetadata();

Expand Down Expand Up @@ -161,7 +159,7 @@ protected Metadata buildMetadataFromPath() throws DatabaseException {
AtomicReference<Metadata> result = new AtomicReference<>();

Thread t = new Thread(() -> result.set(metadataBuilder.build()));
t.setContextClassLoader(getHibernateConnection().getResourceAccessor().toClassLoader());
t.setContextClassLoader(Scope.getCurrentScope().getClassLoader());
t.setUncaughtExceptionHandler((_t,e) -> thrownException.set(e));
t.start();
try {
Expand All @@ -186,12 +184,12 @@ protected MetadataSources createMetadataSources() throws DatabaseException {
if (dialectString != null) {
try {
dialect = (Dialect) Thread.currentThread().getContextClassLoader().loadClass(dialectString).newInstance();
LOG.info("Using dialect " + dialectString);
Scope.getCurrentScope().getLog(getClass()).info("Using dialect " + dialectString);
} catch (Exception e) {
throw new DatabaseException(e);
}
} else {
LOG.info("Could not determine hibernate dialect, using HibernateGenericDialect");
Scope.getCurrentScope().getLog(getClass()).info("Could not determine hibernate dialect, using HibernateGenericDialect");
dialect = new HibernateGenericDialect();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.persistence.metamodel.ManagedType;
import javax.persistence.spi.PersistenceUnitTransactionType;

import liquibase.Scope;
import liquibase.logging.LogService;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
Expand Down Expand Up @@ -63,12 +64,12 @@ protected Metadata buildMetadataFromPath() throws DatabaseException {
if (dialectString != null) {
try {
dialect = (Dialect) Class.forName(dialectString).newInstance();
LOG.info("Using dialect " + dialectString);
Scope.getCurrentScope().getLog(getClass()).info("Using dialect " + dialectString);
} catch (Exception e) {
throw new DatabaseException(e);
}
} else {
LOG.info("Could not determine hibernate dialect, using HibernateGenericDialect");
Scope.getCurrentScope().getLog(getClass()).info("Could not determine hibernate dialect, using HibernateGenericDialect");
dialect = new HibernateGenericDialect();
}

Expand Down Expand Up @@ -163,9 +164,9 @@ protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(Persistence
setField(persistenceUnitDescriptor, "jtaDataSource", null);
setField(persistenceUnitDescriptor, "transactionType", PersistenceUnitTransactionType.RESOURCE_LOCAL);
} catch (Exception ex) {
LogService.getLog(HibernateEjb3Database.class).severe(null, ex);
Scope.getCurrentScope().getLog(getClass()).severe(null, ex);
}
return super.getEntityManagerFactoryBuilder(persistenceUnitDescriptor, integration, providedClassLoader);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package liquibase.ext.hibernate.database;

import liquibase.Scope;
import liquibase.database.DatabaseConnection;
import liquibase.exception.DatabaseException;
import liquibase.ext.hibernate.database.connection.HibernateConnection;
Expand Down Expand Up @@ -100,7 +101,7 @@ protected void configureSources(MetadataSources sources) throws DatabaseExceptio
List<TypedStringValue> annotatedClasses = (List<TypedStringValue>) annotatedClassesProperty.getValue();
if (annotatedClasses != null) {
for (TypedStringValue className : annotatedClasses) {
LOG.info("Found annotated class " + className.getValue());
Scope.getCurrentScope().getLog(getClass()).info("Found annotated class " + className.getValue());
sources.addAnnotatedClass(findClass(className.getValue()));
}
}
Expand All @@ -114,11 +115,11 @@ protected void configureSources(MetadataSources sources) throws DatabaseExceptio
if (mappingLocations != null) {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
for (TypedStringValue mappingLocation : mappingLocations) {
LOG.info("Found mappingLocation " + mappingLocation.getValue());
Scope.getCurrentScope().getLog(getClass()).info("Found mappingLocation " + mappingLocation.getValue());
Resource[] resources = resourcePatternResolver.getResources(mappingLocation.getValue());
for (int i = 0; i < resources.length; i++) {
URL url = resources[i].getURL();
LOG.info("Adding resource " + url);
Scope.getCurrentScope().getLog(getClass()).info("Adding resource " + url);
sources.addURL(url);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import javax.persistence.spi.PersistenceUnitInfo;

import liquibase.Scope;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
import org.hibernate.jpa.boot.spi.Bootstrap;
Expand Down Expand Up @@ -70,12 +71,12 @@ protected boolean isXmlFile(DatabaseConnection connection) {
@Override
protected EntityManagerFactoryBuilderImpl createEntityManagerFactoryBuilder() {
DefaultPersistenceUnitManager internalPersistenceUnitManager = new DefaultPersistenceUnitManager();
internalPersistenceUnitManager.setResourceLoader(new DefaultResourceLoader(getHibernateConnection().getResourceAccessor().toClassLoader()));
internalPersistenceUnitManager.setResourceLoader(new DefaultResourceLoader(Scope.getCurrentScope().getClassLoader()));

String[] packagesToScan = getHibernateConnection().getPath().split(",");

for (String packageName : packagesToScan) {
LOG.info("Found package " + packageName);
Scope.getCurrentScope().getLog(getClass()).info("Found package " + packageName);
}

internalPersistenceUnitManager.setPackagesToScan(packagesToScan);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package liquibase.ext.hibernate.snapshot;

import liquibase.Scope;
import liquibase.datatype.DataTypeFactory;
import liquibase.datatype.core.UnknownType;
import liquibase.exception.DatabaseException;
Expand All @@ -13,7 +14,7 @@
import liquibase.structure.core.Relation;
import liquibase.structure.core.Table;
import liquibase.util.SqlUtil;
import liquibase.util.StringUtils;
import liquibase.util.StringUtil;
import org.hibernate.boot.Metadata;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.PostgreSQL81Dialect;
Expand Down Expand Up @@ -114,7 +115,7 @@ protected void snapshotColumn(Column column, DatabaseSnapshot snapshot) throws D
}

column.setType(dataType);
LOG.info("Found column " + column.getName() + " " + column.getType().toString());
Scope.getCurrentScope().getLog(getClass()).info("Found column " + column.getName() + " " + column.getType().toString());

column.setRemarks(hibernateColumn.getComment());
if (hibernateColumn.getValue() instanceof SimpleValue) {
Expand Down Expand Up @@ -195,7 +196,7 @@ protected DataType toDataType(String hibernateType, Integer sqlTypeCode) throws
dataType.setDecimalDigits(Integer.parseInt(matcher.group(3)));
}

String extra = StringUtils.trimToNull(matcher.group(4));
String extra = StringUtil.trimToNull(matcher.group(4));
if (extra != null) {
if (extra.equalsIgnoreCase("char")) {
dataType.setColumnSizeUnit(DataType.ColumnSizeUnit.CHAR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public abstract class HibernateSnapshotGenerator implements SnapshotGenerator {
private Class<? extends DatabaseObject> defaultFor = null;
private Class<? extends DatabaseObject>[] addsTo = null;

protected static final Logger LOG = LogService.getLog(HibernateSnapshotGenerator.class);


protected HibernateSnapshotGenerator(Class<? extends DatabaseObject> defaultFor) {
this.defaultFor = defaultFor;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package liquibase.ext.hibernate.snapshot;

import liquibase.Scope;
import liquibase.exception.DatabaseException;
import liquibase.snapshot.DatabaseSnapshot;
import liquibase.snapshot.InvalidExampleException;
Expand Down Expand Up @@ -39,7 +40,7 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot
}

if (index.getColumnNames().equalsIgnoreCase(((Index) example).getColumnNames())) {
LOG.info("Found index " + index.getName());
Scope.getCurrentScope().getLog(getClass()).info("Found index " + index.getName());
table.getIndexes().add(index);
return index;
}
Expand Down Expand Up @@ -71,7 +72,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
org.hibernate.mapping.Column hibernateColumn = columnIterator.next();
index.getColumns().add(new Column(hibernateColumn.getName()).setRelation(table));
}
LOG.info("Found index " + index.getName());
Scope.getCurrentScope().getLog(getClass()).info("Found index " + index.getName());
table.getIndexes().add(index);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package liquibase.ext.hibernate.snapshot;

import liquibase.Scope;
import liquibase.exception.DatabaseException;
import liquibase.snapshot.DatabaseSnapshot;
import liquibase.snapshot.InvalidExampleException;
Expand Down Expand Up @@ -49,7 +50,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
*/
String hbnTableName = hibernateTable.getName();
if (hbnPrimaryKeyName != null && hbnPrimaryKeyName.length() == 15 && hbnPrimaryKeyName.equals(PK_ALIAS_15.toAliasString(hbnTableName))) {
LOG.warning("Hibernate primary key name is probably truncated. " + hbnPrimaryKeyName);
Scope.getCurrentScope().getLog(getClass()).warning("Hibernate primary key name is probably truncated. " + hbnPrimaryKeyName);
String newAlias = NEW_PK_ALIAS.toAliasString(hbnTableName);
int newAliasLength = newAlias.length();
if (newAliasLength > 15) {
Expand All @@ -59,7 +60,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
} else {
hbnPrimaryKeyName = newAlias;
}
LOG.warning("Changing hibernate primary key name to " + hbnPrimaryKeyName);
Scope.getCurrentScope().getLog(getClass()).warning("Changing hibernate primary key name to " + hbnPrimaryKeyName);
}
}
pk.setName(hbnPrimaryKeyName);
Expand All @@ -68,7 +69,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
pk.getColumns().add(new Column(((org.hibernate.mapping.Column) hibernateColumn).getName()).setRelation(table));
}

LOG.info("Found primary key " + pk.getName());
Scope.getCurrentScope().getLog(getClass()).info("Found primary key " + pk.getName());
table.setPrimaryKey(pk);
Index index = new Index();
index.setName("IX_" + pk.getName());
Expand All @@ -81,4 +82,4 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package liquibase.ext.hibernate.snapshot;

import liquibase.Scope;
import liquibase.exception.DatabaseException;
import liquibase.ext.hibernate.database.HibernateDatabase;
import liquibase.ext.hibernate.snapshot.extension.ExtendedSnapshotGenerator;
Expand Down Expand Up @@ -41,7 +42,7 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot
}

Table table = new Table().setName(hibernateTable.getName());
LOG.info("Found table " + table.getName());
Scope.getCurrentScope().getLog(getClass()).info("Found table " + table.getName());
// table.setSnapshotId(SnapshotIdService.getInstance().generateId());
table.setSchema(example.getSchema());

Expand Down Expand Up @@ -71,7 +72,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
if (hibernateTable.isPhysicalTable()) {
Table table = new Table().setName(hibernateTable.getName());
table.setSchema(schema);
LOG.info("Found table " + table.getName());
Scope.getCurrentScope().getLog(getClass()).info("Found table " + table.getName());
schema.addDatabaseObject(snapshotObject(table, snapshot));
}
}
Expand Down Expand Up @@ -107,7 +108,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
if (hTable.isPhysicalTable()) {
Table table = new Table().setName(hTable.getName());
table.setSchema(schema);
LOG.info("Found table " + table.getName());
Scope.getCurrentScope().getLog(getClass()).info("Found table " + table.getName());
schema.addDatabaseObject(snapshotObject(table, snapshot));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package liquibase.ext.hibernate.snapshot;

import liquibase.Scope;
import liquibase.exception.DatabaseException;
import liquibase.snapshot.DatabaseSnapshot;
import liquibase.snapshot.InvalidExampleException;
Expand Down Expand Up @@ -57,7 +58,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
Index index = getBackingIndex(uniqueConstraint, hibernateTable, snapshot);
uniqueConstraint.setBackingIndex(index);

LOG.info("Found unique constraint " + uniqueConstraint.toString());
Scope.getCurrentScope().getLog(getClass()).info("Found unique constraint " + uniqueConstraint.toString());
table.getUniqueConstraints().add(uniqueConstraint);
}
Iterator columnIterator = hibernateTable.getColumnIterator();
Expand All @@ -73,7 +74,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
}
uniqueConstraint.addColumn(0, new Column(column.getName()).setRelation(table));
uniqueConstraint.setName(name);
LOG.info("Found unique constraint " + uniqueConstraint.toString());
Scope.getCurrentScope().getLog(getClass()).info("Found unique constraint " + uniqueConstraint.toString());
table.getUniqueConstraints().add(uniqueConstraint);

Index index = getBackingIndex(uniqueConstraint, hibernateTable, snapshot);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
liquibase.ext.hibernate.database.HibernateClassicDatabase
liquibase.ext.hibernate.database.HibernateEjb3Database
liquibase.ext.hibernate.database.HibernateSpringBeanDatabase
liquibase.ext.hibernate.database.HibernateSpringPackageDatabase
liquibase.ext.hibernate.database.JpaPersistenceDatabase
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
liquibase.ext.hibernate.diff.ChangedColumnChangeGenerator
liquibase.ext.hibernate.diff.ChangedForeignKeyChangeGenerator
liquibase.ext.hibernate.diff.MissingSequenceChangeGenerator
liquibase.ext.hibernate.diff.UnexpectedIndexChangeGenerator
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
liquibase.ext.hibernate.snapshot.CatalogSnapshotGenerator
liquibase.ext.hibernate.snapshot.ColumnSnapshotGenerator
liquibase.ext.hibernate.snapshot.ForeignKeySnapshotGenerator
liquibase.ext.hibernate.snapshot.IndexSnapshotGenerator
liquibase.ext.hibernate.snapshot.PrimaryKeySnapshotGenerator
liquibase.ext.hibernate.snapshot.SchemaSnapshotGenerator
liquibase.ext.hibernate.snapshot.SequenceSnapshotGenerator
liquibase.ext.hibernate.snapshot.TableSnapshotGenerator
liquibase.ext.hibernate.snapshot.UniqueConstraintSnapshotGenerator
liquibase.ext.hibernate.snapshot.ViewSnapshotGenerator
Loading

0 comments on commit 5cef3d1

Please sign in to comment.