From ea490a5ac32ca72a2d5c2a953844fa881256e981 Mon Sep 17 00:00:00 2001 From: rob bygrave Date: Fri, 21 Feb 2020 17:16:28 +1300 Subject: [PATCH] Collect the DB schemas in the model via MTable --- .../ebeaninternal/dbmigration/model/MTable.java | 9 +++++---- .../dbmigration/model/ModelContainer.java | 15 +++++++++++++++ .../dbmigration/model/MTableTest.java | 12 ++++++++++++ .../dbmigration/model/ModelContainerTest.java | 17 +++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/ebeaninternal/dbmigration/model/MTable.java b/src/main/java/io/ebeaninternal/dbmigration/model/MTable.java index 4f04ba0c4..db6a803a4 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/model/MTable.java +++ b/src/main/java/io/ebeaninternal/dbmigration/model/MTable.java @@ -1,7 +1,6 @@ package io.ebeaninternal.dbmigration.model; import io.ebeaninternal.dbmigration.ddlgeneration.platform.DdlHelp; -import io.ebeaninternal.dbmigration.ddlgeneration.platform.SplitColumns; import io.ebeaninternal.dbmigration.migration.AddColumn; import io.ebeaninternal.dbmigration.migration.AddHistoryTable; import io.ebeaninternal.dbmigration.migration.AddTableComment; @@ -34,15 +33,12 @@ import static io.ebeaninternal.dbmigration.ddlgeneration.platform.SplitColumns.s *

* This effectively represents a table, its columns and all associated * constraints, foreign keys and indexes. - *

*

* Migrations can be applied to this such that it represents the state * of a given table after various migrations have been applied. - *

*

* This table model can also be derived from the EbeanServer bean descriptor * and associated properties. - *

*/ public class MTable { @@ -420,6 +416,11 @@ public class MTable { return name; } + public String getSchema() { + int pos = name.indexOf('.'); + return pos == -1 ? null : name.substring(0, pos); + } + /** * Return true if this table is a 'Draft' table. */ diff --git a/src/main/java/io/ebeaninternal/dbmigration/model/ModelContainer.java b/src/main/java/io/ebeaninternal/dbmigration/model/ModelContainer.java index 41b522a50..6a8e71463 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/model/ModelContainer.java +++ b/src/main/java/io/ebeaninternal/dbmigration/model/ModelContainer.java @@ -24,6 +24,8 @@ import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.TreeSet; /** * Holds all the tables, views, indexes etc that represent the model. @@ -33,6 +35,8 @@ import java.util.Map; */ public class ModelContainer { + private final Set schemas = new TreeSet<>(); + /** * All the tables in the model. */ @@ -50,6 +54,13 @@ public class ModelContainer { public ModelContainer() { } + /** + * Return the schemas. + */ + public Set getSchemas() { + return schemas; + } + /** * Return true if the model contains tables that are partitioned. */ @@ -310,6 +321,10 @@ public class ModelContainer { if (table.isPartitioned()) { partitionedTables.add(table); } + String schema = table.getSchema(); + if (schema != null) { + schemas.add(schema); + } return tables.put(table.getName(), table); } diff --git a/src/test/java/io/ebeaninternal/dbmigration/model/MTableTest.java b/src/test/java/io/ebeaninternal/dbmigration/model/MTableTest.java index 8d9b8a09b..290cff30a 100644 --- a/src/test/java/io/ebeaninternal/dbmigration/model/MTableTest.java +++ b/src/test/java/io/ebeaninternal/dbmigration/model/MTableTest.java @@ -11,6 +11,8 @@ import org.junit.Test; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; public class MTableTest { @@ -53,6 +55,16 @@ public class MTableTest { return table; } + @Test + public void schema() { + MTable table = new MTable("tab"); + assertNull(table.getSchema()); + + table = new MTable("foo.tab"); + assertEquals("foo", table.getSchema()); + assertEquals("foo.tab", table.getName()); + } + @Test public void test_allHistoryColumns() throws Exception { diff --git a/src/test/java/io/ebeaninternal/dbmigration/model/ModelContainerTest.java b/src/test/java/io/ebeaninternal/dbmigration/model/ModelContainerTest.java index 25a2644ee..368419a08 100644 --- a/src/test/java/io/ebeaninternal/dbmigration/model/ModelContainerTest.java +++ b/src/test/java/io/ebeaninternal/dbmigration/model/ModelContainerTest.java @@ -10,6 +10,7 @@ import io.ebeaninternal.dbmigration.migrationreader.MigrationXmlReader; import org.junit.Test; import java.util.List; +import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; @@ -153,6 +154,22 @@ public class ModelContainerTest { container.applyChange(dropHistoryTable); } + @Test + public void getSchemas() { + + MTable t0 = new MTable("foo.one"); + MTable t1 = new MTable("foo.two"); + MTable t2 = new MTable("bar.three"); + + ModelContainer container = new ModelContainer(); + container.addTable(t0); + container.addTable(t1); + container.addTable(t2); + + final Set schemas = container.getSchemas(); + assertThat(schemas).containsExactly("bar","foo"); + } + private ModelContainer container_2_1() { ModelContainer container = new ModelContainer(); container.apply(mig("2.0.model.xml"), ver("2.0"));