mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1920 Support @Index concurrently and definition
This commit is contained in:
@@ -3,17 +3,12 @@ package io.ebeaninternal.dbmigration;
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.SqlRow;
|
||||
import io.ebean.SqlUpdate;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.migration.ddl.DdlRunner;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.Helper;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -25,28 +20,8 @@ public class DbMigrationTest extends BaseTestCase {
|
||||
runScript(expectErrors, ddl, scriptName);
|
||||
}
|
||||
|
||||
private void runScript(boolean expectErrors, String content, String scriptName) {
|
||||
|
||||
DdlRunner runner = new DdlRunner(expectErrors, scriptName);
|
||||
|
||||
Transaction transaction = server().createTransaction();
|
||||
Connection connection = transaction.getConnection();
|
||||
try {
|
||||
if (expectErrors) {
|
||||
connection.setAutoCommit(true);
|
||||
}
|
||||
runner.runAll(content, connection);
|
||||
if (expectErrors) {
|
||||
connection.setAutoCommit(false);
|
||||
}
|
||||
transaction.commit();
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new PersistenceException("Failed to run script", e);
|
||||
|
||||
} finally {
|
||||
transaction.end();
|
||||
}
|
||||
private void runScript(boolean useAutoCommit, String content, String scriptName) {
|
||||
server().script().runScript(scriptName, content, useAutoCommit);
|
||||
}
|
||||
|
||||
@IgnorePlatform({Platform.ORACLE, Platform.NUODB})
|
||||
@@ -188,21 +163,29 @@ public class DbMigrationTest extends BaseTestCase {
|
||||
}
|
||||
|
||||
private void cleanup(String ... tables) {
|
||||
|
||||
final boolean sqlServer = isSqlServer();
|
||||
final boolean postgres = isPostgres();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String table : tables) {
|
||||
// simple and stupid try to execute all commands on all dialects.
|
||||
sb.append("alter table ").append(table).append(" set ( system_versioning = OFF );\n");
|
||||
sb.append("alter table ").append(table).append(" drop system versioning;\n");
|
||||
sb.append("drop table ").append(table).append(";\n");
|
||||
sb.append("drop table ").append(table).append(" cascade;\n");
|
||||
sb.append("drop table ").append(table).append("_history;\n");
|
||||
sb.append("drop table ").append(table).append("_history cascade;\n");
|
||||
if (sqlServer) {
|
||||
sb.append("alter table ").append(table).append(" set ( system_versioning = OFF );\n");
|
||||
sb.append("alter table ").append(table).append(" drop system versioning;\n");
|
||||
}
|
||||
if (postgres) {
|
||||
sb.append("drop table ").append(table).append(" cascade;\n");
|
||||
sb.append("drop table ").append(table).append("_history cascade;\n");
|
||||
} else {
|
||||
sb.append("drop table ").append(table).append(";\n");
|
||||
sb.append("drop table ").append(table).append("_history;\n");
|
||||
}
|
||||
sb.append("drop view ").append(table).append("_with_history;\n");
|
||||
sb.append("drop sequence ").append(table).append("_seq;\n");
|
||||
}
|
||||
|
||||
runScript(true, sb.toString(), "cleanup");
|
||||
runScript(true, sb.toString(), "cleanup");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class IndexDefinitionTest {
|
||||
|
||||
private static final String[] simpleCol1 = new String[]{"one"};
|
||||
private static final String[] simpleCol2 = new String[]{"one","two"};
|
||||
private static final String[] formulaCol1 = new String[]{"lower(one)"};
|
||||
private static final String[] formulaCol2 = new String[]{"one","lower(two)"};
|
||||
|
||||
@Test
|
||||
public void isUniqueConstraint_TRUE_when_simpleMultiColumn() {
|
||||
assertTrue(new IndexDefinition(simpleCol1).isUniqueConstraint());
|
||||
assertTrue(new IndexDefinition(simpleCol2).isUniqueConstraint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUniqueConstraint_NOT_when_columnWithFormula() {
|
||||
assertFalse(new IndexDefinition(formulaCol1).isUniqueConstraint());
|
||||
assertFalse(new IndexDefinition(formulaCol2).isUniqueConstraint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUniqueConstraint_NOT_when_concurrentTrue() {
|
||||
assertFalse(new IndexDefinition(simpleCol1, "name", true, null, true, null).isUniqueConstraint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUniqueConstraint_NOT_when_definitionNotEmpty() {
|
||||
assertFalse(new IndexDefinition(simpleCol1, "name", true, null, false, "create index foo").isUniqueConstraint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUniqueConstraint_TRUE_otherwise() {
|
||||
assertTrue(new IndexDefinition(simpleCol1, "name", true, null, false, "").isUniqueConstraint());
|
||||
assertTrue(new IndexDefinition(simpleCol1, "name", true, null, false, null).isUniqueConstraint());
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,9 @@ import javax.persistence.Table;
|
||||
|
||||
import static io.ebean.annotation.Platform.POSTGRES;
|
||||
|
||||
@Index(columnNames = "lower(name)", platforms = POSTGRES)
|
||||
@Index(platforms = POSTGRES, name = "idxd_migtest_0", definition = "create index idxd_migtest_0 on migtest_oto_child using hash (upper(name)) where upper(name) = 'JIM'")
|
||||
@Index(platforms = POSTGRES, columnNames = {"lower(name)","id"}, concurrent = true)
|
||||
@Index(platforms = POSTGRES, columnNames = "lower(name)")
|
||||
@Entity
|
||||
@Table(name = "migtest_oto_child")
|
||||
public class OtoChild {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.tests.model.basic;
|
||||
|
||||
import io.ebean.annotation.Index;
|
||||
import io.ebean.annotation.Platform;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
@@ -13,7 +14,8 @@ import javax.validation.constraints.Size;
|
||||
/**
|
||||
* A basic entity to test simple things.
|
||||
*/
|
||||
//@Index(name = "t_detail_foo_name", unique = true, columnNames = "lower(name)")
|
||||
@Index(name = "ix_t_detail_with_other_namexxxyy_lowername", unique = true, columnNames = "lower(name)", concurrent = true, platforms = Platform.POSTGRES)
|
||||
@Index(name = "ix_t_detail_with_other_namexxxyy_defn", platforms = Platform.POSTGRES, definition = "create index ix_t_detail_with_other_namexxxyy_defn on t_detail_with_other_namexxxyy using hash (lower(name)) where lower(name) like 'r%'")
|
||||
@Entity
|
||||
@Table(name = "t_detail_with_other_namexxxyy")
|
||||
public class TSDetail {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package org.tests.saveassociation;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.DB;
|
||||
import org.junit.Test;
|
||||
import org.tests.model.basic.TSDetail;
|
||||
import org.tests.model.basic.TSMaster;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestSaveSamePK extends BaseTestCase {
|
||||
|
||||
@@ -13,22 +14,22 @@ public class TestSaveSamePK extends BaseTestCase {
|
||||
public void test() {
|
||||
|
||||
// delete in case we are running multiple times without full db drop
|
||||
Ebean.delete(TSMaster.class, 10000);
|
||||
DB.delete(TSMaster.class, 10000);
|
||||
|
||||
TSMaster m0 = new TSMaster();
|
||||
m0.setId(10000);
|
||||
m0.setName("master1");
|
||||
|
||||
Ebean.save(m0);
|
||||
DB.save(m0);
|
||||
|
||||
TSDetail tsDetail = new TSDetail("master1 detail1");
|
||||
TSDetail tsDetail = new TSDetail("m4 d1");
|
||||
tsDetail.setId(10000);
|
||||
tsDetail.setMaster(m0);
|
||||
|
||||
Ebean.save(tsDetail);
|
||||
DB.save(tsDetail);
|
||||
|
||||
TSDetail fetchedDetail = Ebean.find(TSDetail.class).setId(10000).fetch("master").findOne();
|
||||
TSDetail fetchedDetail = DB.find(TSDetail.class).setId(10000).fetch("master").findOne();
|
||||
|
||||
Assert.assertNotNull(fetchedDetail);
|
||||
assertNotNull(fetchedDetail);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user