DATACASS-192 - Support RENAME and DROP in ALTER TABLE specification.

Extend alter table specification and the linked CQL generator to support RENAME and DROP operations. Cleanup code in that area.

Original pull request: #69.
This commit is contained in:
Mark Paluch
2016-06-21 15:52:10 +02:00
committed by John Blum
parent 38fef212e4
commit f1e3ef48e8
8 changed files with 329 additions and 25 deletions

View File

@@ -18,9 +18,15 @@ package org.springframework.cassandra.core.cql.generator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cassandra.core.keyspace.AlterTableSpecification;
import org.springframework.cassandra.core.keyspace.TableOption;
import org.springframework.cassandra.core.keyspace.TableOption.CachingOption;
import org.springframework.cassandra.core.keyspace.TableOption.KeyCachingOption;
import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest;
import com.datastax.driver.core.ColumnMetadata;
@@ -32,6 +38,7 @@ import com.datastax.driver.core.TableMetadata;
* Integration tests tests for {@link AlterTableCqlGenerator}.
*
* @author Mark Paluch
* @see DATACASS-192
*/
public class AlterTableCqlGeneratorIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest {
@@ -48,8 +55,8 @@ public class AlterTableCqlGeneratorIntegrationTests extends AbstractKeyspaceCrea
@Test
public void alterTableAlterColumnType() {
session.execute("CREATE TABLE addamsFamily (name varchar PRIMARY KEY, gender varchar,\n"
+ " lastknownlocation bigint);");
session.execute(
"CREATE TABLE addamsFamily (name varchar PRIMARY KEY, gender varchar,\n" + " lastknownlocation bigint);");
AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").alter("lastKnownLocation",
DataType.varint());
@@ -66,8 +73,8 @@ public class AlterTableCqlGeneratorIntegrationTests extends AbstractKeyspaceCrea
@Test
public void alterTableAlterListColumnType() {
session.execute("CREATE TABLE addamsFamily (name varchar PRIMARY KEY, gender varchar,\n"
+ " lastknownlocation list<ascii>);");
session.execute(
"CREATE TABLE addamsFamily (name varchar PRIMARY KEY, gender varchar,\n" + " lastknownlocation list<ascii>);");
AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").alter("lastKnownLocation",
DataType.list(DataType.varchar()));
@@ -84,8 +91,8 @@ public class AlterTableCqlGeneratorIntegrationTests extends AbstractKeyspaceCrea
@Test
public void alterTableAddColumn() {
session.execute("CREATE TABLE addamsFamily (name varchar PRIMARY KEY, gender varchar,\n"
+ " lastknownlocation varchar);");
session.execute(
"CREATE TABLE addamsFamily (name varchar PRIMARY KEY, gender varchar,\n" + " lastknownlocation varchar);");
AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").add("gravesite",
DataType.varchar());
@@ -113,6 +120,58 @@ public class AlterTableCqlGeneratorIntegrationTests extends AbstractKeyspaceCrea
assertThat(column.getType(), is(equalTo((DataType) DataType.list(DataType.ascii()))));
}
/**
* @see DATACASS-192
*/
@Test
public void alterTableDropColumn() {
session.execute("CREATE TABLE addamsFamily (name varchar PRIMARY KEY, gender varchar);");
AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").drop("gender");
execute(spec);
assertThat(getTableMetadata("addamsfamily").getColumn("gender"), is(nullValue()));
}
/**
* @see DATACASS-192
*/
@Test
public void alterTableRenameColumn() {
session.execute("CREATE TABLE addamsFamily (name varchar PRIMARY KEY, firstname varchar);");
AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").rename("name", "newname");
execute(spec);
assertThat(getTableMetadata("addamsfamily").getColumn("name"), is(nullValue()));
assertThat(getTableMetadata("addamsfamily").getColumn("newname"), is(notNullValue()));
}
/**
* @see DATACASS-192
*/
@Test
public void alterTableAddCaching() {
session.execute("CREATE TABLE users (user_name varchar PRIMARY KEY);");
Map<Object, Object> cachingMap = new LinkedHashMap<Object, Object>();
cachingMap.put(CachingOption.KEYS, KeyCachingOption.NONE);
cachingMap.put(CachingOption.ROWS_PER_PARTITION, "15");
AlterTableSpecification spec = AlterTableSpecification.alterTable("users").with(TableOption.CACHING, cachingMap);
execute(spec);
assertThat(getTableMetadata("users").getOptions().getCaching().get("keys"), is(equalTo("NONE")));
assertThat(getTableMetadata("users").getOptions().getCaching().get("rows_per_partition"), is(equalTo("15")));
}
private void execute(AlterTableSpecification spec) {
session.execute(new AlterTableCqlGenerator(spec).toCql());
}

View File

@@ -18,8 +18,14 @@ package org.springframework.cassandra.core.cql.generator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.cassandra.core.keyspace.AlterTableSpecification;
import org.springframework.cassandra.core.keyspace.TableOption;
import org.springframework.cassandra.core.keyspace.TableOption.CachingOption;
import org.springframework.cassandra.core.keyspace.TableOption.KeyCachingOption;
import com.datastax.driver.core.DataType;
@@ -80,6 +86,71 @@ public class AlterTableCqlGeneratorUnitTests {
assertThat(toCql(spec), is(equalTo("ALTER TABLE users ADD top_places list<ascii>;")));
}
/**
* @see DATACASS-192
*/
@Test
public void alterTableDropColumn() {
AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").drop("gender");
assertThat(toCql(spec), is(equalTo("ALTER TABLE addamsfamily DROP gender;")));
}
/**
* @see DATACASS-192
*/
@Test
public void alterTableRenameColumn() {
AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").rename("firstname", "lastname");
assertThat(toCql(spec), is(equalTo("ALTER TABLE addamsfamily RENAME firstname TO lastname;")));
}
/**
* @see DATACASS-192
*/
@Test
public void alterTableAddCommentAndTableOption() {
AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily")
.with(TableOption.READ_REPAIR_CHANCE, 0.2f).with(TableOption.COMMENT, "A most excellent and useful table");
assertThat(toCql(spec), is(equalTo(
"ALTER TABLE addamsfamily WITH read_repair_chance = 0.2 AND comment = 'A most excellent and useful table';")));
}
/**
* @see DATACASS-192
*/
@Test
public void alterTableAddColumnAndComment() {
AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily")
.add("top_places", DataType.list(DataType.ascii())).add("other", DataType.list(DataType.ascii()))
.with(TableOption.COMMENT, "A most excellent and useful table");
assertThat(toCql(spec), is(equalTo(
"ALTER TABLE addamsfamily ADD top_places list<ascii> ADD other list<ascii> WITH comment = 'A most excellent and useful table';")));
}
/**
* @see DATACASS-192
*/
@Test
public void alterTableAddCaching() {
Map<Object, Object> cachingMap = new LinkedHashMap<Object, Object>();
cachingMap.put(CachingOption.KEYS, KeyCachingOption.NONE);
cachingMap.put(CachingOption.ROWS_PER_PARTITION, "15");
AlterTableSpecification spec = AlterTableSpecification.alterTable("users").with(TableOption.CACHING, cachingMap);
assertThat(toCql(spec),
is(equalTo("ALTER TABLE users WITH caching = { 'keys' : 'none', 'rows_per_partition' : '15' };")));
}
private String toCql(AlterTableSpecification spec) {
return new AlterTableCqlGenerator(spec).toCql();
}