Skip to content

Commit

Permalink
DATACASS-594 code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mipo256 committed Mar 5, 2024
1 parent 7d1d9e4 commit 3da8a0b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.springframework.data.cassandra.config;
package org.springframework.data.cassandra.core.convert;

import java.util.Collection;
import java.util.LinkedList;
Expand All @@ -12,6 +12,7 @@
import org.springframework.data.cassandra.CassandraKeyspaceDoesNotExistsException;
import org.springframework.data.cassandra.CassandraNoActiveKeyspaceSetForCqlSessionException;
import org.springframework.data.cassandra.CassandraSchemaValidationException;
import org.springframework.data.cassandra.config.CassandraSchemaValidationProfile;
import org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
Expand All @@ -36,20 +37,28 @@ public class CassandraSchemaValidator implements SmartInitializingSingleton {

private static final Log logger = LogFactory.getLog(CassandraSchemaValidator.class);

private final CqlSessionFactoryBean cqlSessionFactoryBean;
private final CqlSession cqlSession;

private final CassandraMappingContext cassandraMappingContext;

private final ColumnTypeResolver columnTypeResolver;

private final boolean strictValidation;

public CassandraSchemaValidator(
CqlSessionFactoryBean cqlSessionFactoryBean,
CassandraMappingContext cassandraMappingContext,
CqlSession cqlSession,
CassandraConverter cassandraConverter,
boolean strictValidation
) {
this.strictValidation = strictValidation;
this.cqlSessionFactoryBean = cqlSessionFactoryBean;
this.cassandraMappingContext = cassandraMappingContext;
this.cqlSession = cqlSession;
this.cassandraMappingContext = cassandraConverter.getMappingContext();
this.columnTypeResolver = new DefaultColumnTypeResolver(
cassandraMappingContext,
SchemaFactory.ShallowUserTypeResolver.INSTANCE,
cassandraConverter::getCodecRegistry,
cassandraConverter::getCustomConversions
);
}

/**
Expand All @@ -60,13 +69,11 @@ public CassandraSchemaValidator(
*/
@Override
public void afterSingletonsInstantiated() {
CqlSession session = cqlSessionFactoryBean.getSession();

CqlIdentifier activeKeyspace = session
CqlIdentifier activeKeyspace = cqlSession
.getKeyspace()
.orElseThrow(CassandraNoActiveKeyspaceSetForCqlSessionException::new);

KeyspaceMetadata keyspaceMetadata = session
KeyspaceMetadata keyspaceMetadata = cqlSession
.getMetadata()
.getKeyspace(activeKeyspace)
.orElseThrow(() -> new CassandraKeyspaceDoesNotExistsException(activeKeyspace.asInternal()));
Expand Down Expand Up @@ -131,6 +138,11 @@ private List<String> validateProperties(TableMetadata tableMetadata, BasicCassan
List<String> validationErrors = new LinkedList<>();

entity.doWithProperties((PropertyHandler<CassandraPersistentProperty>) persistentProperty -> {

if (persistentProperty.isTransient()) {
return;
}

CqlIdentifier expectedColumnName = persistentProperty.getColumnName();

Assert.notNull(expectedColumnName, "Column cannot not be null at this point");
Expand All @@ -139,7 +151,7 @@ private List<String> validateProperties(TableMetadata tableMetadata, BasicCassan

if (column.isPresent()) {
ColumnMetadata columnMetadata = column.get();
DataType dataTypeExpected = CassandraSimpleTypeHolder.getDataTypeFor(persistentProperty.getRawType());
DataType dataTypeExpected = columnTypeResolver.resolve(persistentProperty).getDataType();

if (dataTypeExpected == null) {
validationErrors.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.data.annotation.Id;
import org.springframework.data.cassandra.CassandraSchemaValidationException;
import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.convert.CassandraSchemaValidator;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.Table;
Expand All @@ -36,12 +38,12 @@ static class Config extends IntegrationTestConfig {
@Bean("cassandraSchemaValidator")
CassandraSchemaValidator cassandraSchemaValidator(
CqlSessionFactoryBean cqlSessionFactoryBean,
CassandraMappingContext cassandraMappingContext,
CassandraConverter cassandraConverter,
@Value("${validation.mode.strict:true}") Boolean validationModeStrict
) {
return new CassandraSchemaValidator(
cqlSessionFactoryBean,
cassandraMappingContext,
cqlSessionFactoryBean.getSession(),
cassandraConverter,
validationModeStrict
);
}
Expand Down

0 comments on commit 3da8a0b

Please sign in to comment.