Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* @author Matthew T. Adams
* @author Mark Paluch
* @author Fabio J. Mendes
* @author Mikhail Polivakha
*/
public interface CassandraAdminOperations extends CassandraOperations {

Expand All @@ -55,6 +56,20 @@ public interface CassandraAdminOperations extends CassandraOperations {
void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass,
Map<String, Object> optionsByName);

/**
* Create a table with the name, that is derived from the given {@code entityClass} and also fields corresponding to the
* same class. If the table already exists and parameter {@code ifNotExists} is {@literal true}, this is a no-op and
* {@literal false} is returned. If the table doesn't exist, parameter {@code ifNotExists} is ignored, the table is created
* and {@literal true} is returned.
*
* @param ifNotExists If true, will only create the table if it doesn't exist, else the create operation will be
* ignored.
* @param entityClass The class whose fields determine the columns created.
* @param optionsByName Table options, given by the string option name and the appropriate option value.
*/
void createTable(boolean ifNotExists, Class<?> entityClass, Map<String, Object> optionsByName);


/**
* Drops a table based on the given {@link Class entity type}. The name of the table is derived from either the simple
* name of the {@link Class entity class} or name of the table specified with the {@link Table} mapping annotation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ public SchemaFactory getSchemaFactory() {
}

@Override
public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass, Map<String, Object> optionsByName) {
public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass,
Map<String, Object> optionsByName) {
CassandraPersistentEntity<?> entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass);

CreateTableSpecification createTableSpecification = this.schemaFactory
.getCreateTableSpecificationFor(entity, tableName)
.ifNotExists(ifNotExists);
.getCreateTableSpecificationFor(entity, tableName)
.ifNotExists(ifNotExists);

if (!CollectionUtils.isEmpty(optionsByName)) {
optionsByName.forEach((key, value) -> {
Expand All @@ -129,6 +130,12 @@ public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> e
getCqlOperations().execute(CreateTableCqlGenerator.toCql(createTableSpecification));
}

@Override
public void createTable(boolean ifNotExists, Class<?> entityClass, Map<String, Object> optionsByName) {
CassandraPersistentEntity<?> entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass);
this.createTable(ifNotExists, entity.getTableName(), entityClass, optionsByName);
}

@Override
public void dropTable(Class<?> entityClass) {
dropTable(getTableName(entityClass));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Map;

import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

/**
* Enumeration that represents all known table options. If a table option is not listed here, but is supported by
Expand Down Expand Up @@ -94,13 +95,11 @@ public enum TableOption implements Option {
* @since 4.1.1
*/
public static TableOption valueOfIgnoreCase(String optionName) {

for (TableOption value : values()) {
if (value.getName().equalsIgnoreCase(optionName)) {
return value;
}
}

throw new IllegalArgumentException(String.format("Unable to recognize specified Table option '%s'", optionName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collection;
import java.util.Map;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.annotation.Id;
Expand Down Expand Up @@ -83,28 +84,49 @@ void shouldApplyTableOptions() {
.isEqualTo(0.3);
}

@Test
void shouldCreateTableWithNameDerivedFromEntityClass() {
cassandraAdminTemplate.createTable(
true,
SomeTable.class,
Map.of(
TableOption.COMMENT.getName(), "This is comment for table", TableOption.BLOOM_FILTER_FP_CHANCE.getName(), "0.3"
)
);

TableMetadata someTable = getKeyspaceMetadata().getTables()
.values()
.stream()
.findFirst()
.orElse(null);

Assertions.assertThat(someTable).isNotNull();
Assertions.assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.COMMENT.getName()))).isEqualTo("This is comment for table");
Assertions.assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.BLOOM_FILTER_FP_CHANCE.getName()))).isEqualTo(3);
}

@Test // DATACASS-173
void testCreateTables() {

assertThat(getKeyspaceMetadata().getTables()).hasSize(0);

cassandraAdminTemplate.createTable(true, CqlIdentifier.fromCql("users"), User.class, null);
cassandraAdminTemplate.createTable(true, User.class, null);
assertThat(getKeyspaceMetadata().getTables()).hasSize(1);

cassandraAdminTemplate.createTable(true, CqlIdentifier.fromCql("users"), User.class, null);
cassandraAdminTemplate.createTable(true, User.class, null);
assertThat(getKeyspaceMetadata().getTables()).hasSize(1);
}

@Test
void testDropTable() {

cassandraAdminTemplate.createTable(true, CqlIdentifier.fromCql("users"), User.class, null);
cassandraAdminTemplate.createTable(true, User.class, null);
assertThat(getKeyspaceMetadata().getTables()).hasSize(1);

cassandraAdminTemplate.dropTable(User.class);
assertThat(getKeyspaceMetadata().getTables()).hasSize(0);

cassandraAdminTemplate.createTable(true, CqlIdentifier.fromCql("users"), User.class, null);
cassandraAdminTemplate.createTable(true, User.class, null);
cassandraAdminTemplate.dropTable(CqlIdentifier.fromCql("users"));

assertThat(getKeyspaceMetadata().getTables()).hasSize(0);
Expand Down