Skip to content

Commit

Permalink
Extract schema matching logic to HibernateSnapshotGenerator.
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-kleber-avelios committed Dec 18, 2024
1 parent ca205e4 commit 477bc66
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 477bc66

Please sign in to comment.