#556 - DDL - DB Migration for History - change treatment of dropped columns such that they are still included in apply script

This commit is contained in:
Robin Bygrave
2016-02-10 16:04:46 +13:00
parent d2b048b0b4
commit a1a5dcd035
21 changed files with 609 additions and 217 deletions
@@ -0,0 +1,46 @@
package com.avaje.ebean.dbmigration.ddlgeneration.platform;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.dbplatform.H2Platform;
import com.avaje.ebean.dbmigration.ddlgeneration.DdlWrite;
import com.avaje.ebean.dbmigration.model.CurrentModel;
import com.avaje.ebean.dbmigration.model.MConfiguration;
import com.avaje.ebean.dbmigration.model.ModelContainer;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class H2HistoryDdlTest {
@Test
public void testRegenerateHistoryTriggers() throws Exception {
SpiEbeanServer ebeanServer = (SpiEbeanServer)Ebean.getDefaultServer();
HistoryTableUpdate update = new HistoryTableUpdate("c_user");
update.add(HistoryTableUpdate.Change.ADD, "one");
update.add(HistoryTableUpdate.Change.DROP, "two");
CurrentModel currentModel = new CurrentModel(ebeanServer);
ModelContainer modelContainer = currentModel.read();
DdlWrite write = new DdlWrite(new MConfiguration(), modelContainer);
H2Platform h2Platform = new H2Platform();
PlatformDdl h2Ddl = h2Platform.getPlatformDdl();
h2Ddl.configure(ebeanServer.getServerConfig());
h2Ddl.regenerateHistoryTriggers(write, update);
assertThat(write.dropHistory().isEmpty()).isFalse();
assertThat(write.dropHistory().getBuffer()).contains("drop two");
assertThat(write.applyHistory().isEmpty()).isFalse();
assertThat(write.applyHistory().getBuffer()).contains("add one");
assertThat(write.applyHistory().getBuffer()).doesNotContain("two");
assertThat(write.rollback().isEmpty()).isFalse();
}
}
@@ -14,7 +14,11 @@ public class HistoryTableUpdateTest {
public void testToRevertedColumns_add() throws Exception {
HistoryTableUpdate upd = new HistoryTableUpdate("mytab");
assertThat(upd.getBaseTable()).isEqualTo("mytab");
upd.add(HistoryTableUpdate.Change.ADD, "two");
assertThat(upd.hasApplyChanges()).isTrue();
assertThat(upd.hasDropChanges()).isFalse();
List<String> current = current();
upd.toRevertedColumns(current);
@@ -26,6 +30,8 @@ public class HistoryTableUpdateTest {
HistoryTableUpdate upd = new HistoryTableUpdate("mytab");
upd.add(HistoryTableUpdate.Change.INCLUDE, "two");
assertThat(upd.hasApplyChanges()).isTrue();
assertThat(upd.hasDropChanges()).isFalse();
List<String> current = current();
upd.toRevertedColumns(current);
@@ -36,11 +42,13 @@ public class HistoryTableUpdateTest {
public void testToRevertedColumns_drop() throws Exception {
HistoryTableUpdate upd = new HistoryTableUpdate("mytab");
upd.add(HistoryTableUpdate.Change.DROP, "four");
upd.add(HistoryTableUpdate.Change.DROP, "three");
assertThat(upd.hasApplyChanges()).isFalse();
assertThat(upd.hasDropChanges()).isTrue();
List<String> current = current();
upd.toRevertedColumns(current);
assertThat(current).contains("one","two","three","four");
assertThat(current).contains("one","two","three");
}
@Test
@@ -48,6 +56,8 @@ public class HistoryTableUpdateTest {
HistoryTableUpdate upd = new HistoryTableUpdate("mytab");
upd.add(HistoryTableUpdate.Change.EXCLUDE, "four");
assertThat(upd.hasApplyChanges()).isTrue();
assertThat(upd.hasDropChanges()).isFalse();
List<String> current = current();
upd.toRevertedColumns(current);
@@ -61,8 +71,26 @@ public class HistoryTableUpdateTest {
HistoryTableUpdate upd = new HistoryTableUpdate("mytab");
upd.add(HistoryTableUpdate.Change.ADD, "two");
upd.add(HistoryTableUpdate.Change.DROP, "four");
assertThat(upd.hasApplyChanges()).isTrue();
assertThat(upd.hasDropChanges()).isTrue();
assertThat(upd.description()).isEqualTo("add two, drop four");
assertThat(upd.descriptionForApply()).isEqualTo("add two");
assertThat(upd.descriptionForDrop()).isEqualTo("drop four");
}
@Test
public void testDescription_withIncludeExclude() throws Exception {
HistoryTableUpdate upd = new HistoryTableUpdate("mytab");
upd.add(HistoryTableUpdate.Change.ADD, "two");
upd.add(HistoryTableUpdate.Change.INCLUDE, "five");
upd.add(HistoryTableUpdate.Change.EXCLUDE, "six");
upd.add(HistoryTableUpdate.Change.DROP, "four");
assertThat(upd.hasApplyChanges()).isTrue();
assertThat(upd.hasDropChanges()).isTrue();
assertThat(upd.descriptionForApply()).isEqualTo("add two, include five, exclude six");
assertThat(upd.descriptionForDrop()).isEqualTo("drop four");
}
List<String> current() {
@@ -1,8 +1,11 @@
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.DropColumn;
import com.avaje.ebean.dbmigration.migration.DropHistoryTable;
import com.avaje.ebean.dbmigration.migration.DropTable;
import org.junit.Test;
import java.util.List;
@@ -51,7 +54,26 @@ public class MTableTest {
}
@Test
public void testCompare_addColumnDropColumn() throws Exception {
public void test_allHistoryColumns() throws Exception {
MTable base = base();
base.registerDroppedColumn("fullName",2);
base.registerDroppedColumn("last",4);
assertThat(base.allHistoryColumns(false)).containsExactly("id","name","status");
assertThat(base.allHistoryColumns(true)).containsExactly("id","name","fullName","status","last");
}
@Test
public void test_dropTable() {
MTable base = base();
DropTable dropTable = base.dropTable();
assertThat(dropTable.getName()).isEqualTo(base.getName());
}
@Test
public void test_compare_addColumnDropColumn() throws Exception {
ModelDiff diff = new ModelDiff();
diff.compareTables(base(), newTable());
@@ -71,7 +93,7 @@ public class MTableTest {
}
@Test
public void testCompare_addTwoColumnsToSameTable() throws Exception {
public void test_compare_addTwoColumnsToSameTable() throws Exception {
ModelDiff diff = new ModelDiff();
diff.compareTables(base(), newTableAdd2Columns());
@@ -88,7 +110,7 @@ public class MTableTest {
}
@Test
public void testCompare_modifyColumn() throws Exception {
public void test_compare_modifyColumn() throws Exception {
ModelDiff diff = new ModelDiff();
diff.compareTables(base(), newTableModifiedColumn());
@@ -107,4 +129,99 @@ public class MTableTest {
assertThat(diff.getDropChanges()).hasSize(0);
}
@Test
public void test_apply_dropColumn() {
MTable base = base();
DropColumn dropColumn = new DropColumn();
dropColumn.setTableName("tab");
dropColumn.setColumnName("name");
base.apply(dropColumn);
assertThat(base.getColumn("name")).isNull();
}
@Test(expected = IllegalStateException.class)
public void test_apply_dropColumn_doesNotExist() {
MTable base = base();
DropColumn dropColumn = new DropColumn();
dropColumn.setTableName(base.getName());
dropColumn.setColumnName("DoesNotExist");
base.apply(dropColumn);
}
@Test(expected = IllegalStateException.class)
public void test_apply_alterColumn_doesNotExist() {
MTable base = base();
AlterColumn alterColumn = new AlterColumn();
alterColumn.setTableName(base.getName());
alterColumn.setColumnName("DoesNotExist");
alterColumn.setType("integer");
base.apply(alterColumn);
}
@Test
public void test_apply_alterColumn_type() {
MTable base = base();
AlterColumn alterColumn = new AlterColumn();
alterColumn.setTableName(base.getName());
alterColumn.setColumnName("id");
alterColumn.setType("uuid");
base.apply(alterColumn);
assertThat(base.getColumn("id").getType()).isEqualTo("uuid");
}
@Test
public void test_compare_addAndDropColumn() throws Exception {
MTable base = base();
MTable newTable = newTable();
ModelDiff diff = new ModelDiff();
base.compare(diff, newTable);
assertThat(diff.getApplyChanges()).hasSize(1);
assertThat(diff.getDropChanges()).hasSize(1);
}
@Test
public void test_compare_addHistoryToTable() {
MTable base = base();
MTable withHistory = base();
withHistory.setWithHistory(true);
ModelDiff diff = new ModelDiff();
base.compare(diff, withHistory);
assertThat(diff.getDropChanges()).isEmpty();
assertThat(diff.getApplyChanges()).hasSize(1);
assertThat(diff.getApplyChanges().get(0)).isInstanceOf(AddHistoryTable.class);
}
@Test
public void test_compare_removeHistoryFromTable() throws Exception {
MTable withHistory = base();
withHistory.setWithHistory(true);
MTable noHistory = base();
ModelDiff diff = new ModelDiff();
withHistory.compare(diff, noHistory);
assertThat(diff.getApplyChanges()).isEmpty();
assertThat(diff.getDropChanges()).hasSize(1);
assertThat(diff.getDropChanges().get(0)).isInstanceOf(DropHistoryTable.class);
}
}
@@ -36,6 +36,6 @@ public class ModelContainerApplyTest {
assertThat(foo.getTablespace()).isEqualTo("fooSpace");
assertThat(foo.getIndexTablespace()).isEqualTo("fooIndexSpace");
assertThat(foo.isWithHistory()).isEqualTo(false);
assertThat(foo.getColumns()).containsKeys("col1", "col3", "added_to_foo");
assertThat(foo.allColumns()).extracting("name").contains("col1", "col3", "added_to_foo");
}
}
@@ -37,8 +37,6 @@ public class ModelBuildBeanVisitorTest extends BaseTestCase {
assertThat(item.primaryKeyColumns()).hasSize(2);
MTable customer = model.getTable("o_customer");
assertThat(customer).isNotNull();
assertThat(customer.getSequenceName()).isNull();
}
}