mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
DbMigration update - constraints up front
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package com.avaje.ebean.config;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DbConstraintNamingTest {
|
||||
|
||||
DbConstraintNaming naming = new DbConstraintNaming();
|
||||
|
||||
@Test
|
||||
public void testPrimaryKeyName() throws Exception {
|
||||
|
||||
assertThat(naming.primaryKeyName("[cat].[sce].[foo_bar]")).isEqualTo("pk_foo_bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUniqueConstraintName() throws Exception {
|
||||
|
||||
assertThat(naming.uniqueConstraintName("[foo_bar]", "[jim]")).isEqualTo("uq_foo_bar_jim");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckConstraintName() throws Exception {
|
||||
|
||||
assertThat(naming.checkConstraintName("[foo_bar]", "[jim]")).isEqualTo("ck_foo_bar_jim");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalise() throws Exception {
|
||||
|
||||
assertThat(naming.normaliseTable("cat.sch.foo_bar]")).isEqualTo("foo_bar");
|
||||
assertThat(naming.normaliseTable("sch.foo_bar]")).isEqualTo("foo_bar");
|
||||
assertThat(naming.normaliseTable("foo_bar]")).isEqualTo("foo_bar");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.avaje.ebean.dbmigration.ddlgeneration;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.ebean.config.dbplatform.H2Platform;
|
||||
import com.avaje.ebean.config.dbplatform.PostgresPlatform;
|
||||
import com.avaje.ebean.dbmigration.migration.ChangeSet;
|
||||
@@ -15,13 +16,14 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BaseDdlHandlerTest extends BaseTestCase {
|
||||
|
||||
ServerConfig serverConfig = new ServerConfig();
|
||||
|
||||
private DdlHandler h2Handler() {
|
||||
return new H2Platform().createDdlHandler();
|
||||
return new H2Platform().createDdlHandler(serverConfig);
|
||||
}
|
||||
|
||||
private DdlHandler postgresHandler() {
|
||||
return new PostgresPlatform().createDdlHandler();
|
||||
return new PostgresPlatform().createDdlHandler(serverConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+8
-1
@@ -1,6 +1,7 @@
|
||||
package com.avaje.ebean.dbmigration.ddlgeneration.platform;
|
||||
|
||||
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.ebean.config.dbplatform.H2Platform;
|
||||
import com.avaje.ebean.dbmigration.ddlgeneration.DdlWrite;
|
||||
import com.avaje.ebean.dbmigration.ddlgeneration.Helper;
|
||||
@@ -15,10 +16,12 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BaseTableDdlTest {
|
||||
|
||||
ServerConfig serverConfig = new ServerConfig();
|
||||
|
||||
@Test
|
||||
public void testGenerate() throws Exception {
|
||||
|
||||
BaseTableDdl ddlGen = new BaseTableDdl(new DdlNamingConvention(), new H2Platform().getPlatformDdl());
|
||||
BaseTableDdl ddlGen = new BaseTableDdl(serverConfig, new H2Platform().getPlatformDdl());
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
@@ -38,6 +41,7 @@ public class BaseTableDdlTest {
|
||||
private CreateTable createTable() {
|
||||
CreateTable createTable = new CreateTable();
|
||||
createTable.setName("mytable");
|
||||
createTable.setPkName("pk_mytable");
|
||||
List<Column> columns = createTable.getColumn();
|
||||
Column col = new Column();
|
||||
col.setName("id");
|
||||
@@ -51,6 +55,7 @@ public class BaseTableDdlTest {
|
||||
col2.setType("varchar(1)");
|
||||
col2.setNotnull(true);
|
||||
col2.setCheckConstraint("check (status in ('A','B'))");
|
||||
col2.setCheckConstraintName("ck_mytable_status");
|
||||
|
||||
columns.add(col2);
|
||||
|
||||
@@ -59,6 +64,8 @@ public class BaseTableDdlTest {
|
||||
col3.setType("integer");
|
||||
col3.setNotnull(true);
|
||||
col3.setReferences("orders.id");
|
||||
col3.setForeignKeyName("fk_mytable_order_id");
|
||||
col3.setForeignKeyIndex("ix_mytable_order_id");
|
||||
|
||||
columns.add(col3);
|
||||
|
||||
|
||||
+2
-1
@@ -1,12 +1,13 @@
|
||||
package com.avaje.ebean.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import com.avaje.ebean.config.DbConstraintNormalise;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
public class DbNameNormaliseTest {
|
||||
|
||||
DdlNameNormalise normalise = new DdlNameNormalise();
|
||||
DbConstraintNormalise normalise = new DbConstraintNormalise();
|
||||
|
||||
@Test
|
||||
public void testNormalise() throws Exception {
|
||||
|
||||
-37
@@ -1,37 +0,0 @@
|
||||
package com.avaje.ebean.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DdlNamingConventionTest {
|
||||
|
||||
DdlNamingConvention defaultNaming = new DdlNamingConvention();
|
||||
|
||||
@Test
|
||||
public void testPrimaryKeyName() throws Exception {
|
||||
|
||||
assertThat(defaultNaming.primaryKeyName("[cat].[sce].[foo_bar]")).isEqualTo("pk_foo_bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUniqueConstraintName() throws Exception {
|
||||
|
||||
assertThat(defaultNaming.uniqueConstraintName("[foo_bar]", "[jim]", 1)).isEqualTo("uq_foo_bar_jim");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckConstraintName() throws Exception {
|
||||
|
||||
assertThat(defaultNaming.checkConstraintName("[foo_bar]", "[jim]", 1)).isEqualTo("ck_foo_bar_jim");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalise() throws Exception {
|
||||
|
||||
assertThat(defaultNaming.normaliseTable("cat.sch.foo_bar]")).isEqualTo("foo_bar");
|
||||
assertThat(defaultNaming.normaliseTable("sch.foo_bar]")).isEqualTo("foo_bar");
|
||||
assertThat(defaultNaming.normaliseTable("foo_bar]")).isEqualTo("foo_bar");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.avaje.ebean.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import com.avaje.ebean.dbmigration.ddlgeneration.platform.util.IndexSet;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
@@ -11,7 +12,7 @@ public class IndexSetTest {
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
BaseTableDdl.IndexSet set = new BaseTableDdl.IndexSet();
|
||||
IndexSet set = new IndexSet();
|
||||
assertTrue(set.add(new String[]{"one_column"}));
|
||||
assertTrue(set.add(new String[]{"two_column"}));
|
||||
assertFalse(set.add(new String[]{"one_column"}));
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.avaje.ebean.dbmigration.ddlgeneration.platform.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class VowelRemoverTest {
|
||||
|
||||
@Test
|
||||
public void testTrim() throws Exception {
|
||||
|
||||
|
||||
assertEquals("fk_abcd",VowelRemover.trim("fk_abcde",4));
|
||||
assertEquals("fk_a", VowelRemover.trim("fk_aaaaaa", 4));
|
||||
assertEquals("ab_avrylngtblnm", VowelRemover.trim("ab_averylongtablename", 4));
|
||||
}
|
||||
}
|
||||
@@ -53,9 +53,9 @@ public class MColumnTest {
|
||||
assertThat(alterColumn.isNotnull()).isEqualTo(true);
|
||||
|
||||
assertThat(alterColumn.getType()).isNull();
|
||||
assertThat(alterColumn.isUnique()).isNull();
|
||||
assertThat(alterColumn.isUniqueOneToOne()).isNull();
|
||||
assertThat(alterColumn.getNewDefaultValue()).isNull();
|
||||
assertThat(alterColumn.getUnique()).isNull();
|
||||
assertThat(alterColumn.getUniqueOneToOne()).isNull();
|
||||
assertThat(alterColumn.getDefaultValue()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -67,8 +67,8 @@ public class MColumnTest {
|
||||
basic().compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getNewCheckConstraint()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getOldCheckConstraint()).isNull();
|
||||
assertThat(getAlterColumn(diff).getCheckConstraint()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getDropCheckConstraint()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -77,12 +77,13 @@ public class MColumnTest {
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setCheckConstraint("abc");
|
||||
oldCol.setCheckConstraint("z");
|
||||
oldCol.setCheckConstraintName("abc");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getNewCheckConstraint()).isNull();
|
||||
assertThat(getAlterColumn(diff).getOldCheckConstraint()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getCheckConstraint()).isNull();
|
||||
assertThat(getAlterColumn(diff).getDropCheckConstraint()).isEqualTo("abc");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -92,12 +93,13 @@ public class MColumnTest {
|
||||
MColumn newCol = basic();
|
||||
newCol.setCheckConstraint("abc");
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setCheckConstraint("d");
|
||||
oldCol.setCheckConstraint("z");
|
||||
oldCol.setCheckConstraintName("d");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getNewCheckConstraint()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getOldCheckConstraint()).isEqualTo("d");
|
||||
assertThat(getAlterColumn(diff).getCheckConstraint()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getDropCheckConstraint()).isEqualTo("d");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -109,8 +111,7 @@ public class MColumnTest {
|
||||
basic().compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getNewDefaultValue()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getOldDefaultValue()).isNull();
|
||||
assertThat(getAlterColumn(diff).getDefaultValue()).isEqualTo("abc");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -123,8 +124,7 @@ public class MColumnTest {
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getNewDefaultValue()).isNull();
|
||||
assertThat(getAlterColumn(diff).getOldDefaultValue()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getDefaultValue()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -138,8 +138,7 @@ public class MColumnTest {
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getNewDefaultValue()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getOldDefaultValue()).isEqualTo("d");
|
||||
assertThat(getAlterColumn(diff).getDefaultValue()).isEqualTo("abc");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -151,8 +150,8 @@ public class MColumnTest {
|
||||
basic().compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getNewReferences()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getOldReferences()).isNull();
|
||||
assertThat(getAlterColumn(diff).getReferences()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getDropForeignKey()).isNull();
|
||||
}
|
||||
|
||||
|
||||
@@ -163,11 +162,17 @@ public class MColumnTest {
|
||||
MColumn newCol = basic();
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setReferences("abc");
|
||||
oldCol.setForeignKeyName("fk_ab");
|
||||
oldCol.setForeignKeyIndex("ix_ab");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getNewReferences()).isNull();
|
||||
assertThat(getAlterColumn(diff).getOldReferences()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getReferences()).isNull();
|
||||
assertThat(getAlterColumn(diff).getForeignKeyName()).isNull();
|
||||
assertThat(getAlterColumn(diff).getForeignKeyIndex()).isNull();
|
||||
|
||||
assertThat(getAlterColumn(diff).getDropForeignKey()).isEqualTo("fk_ab");
|
||||
assertThat(getAlterColumn(diff).getDropForeignKeyIndex()).isEqualTo("ix_ab");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -175,14 +180,24 @@ public class MColumnTest {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setReferences("abc");
|
||||
newCol.setReferences("ab");
|
||||
newCol.setForeignKeyName("fk_ab");
|
||||
newCol.setForeignKeyIndex("ix_ab");
|
||||
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setReferences("d");
|
||||
oldCol.setForeignKeyName("fk_d");
|
||||
oldCol.setForeignKeyIndex("ix_d");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getNewReferences()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getOldReferences()).isEqualTo("d");
|
||||
|
||||
assertThat(getAlterColumn(diff).getReferences()).isEqualTo("ab");
|
||||
assertThat(getAlterColumn(diff).getForeignKeyName()).isEqualTo("fk_ab");
|
||||
assertThat(getAlterColumn(diff).getForeignKeyIndex()).isEqualTo("ix_ab");
|
||||
|
||||
assertThat(getAlterColumn(diff).getDropForeignKey()).isEqualTo("fk_d");
|
||||
assertThat(getAlterColumn(diff).getDropForeignKeyIndex()).isEqualTo("ix_d");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -190,11 +205,11 @@ public class MColumnTest {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setUnique(true);
|
||||
newCol.setUnique("uq_one");
|
||||
basic().compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).isUnique()).isEqualTo(true);
|
||||
assertThat(getAlterColumn(diff).getUnique()).isEqualTo("uq_one");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -203,11 +218,11 @@ public class MColumnTest {
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setUnique(true);
|
||||
oldCol.setUnique("uq_one");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).isUnique()).isEqualTo(false);
|
||||
assertThat(getAlterColumn(diff).getDropUnique()).isEqualTo("uq_one");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -215,11 +230,14 @@ public class MColumnTest {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setUniqueOneToOne(true);
|
||||
basic().compare(diff, table, newCol);
|
||||
newCol.setUniqueOneToOne("uq_new");
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setUniqueOneToOne("uq_old");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).isUniqueOneToOne()).isEqualTo(true);
|
||||
assertThat(getAlterColumn(diff).getUniqueOneToOne()).isEqualTo("uq_new");
|
||||
assertThat(getAlterColumn(diff).getDropUnique()).isEqualTo("uq_old");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -228,11 +246,11 @@ public class MColumnTest {
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setUniqueOneToOne(true);
|
||||
oldCol.setUniqueOneToOne("uq_new");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).isUniqueOneToOne()).isEqualTo(false);
|
||||
assertThat(getAlterColumn(diff).getDropUnique()).isEqualTo("uq_new");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -100,9 +100,9 @@ public class MTableTest {
|
||||
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(alterColumn.getUnique()).isNull();
|
||||
assertThat(alterColumn.getCheckConstraint()).isNull();
|
||||
assertThat(alterColumn.getReferences()).isNull();
|
||||
|
||||
assertThat(diff.getDropChanges()).hasSize(0);
|
||||
|
||||
|
||||
+6
-1
@@ -3,6 +3,8 @@ package com.avaje.ebean.dbmigration.model.build;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.config.DbConstraintNaming;
|
||||
import com.avaje.ebean.dbmigration.ddlgeneration.platform.PlatformDdl;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebean.dbmigration.model.MTable;
|
||||
import com.avaje.ebean.dbmigration.model.ModelContainer;
|
||||
@@ -20,7 +22,10 @@ public class ModelBuildBeanVisitorTest extends BaseTestCase {
|
||||
|
||||
ModelContainer model = new ModelContainer();
|
||||
|
||||
ModelBuildContext ctx = new ModelBuildContext(model);
|
||||
PlatformDdl platformDdl = defaultServer.getDatabasePlatform().getPlatformDdl();
|
||||
DbConstraintNaming constraintNaming = defaultServer.getServerConfig().getConstraintNaming();
|
||||
ModelBuildContext ctx = new ModelBuildContext(model, constraintNaming, platformDdl);
|
||||
|
||||
ModelBuildBeanVisitor addTable = new ModelBuildBeanVisitor(ctx);
|
||||
|
||||
new VisitAllUsing(addTable, defaultServer).visitAllBeans();
|
||||
|
||||
@@ -45,9 +45,6 @@ public class TestQueryFindPagedList extends BaseTestCase {
|
||||
|
||||
PagedList<Order> pagedList = Ebean.find(Order.class).findPagedList(0, 3);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
Thread.sleep(1); // give slf4j a little time in this multithreaded test case
|
||||
|
||||
Future<Integer> rowCount = pagedList.getFutureRowCount();
|
||||
List<Order> orders = pagedList.getList();
|
||||
|
||||
@@ -56,17 +53,9 @@ public class TestQueryFindPagedList extends BaseTestCase {
|
||||
Integer totalRowCountWithTimeout = rowCount.get(30, TimeUnit.SECONDS);
|
||||
Integer totalRowCountViaFuture = rowCount.get();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
|
||||
assertTrue(orders.size() < totalRowCount);
|
||||
assertEquals(Integer.valueOf(totalRowCount), totalRowCountViaFuture);
|
||||
assertEquals(Integer.valueOf(totalRowCount), totalRowCountWithTimeout);
|
||||
assertEquals(2, loggedSql.size());
|
||||
|
||||
String firstTxn = loggedSql.get(0).substring(0, 10);
|
||||
String secTxn = loggedSql.get(1).substring(0, 10);
|
||||
|
||||
assertNotEquals(firstTxn, secTxn);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user