com.avaje.ebean.dbmigration.model.ModelContainer Maven / Gradle / Ivy
package com.avaje.ebean.dbmigration.model;
import com.avaje.ebean.dbmigration.migration.AddColumn;
import com.avaje.ebean.dbmigration.migration.AddHistoryTable;
import com.avaje.ebean.dbmigration.migration.AlterColumn;
import com.avaje.ebean.dbmigration.migration.ChangeSet;
import com.avaje.ebean.dbmigration.migration.ChangeSetType;
import com.avaje.ebean.dbmigration.migration.CreateIndex;
import com.avaje.ebean.dbmigration.migration.CreateTable;
import com.avaje.ebean.dbmigration.migration.DropColumn;
import com.avaje.ebean.dbmigration.migration.DropHistoryTable;
import com.avaje.ebean.dbmigration.migration.DropIndex;
import com.avaje.ebean.dbmigration.migration.DropTable;
import com.avaje.ebean.dbmigration.migration.Migration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Holds all the tables, views, indexes etc that represent the model.
*
* Migration changeSets can be applied to the model.
*
*/
public class ModelContainer {
/**
* All the tables in the model.
*/
private final Map tables = new LinkedHashMap();
/**
* All the non unique non foreign key indexes.
*/
private final Map indexes = new LinkedHashMap();
private final PendingDrops pendingDrops = new PendingDrops();
public ModelContainer() {
}
/**
* Adjust the FK references on all the draft tables.
*/
public void adjustDraftReferences() {
for (MTable table : this.tables.values()) {
if (table.isDraft()) {
table.adjustReferences(this);
}
}
}
/**
* Return the map of all the tables.
*/
public Map getTables() {
return tables;
}
/**
* Return the map of all the non unique non fk indexes.
*/
public Map getIndexes() {
return indexes;
}
/**
* Return the table by name.
*/
public MTable getTable(String tableName) {
return tables.get(tableName);
}
/**
* Return the index by name.
*/
public MIndex getIndex(String indexName) {
return indexes.get(indexName);
}
/**
* Apply a migration with associated changeSets to the model.
*/
public void apply(Migration migration, MigrationVersion version) {
List changeSets = migration.getChangeSet();
for (ChangeSet changeSet : changeSets) {
boolean pending = changeSet.getType() == ChangeSetType.PENDING_DROPS;
if (pending) {
// un-applied drop columns etc
pendingDrops.add(version, changeSet);
} else if (isDropsFor(changeSet)) {
pendingDrops.appliedDropsFor(changeSet);
}
if (!isDropsFor(changeSet)) {
applyChangeSet(changeSet);
}
}
}
/**
* Return true if the changeSet contains drops for a previous PENDING_DROPS changeSet.
*/
private boolean isDropsFor(ChangeSet changeSet) {
return changeSet.getDropsFor() != null;
}
/**
* Apply a changeSet to the model.
*/
protected void applyChangeSet(ChangeSet changeSet) {
List