Skip to content

Commit 6d8764b

Browse files
Extract schema matching logic to HibernateSnapshotGenerator.
1 parent 4189c56 commit 6d8764b

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/main/java/liquibase/ext/hibernate/snapshot/HibernateSnapshotGenerator.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
import liquibase.database.Database;
44
import liquibase.exception.DatabaseException;
55
import liquibase.ext.hibernate.database.HibernateDatabase;
6-
import liquibase.logging.LogFactory;
7-
import liquibase.logging.LogService;
8-
import liquibase.logging.Logger;
96
import liquibase.snapshot.DatabaseSnapshot;
107
import liquibase.snapshot.InvalidExampleException;
118
import liquibase.snapshot.SnapshotGenerator;
129
import liquibase.snapshot.SnapshotGeneratorChain;
1310
import liquibase.structure.DatabaseObject;
11+
import liquibase.structure.core.Schema;
12+
import org.hibernate.boot.model.relational.Namespace;
1413
import org.hibernate.boot.spi.MetadataImplementor;
1514
import org.hibernate.mapping.Table;
1615

@@ -107,4 +106,27 @@ protected org.hibernate.mapping.Table findHibernateTable(DatabaseObject example,
107106
}
108107
return null;
109108
}
109+
110+
protected static boolean schemaMatchesTable(
111+
DatabaseObject example,
112+
Table hibernateTable
113+
) {
114+
if (example.getSchema().getName() != null && example.getSchema().getName().equalsIgnoreCase(hibernateTable.getSchema())) {
115+
return true;
116+
}
117+
118+
return example.getSchema().isDefault() && hibernateTable.getSchema() == null;
119+
}
120+
121+
protected static boolean schemaMatchesNamespace(
122+
Schema schema,
123+
Namespace hibernateNamespace
124+
) {
125+
if (hibernateNamespace.getName().getSchema() != null && hibernateNamespace.getName().getSchema().matches(schema.getName())) {
126+
return true;
127+
}
128+
129+
return hibernateNamespace.getName().getSchema() == null && schema.isDefault();
130+
}
131+
110132
}

src/main/java/liquibase/ext/hibernate/snapshot/SequenceSnapshotGenerator.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
3434
Schema schema = (Schema) foundObject;
3535
HibernateDatabase database = (HibernateDatabase) snapshot.getDatabase();
3636
for (org.hibernate.boot.model.relational.Namespace namespace : database.getMetadata().getDatabase().getNamespaces()) {
37-
boolean namespaceMatchesSchema = (namespace.getName().getSchema() != null && namespace.getName().getSchema().matches(foundObject.getName()))
38-
|| (namespace.getName().getSchema() == null && ((Schema) foundObject).isDefault());
39-
if (!namespaceMatchesSchema) {
37+
if (!schemaMatchesNamespace(schema, namespace)) {
4038
continue;
4139
}
4240

src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot
4141
return example;
4242
}
4343

44-
boolean schemaMatches = (example.getSchema().getName() != null && example.getSchema().getName().equalsIgnoreCase(hibernateTable.getSchema()))
45-
|| (example.getSchema().isDefault() && hibernateTable.getSchema() == null);
46-
if (!schemaMatches) {
44+
if (!schemaMatchesTable(example, hibernateTable)) {
4745
Scope.getCurrentScope().getLog(getClass()).info("Skipping table " + hibernateTable.getName() + " for schema " + example.getSchema().getName() + ", because it is part of another one.");
4846
return null;
4947
}

0 commit comments

Comments
 (0)