WIP: Db migration alterColumn ...

This commit is contained in:
Robin Bygrave
2015-08-10 14:26:47 +12:00
parent c0d4dac2b3
commit 845faf74ff
48 changed files with 6924 additions and 424 deletions
@@ -0,0 +1,23 @@
package com.avaje.ebean.config;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class DbMigrationConfigTest {
@Test
public void testLoad() {
ServerConfig config = new ServerConfig();
config.setName("h2other");
config.loadFromProperties();
DbMigrationConfig migrationConfig = config.getMigrationConfig();
assertThat(migrationConfig.getAppName()).isEqualTo("myapp");
assertThat(migrationConfig.getResourcePath()).isEqualTo("dbmigration/myapp");
}
}
@@ -0,0 +1,32 @@
package com.avaje.ebean.dbmigration;
import com.avaje.ebean.BaseTestCase;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.assertj.core.api.Assertions.*;
public class DbMigrationTest extends BaseTestCase {
private static final Logger logger = LoggerFactory.getLogger(DbMigrationTest.class);
//@Ignore
@Test
public void writeCurrent() {
logger.info("start");
DbOffline.asH2();
DbMigration migration = new DbMigration();
DbOffline.reset();
migration.writeCurrent();
assertThat(DbOffline.isSet()).isFalse();
logger.info("end");
}
}
@@ -12,7 +12,7 @@ public class MigrationXmlWriterTest {
Logger logger = LoggerFactory.getLogger(MigrationXmlWriterTest.class);
@Test
public void testWrite() throws Exception {
public void testReadWrite() throws Exception {
Migration migration = MigrationXmlReader.read("/container/test-create-table.xml");
@@ -0,0 +1,273 @@
package com.avaje.ebean.dbmigration.model;
import com.avaje.ebean.dbmigration.migration.AlterColumn;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class MColumnTest {
MTable table = new MTable("tab");
MColumn basic() {
return new MColumn("col", "integer");
}
ModelDiff diff() {
return new ModelDiff();
}
@Test
public void noDiff() throws Exception {
ModelDiff diff = diff();
basic().compare(diff, table, basic());
assertThat(diff.getCreateChanges()).isEmpty();
assertThat(diff.getDropChanges()).isEmpty();
}
@Test
public void diffType() throws Exception {
ModelDiff diff = diff();
basic().compare(diff, table, new MColumn("col", "integer(8)"));
assertChanges(diff);
AlterColumn alterColumn = getAlterColumn(diff);
assertThat(alterColumn.getType()).isEqualTo("integer(8)");
}
@Test
public void diffNotNull() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
newCol.setNotnull(true);
basic().compare(diff, table, newCol);
assertChanges(diff);
AlterColumn alterColumn = getAlterColumn(diff);
assertThat(alterColumn.isNotnull()).isEqualTo(true);
assertThat(alterColumn.getType()).isNull();
assertThat(alterColumn.isUnique()).isNull();
assertThat(alterColumn.isUniqueOneToOne()).isNull();
assertThat(alterColumn.getNewDefaultValue()).isNull();
}
@Test
public void diffCheckAdd() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
newCol.setCheckConstraint("abc");
basic().compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).getNewCheckConstraint()).isEqualTo("abc");
assertThat(getAlterColumn(diff).getOldCheckConstraint()).isNull();
}
@Test
public void diffCheckRemove() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
MColumn oldCol = basic();
oldCol.setCheckConstraint("abc");
oldCol.compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).getNewCheckConstraint()).isNull();
assertThat(getAlterColumn(diff).getOldCheckConstraint()).isEqualTo("abc");
}
@Test
public void diffCheckChange() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
newCol.setCheckConstraint("abc");
MColumn oldCol = basic();
oldCol.setCheckConstraint("d");
oldCol.compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).getNewCheckConstraint()).isEqualTo("abc");
assertThat(getAlterColumn(diff).getOldCheckConstraint()).isEqualTo("d");
}
@Test
public void diffDefaultValueAdd() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
newCol.setDefaultValue("abc");
basic().compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).getNewDefaultValue()).isEqualTo("abc");
assertThat(getAlterColumn(diff).getOldDefaultValue()).isNull();
}
@Test
public void diffDefaultValueRemove() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
MColumn oldCol = basic();
oldCol.setDefaultValue("abc");
oldCol.compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).getNewDefaultValue()).isNull();
assertThat(getAlterColumn(diff).getOldDefaultValue()).isEqualTo("abc");
}
@Test
public void diffDefaultValueChange() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
newCol.setDefaultValue("abc");
MColumn oldCol = basic();
oldCol.setDefaultValue("d");
oldCol.compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).getNewDefaultValue()).isEqualTo("abc");
assertThat(getAlterColumn(diff).getOldDefaultValue()).isEqualTo("d");
}
@Test
public void diffReferencesAdd() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
newCol.setReferences("abc");
basic().compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).getNewReferences()).isEqualTo("abc");
assertThat(getAlterColumn(diff).getOldReferences()).isNull();
}
@Test
public void diffReferencesRemove() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
MColumn oldCol = basic();
oldCol.setReferences("abc");
oldCol.compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).getNewReferences()).isNull();
assertThat(getAlterColumn(diff).getOldReferences()).isEqualTo("abc");
}
@Test
public void diffReferencesChange() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
newCol.setReferences("abc");
MColumn oldCol = basic();
oldCol.setReferences("d");
oldCol.compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).getNewReferences()).isEqualTo("abc");
assertThat(getAlterColumn(diff).getOldReferences()).isEqualTo("d");
}
@Test
public void diffUniqueAdd() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
newCol.setUnique(true);
basic().compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).isUnique()).isEqualTo(true);
}
@Test
public void diffUniqueRemove() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
MColumn oldCol = basic();
oldCol.setUnique(true);
oldCol.compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).isUnique()).isEqualTo(false);
}
@Test
public void diffUniqueOneToOneAdd() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
newCol.setUniqueOneToOne(true);
basic().compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).isUniqueOneToOne()).isEqualTo(true);
}
@Test
public void diffUniqueOneToOneRemove() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
MColumn oldCol = basic();
oldCol.setUniqueOneToOne(true);
oldCol.compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).isUniqueOneToOne()).isEqualTo(false);
}
@Test
public void diffHistoryExcludeAdd() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
newCol.setHistoryExclude(true);
basic().compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).isHistoryExclude()).isEqualTo(true);
}
@Test
public void diffHistoryExcludeRemove() throws Exception {
ModelDiff diff = diff();
MColumn newCol = basic();
MColumn oldCol = basic();
oldCol.setHistoryExclude(true);
oldCol.compare(diff, table, newCol);
assertChanges(diff);
assertThat(getAlterColumn(diff).isHistoryExclude()).isEqualTo(false);
}
@NotNull
private AlterColumn getAlterColumn(ModelDiff diff) {
return (AlterColumn) diff.getCreateChanges().get(0);
}
private void assertChanges(ModelDiff diff) {
assertThat(diff.getDropChanges()).isEmpty();
assertThat(diff.getCreateChanges()).hasSize(1);
}
}
@@ -0,0 +1,110 @@
package com.avaje.ebean.dbmigration.model;
import com.avaje.ebean.dbmigration.migration.AddColumn;
import com.avaje.ebean.dbmigration.migration.AlterColumn;
import com.avaje.ebean.dbmigration.migration.DropColumn;
import org.junit.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
public class MTableTest {
MTable base() {
MTable table = new MTable("tab");
table.addColumn(new MColumn("id","bigint"));
table.addColumn(new MColumn("name","varchar(20)"));
table.addColumn(new MColumn("status","varchar(3)"));
return table;
}
MTable newTable() {
MTable table = new MTable("tab");
table.addColumn(new MColumn("id","bigint"));
table.addColumn(new MColumn("name","varchar(20)"));
table.addColumn(new MColumn("comment","varchar(1000)"));
return table;
}
MTable newTableAdd2Columns() {
MTable table = new MTable("tab");
table.addColumn(new MColumn("id","bigint"));
table.addColumn(new MColumn("name","varchar(20)"));
table.addColumn(new MColumn("status","varchar(3)"));
table.addColumn(new MColumn("comment","varchar(1000)"));
table.addColumn(new MColumn("note","varchar(2000)"));
return table;
}
MTable newTableModifiedColumn() {
MColumn modCol = new MColumn("name", "varchar(30)");// modified type
modCol.setNotnull(true);
MTable table = new MTable("tab");
table.addColumn(modCol);
table.addColumn(new MColumn("id","bigint"));
table.addColumn(new MColumn("status","varchar(3)"));
return table;
}
@Test
public void testCompare_addColumnDropColumn() throws Exception {
ModelDiff diff = new ModelDiff();
diff.compareTables(base(), newTable());
List<Object> createChanges = diff.getCreateChanges();
assertThat(createChanges).hasSize(1);
AddColumn addColumn = (AddColumn)createChanges.get(0);
assertThat(addColumn.getColumn()).extracting("name").contains("comment");
assertThat(addColumn.getColumn()).extracting("type").contains("varchar(1000)");
List<Object> dropChanges = diff.getDropChanges();
assertThat(dropChanges).hasSize(1);
DropColumn dropColumn = (DropColumn)dropChanges.get(0);
assertThat(dropColumn.getColumnName()).isEqualTo("status");
assertThat(dropColumn.getTableName()).isEqualTo("tab");
}
@Test
public void testCompare_addTwoColumnsToSameTable() throws Exception {
ModelDiff diff = new ModelDiff();
diff.compareTables(base(), newTableAdd2Columns());
List<Object> createChanges = diff.getCreateChanges();
assertThat(createChanges).hasSize(1);
AddColumn addColumn = (AddColumn)createChanges.get(0);
assertThat(addColumn.getColumn()).extracting("name").contains("comment","note");
assertThat(addColumn.getColumn()).extracting("type").contains("varchar(1000)","varchar(2000)");
assertThat(diff.getDropChanges()).hasSize(0);
}
@Test
public void testCompare_modifyColumn() throws Exception {
ModelDiff diff = new ModelDiff();
diff.compareTables(base(), newTableModifiedColumn());
List<Object> createChanges = diff.getCreateChanges();
assertThat(createChanges).hasSize(1);
AlterColumn alterColumn = (AlterColumn)createChanges.get(0);
assertThat(alterColumn.getColumnName()).isEqualTo("name");
assertThat(alterColumn.getType()).isEqualTo("varchar(30)");
assertThat(alterColumn.isNotnull()).isEqualTo(true);
assertThat(alterColumn.isUnique()).isNull();
assertThat(alterColumn.getNewCheckConstraint()).isNull();
assertThat(alterColumn.getNewReferences()).isNull();
assertThat(diff.getDropChanges()).hasSize(0);
}
}
@@ -0,0 +1,39 @@
package com.avaje.ebean.dbmigration.model;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class MigrationModelTest {
@Test
public void testRead() throws Exception {
MigrationModel migrationModel = new MigrationModel("dbmigration/app1");
ModelContainer model = migrationModel.read();
assertThat(migrationModel.getReadVersions()).contains("1.0","1.1","2.0");
assertThat(model.getTable("v10_table")).isNotNull();
}
@Test
public void testRead_leadingSlash() throws Exception {
MigrationModel migrationModel = new MigrationModel("/dbmigration/app1");
ModelContainer model = migrationModel.read();
assertThat(migrationModel.getReadVersions()).contains("1.0","1.1","2.0");
assertThat(model.getTable("v10_table")).isNotNull();
}
@Test
public void testRead_trailingSlash() throws Exception {
MigrationModel migrationModel = new MigrationModel("/dbmigration/app1/");
ModelContainer model = migrationModel.read();
assertThat(migrationModel.getReadVersions()).contains("1.0","1.1","2.0");
assertThat(model.getTable("v10_table")).isNotNull();
}
}
@@ -32,7 +32,7 @@ public class ModelContainerApplyTest {
model.apply(migration);
MTable foo = model.getTable("foo");
assertThat(foo.getRemarks()).isEqualTo("comment");
assertThat(foo.getComment()).isEqualTo("comment");
assertThat(foo.getTablespace()).isEqualTo("fooSpace");
assertThat(foo.getIndexTablespace()).isEqualTo("fooIndexSpace");
assertThat(foo.getWithHistory()).isEqualTo(true);
@@ -0,0 +1,43 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.config.JsonConfig;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.junit.Test;
import java.sql.Timestamp;
import static org.junit.Assert.*;
public class ScalarTypeJodaLocalDateTimeTest {
ScalarTypeJodaLocalDateTime type = new ScalarTypeJodaLocalDateTime(JsonConfig.DateTime.ISO8601);
@Test
public void testConvertFromTimestamp() throws Exception {
long now = System.currentTimeMillis();
Timestamp nowTs = new Timestamp(now);
LocalDateTime ldt1 = type.convertFromTimestamp(nowTs);
LocalDateTime ldt2 = localConvertFromTimestamp(nowTs);
assertEquals(ldt1, ldt2);
Timestamp ts1 = type.convertToTimestamp(ldt1);
Timestamp ts2 = localConvertToTimestamp(ldt2);
assertEquals(ts1, ts2);
}
LocalDateTime localConvertFromTimestamp(Timestamp ts) {
return new LocalDateTime(ts.getTime(), DateTimeZone.getDefault());
}
Timestamp localConvertToTimestamp(LocalDateTime t) {
return new Timestamp(t.toDateTime(DateTimeZone.getDefault()).getMillis());
}
}
@@ -0,0 +1,32 @@
package com.avaje.ebeaninternal.server.type;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.junit.Test;
import java.sql.Timestamp;
import static org.junit.Assert.assertEquals;
public class ScalarTypeJodaLocalTimeTest {
@Test
public void test() {
long now = System.currentTimeMillis();
//DateTimeZone timeZone = DateTimeZone.getDefault();
//ISOChronology instance = ISOChronology.getInstance();
LocalDateTime ldt1 = new LocalDateTime(now, DateTimeZone.getDefault());
LocalDateTime ldt2 = new LocalDateTime(now);
assertEquals(ldt1, ldt2);
Timestamp ts1 = new Timestamp(ldt1.toDateTime(DateTimeZone.getDefault()).getMillis());
Timestamp ts2 = new Timestamp(ldt2.toDateTime().getMillis());
assertEquals(ts1, ts2);
}
}