diff --git a/src/main/java/liquibase/ext/hibernate/snapshot/HibernateSnapshotGenerator.java b/src/main/java/liquibase/ext/hibernate/snapshot/HibernateSnapshotGenerator.java index 5fbc5190..c0fcaf79 100644 --- a/src/main/java/liquibase/ext/hibernate/snapshot/HibernateSnapshotGenerator.java +++ b/src/main/java/liquibase/ext/hibernate/snapshot/HibernateSnapshotGenerator.java @@ -3,14 +3,13 @@ import liquibase.database.Database; import liquibase.exception.DatabaseException; import liquibase.ext.hibernate.database.HibernateDatabase; -import liquibase.logging.LogFactory; -import liquibase.logging.LogService; -import liquibase.logging.Logger; import liquibase.snapshot.DatabaseSnapshot; import liquibase.snapshot.InvalidExampleException; import liquibase.snapshot.SnapshotGenerator; import liquibase.snapshot.SnapshotGeneratorChain; import liquibase.structure.DatabaseObject; +import liquibase.structure.core.Schema; +import org.hibernate.boot.model.relational.Namespace; import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.mapping.Table; @@ -107,4 +106,27 @@ protected org.hibernate.mapping.Table findHibernateTable(DatabaseObject example, } return null; } + + protected static boolean schemaMatchesTable( + DatabaseObject example, + Table hibernateTable + ) { + if (example.getSchema().getName() != null && example.getSchema().getName().equalsIgnoreCase(hibernateTable.getSchema())) { + return true; + } + + return example.getSchema().isDefault() && hibernateTable.getSchema() == null; + } + + protected static boolean schemaMatchesNamespace( + Schema schema, + Namespace hibernateNamespace + ) { + if (hibernateNamespace.getName().getSchema() != null && hibernateNamespace.getName().getSchema().matches(schema.getName())) { + return true; + } + + return hibernateNamespace.getName().getSchema() == null && schema.isDefault(); + } + } diff --git a/src/main/java/liquibase/ext/hibernate/snapshot/SequenceSnapshotGenerator.java b/src/main/java/liquibase/ext/hibernate/snapshot/SequenceSnapshotGenerator.java index e8c4f85a..80e00f7b 100644 --- a/src/main/java/liquibase/ext/hibernate/snapshot/SequenceSnapshotGenerator.java +++ b/src/main/java/liquibase/ext/hibernate/snapshot/SequenceSnapshotGenerator.java @@ -34,9 +34,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro Schema schema = (Schema) foundObject; HibernateDatabase database = (HibernateDatabase) snapshot.getDatabase(); for (org.hibernate.boot.model.relational.Namespace namespace : database.getMetadata().getDatabase().getNamespaces()) { - boolean namespaceMatchesSchema = (namespace.getName().getSchema() != null && namespace.getName().getSchema().matches(foundObject.getName())) - || (namespace.getName().getSchema() == null && ((Schema) foundObject).isDefault()); - if (!namespaceMatchesSchema) { + if (!schemaMatchesNamespace(schema, namespace)) { continue; } diff --git a/src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java b/src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java index 326befd2..4d24b371 100644 --- a/src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java +++ b/src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java @@ -41,9 +41,7 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot return example; } - boolean schemaMatches = (example.getSchema().getName() != null && example.getSchema().getName().equalsIgnoreCase(hibernateTable.getSchema())) - || (example.getSchema().isDefault() && hibernateTable.getSchema() == null); - if (!schemaMatches) { + if (!schemaMatchesTable(example, hibernateTable)) { Scope.getCurrentScope().getLog(getClass()).info("Skipping table " + hibernateTable.getName() + " for schema " + example.getSchema().getName() + ", because it is part of another one."); return null; }