DATACASS-678 - Consider custom name in CassandraAdminTemplate.createTable().

Original pull request: #163.
This commit is contained in:
Vagif Zeynalov
2019-07-29 11:08:28 -07:00
committed by Mark Paluch
parent ca1e57d0c3
commit fa5f026364
3 changed files with 37 additions and 2 deletions

View File

@@ -98,7 +98,7 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand
CassandraPersistentEntity<?> entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass);
CreateTableSpecification createTableSpecification = getConverter().getMappingContext()
.getCreateTableSpecificationFor(entity).ifNotExists(ifNotExists);
.getCreateTableSpecificationFor(tableName, entity).ifNotExists(ifNotExists);
getCqlOperations().execute(CreateTableCqlGenerator.toCql(createTableSpecification));
}

View File

@@ -430,12 +430,24 @@ public class CassandraMappingContext
* Returns a {@link CreateTableSpecification} for the given entity, including all mapping information.
*
* @param entity must not be {@literal null}.
* @return
*/
public CreateTableSpecification getCreateTableSpecificationFor(CassandraPersistentEntity<?> entity) {
return getCreateTableSpecificationFor(null, entity);
}
/**
* Returns a {@link CreateTableSpecification} for the given entity, including all mapping information.
*
* @param tableName if {@literal null} then the table name will be derived from entity
* @param entity must not be {@literal null}.
* @return
*/
public CreateTableSpecification getCreateTableSpecificationFor(CqlIdentifier tableName, CassandraPersistentEntity<?> entity) {
Assert.notNull(entity, "CassandraPersistentEntity must not be null");
CreateTableSpecification specification = createTable(entity.getTableName());
CreateTableSpecification specification = createTable(tableName != null ? tableName : entity.getTableName());
for (CassandraPersistentProperty property : entity) {

View File

@@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -333,6 +334,28 @@ public class CreateTableSpecificationBasicCassandraMappingContextUnitTests {
.isEqualTo("list<frozen<tuple<mappedudt, human_udt, text>>>");
}
@Test // DATACASS-678
public void createTableSpecificationShouldHaveCustomTableName() {
final CqlIdentifier customTableName = CqlIdentifier.of("employee_" + UUID.randomUUID().toString().replace("-","_"));
final CassandraPersistentEntity<?> persistentEntity = ctx.getRequiredPersistentEntity(Employee.class);
assertThat(persistentEntity).isNotNull();
final CreateTableSpecification regularSpecification = ctx.getCreateTableSpecificationFor(persistentEntity);
assertThat(regularSpecification).isNotNull();
assertThat(persistentEntity.getTableName()).isEqualTo(regularSpecification.getName());
assertThat(customTableName).isNotEqualTo(regularSpecification.getName());
final CreateTableSpecification customSpecification = ctx.getCreateTableSpecificationFor(customTableName, persistentEntity);
assertThat(customSpecification).isNotNull();
assertThat(customTableName).isEqualTo(customSpecification.getName());
assertThat(customTableName).isNotEqualTo(persistentEntity.getTableName());
}
private CreateTableSpecification getCreateTableSpecificationFor(Class<?> persistentEntityClass) {
CassandraCustomConversions customConversions = new CassandraCustomConversions(Collections.emptyList());