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

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 @@ -43,14 +43,15 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand
/**
* Constructor used for a basic template configuration
*
* @param keyspace must not be {@literal null}.
* @param session must not be {@literal null}.
* @param converter must not be {@literal null}.
*/
public CassandraAdminTemplate(Session session, CassandraConverter converter) {
super(session, converter);
}

@Override
public void createTable(boolean ifNotExists, final CqlIdentifier tableName, Class<?> entityClass,
public void createTable(final boolean ifNotExists, final CqlIdentifier tableName, Class<?> entityClass,
Map<String, Object> optionsByName) {

final CassandraPersistentEntity<?> entity = getCassandraMappingContext().getPersistentEntity(entityClass);
Expand All @@ -59,8 +60,8 @@ public void createTable(boolean ifNotExists, final CqlIdentifier tableName, Clas
@Override
public Object doInSession(Session s) throws DataAccessException {

String cql = new CreateTableCqlGenerator(getCassandraMappingContext().getCreateTableSpecificationFor(entity))
.toCql();
String cql = new CreateTableCqlGenerator(getCassandraMappingContext().
getCreateTableSpecificationFor(entity).ifNotExists(ifNotExists)).toCql();

log.debug(cql);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2013-2014 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.template;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;

import java.util.Collection;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cassandra.core.cql.CqlIdentifier;
import org.springframework.cassandra.core.keyspace.DropTableSpecification;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.core.CassandraAdminTemplate;
import org.springframework.data.cassandra.test.integration.simpletons.Book;
import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest;
import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.datastax.driver.core.KeyspaceMetadata;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.TableMetadata;

/**
* Test for CassandraAdminTemplate
*
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class CassandraAdminTemplateTest extends AbstractSpringDataEmbeddedCassandraIntegrationTest {

@Configuration
public static class Config extends IntegrationTestConfig {

@Override
public String[] getEntityBasePackages() {
return new String[] { Book.class.getPackage().getName() };
}
}

@Autowired private CassandraAdminTemplate cassandraAdminTemplate;
@Autowired private CassandraConverter converter;

@Before
public void before() {
KeyspaceMetadata keyspace = getKeyspaceMetadata();
Collection<TableMetadata> tables = keyspace.getTables();
for (TableMetadata table : tables) {
cassandraAdminTemplate.execute(DropTableSpecification.dropTable(table.getName()));
}
}

private KeyspaceMetadata getKeyspaceMetadata() {
Metadata metadata = getSession().getCluster().getMetadata();
return metadata.getKeyspace(getSession().getLoggedKeyspace());
}

private Session getSession() {
return template.getSession();
}

/**
*
* @see DATACASS-173
*/
@Test
public void testCreateTables() throws Exception {

assertThat(getKeyspaceMetadata().getTables().size(), is(0));

cassandraAdminTemplate.createTable(true, CqlIdentifier.cqlId("book"), Book.class, null);
assertThat(getKeyspaceMetadata().getTables().size(), is(1));

cassandraAdminTemplate.createTable(true, CqlIdentifier.cqlId("book"), Book.class, null);
assertThat(getKeyspaceMetadata().getTables().size(), is(1));
}
}