Collect the DB schemas in the model via MTable

This commit is contained in:
rob bygrave
2020-02-21 17:16:28 +13:00
parent e4390ed8a2
commit ea490a5ac3
4 changed files with 49 additions and 4 deletions
@@ -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
* <p>
* This effectively represents a table, its columns and all associated
* constraints, foreign keys and indexes.
* </p>
* <p>
* Migrations can be applied to this such that it represents the state
* of a given table after various migrations have been applied.
* </p>
* <p>
* This table model can also be derived from the EbeanServer bean descriptor
* and associated properties.
* </p>
*/
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.
*/
@@ -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<String> schemas = new TreeSet<>();
/**
* All the tables in the model.
*/
@@ -50,6 +54,13 @@ public class ModelContainer {
public ModelContainer() {
}
/**
* Return the schemas.
*/
public Set<String> 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);
}
@@ -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 {
@@ -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<String> 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"));