/** * Start building a new ALTER TABLE statement. * * @param tableName the name of the table to be altered. * @return an in-construction ALTER TABLE statement. */ public static Alter alterTable(String tableName) { return new Alter(tableName); }
/** * Define the new type of the altered column. * * @param type the new type of the altered column. * @return the final statement. */ public SchemaStatement type(DataType type) { return SchemaStatement.fromQueryString( alter.buildInternal() + " ALTER " + columnName + " TYPE " + type.toString()); }
@Test(groups = "unit") public void should_add_column() throws Exception { // When SchemaStatement statement = alterTable("test").addColumn("location").type(DataType.ascii()); // Then assertThat(statement.getQueryString()).isEqualTo("\n\tALTER TABLE test ADD location ascii"); }
@Test(groups = "unit") public void should_alter_column_type_to_UDT() throws Exception { // When SchemaStatement statement = alterTable("ks", "test").alterColumn("address").udtType(frozen("address")); // Then assertThat(statement.getQueryString()) .isEqualTo("\n\tALTER TABLE ks.test ALTER address TYPE frozen<address>"); }
@Test(groups = "short") public void should_add_and_drop_a_column() { // Create a table, add a column to it with an alter table statement and delete that column session() .execute( SchemaBuilder.createTable("ks", "DropColumn") .ifNotExists() .addPartitionKey("a", DataType.cint())); // Add and then drop a column session() .execute(SchemaBuilder.alterTable("ks", "DropColumn").addColumn("b").type(DataType.cint())); session().execute(SchemaBuilder.alterTable("ks", "DropColumn").dropColumn("b")); // Check that only column a exist ResultSet rows = session() .execute( "SELECT column_name, type, validator " + "FROM system.schema_columns " + "WHERE keyspace_name='ks' AND columnfamily_name='dropcolumn'"); Iterator<Row> iterator = rows.iterator(); verifyNextColumnDefinition( iterator, "a", "partition_key", "org.apache.cassandra.db.marshal.Int32Type"); assertThat(iterator.hasNext()).isFalse(); }
@Test( groups = "unit", expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The renamed column name 'add' is not allowed because it is a reserved keyword") public void should_fail_if_renamed_column_is_a_reserved_keyword() throws Exception { alterTable("test").renameColumn("add"); }
@Test(groups = "unit") public void should_drop_column() throws Exception { // When SchemaStatement statement = alterTable("test").dropColumn("name"); // Then assertThat(statement.getQueryString()).isEqualTo("\n\tALTER TABLE test DROP name"); }
.execute( SchemaBuilder.alterTable("TableMetadata") .withOptions() .defaultTimeToLive(1337) .bloomFilterFPChance(0.42)
@Test(groups = "unit") public void should_add_static_column() throws Exception { // When SchemaStatement statement = alterTable("test").addStaticColumn("stat").type(DataType.text()); // Then assertThat(statement.getQueryString()).isEqualTo("\n\tALTER TABLE test ADD stat text static"); } }
@Test(groups = "unit") public void should_alter_column_type() throws Exception { // When SchemaStatement statement = alterTable("test").alterColumn("name").type(DataType.ascii()); // Then assertThat(statement.getQueryString()).isEqualTo("\n\tALTER TABLE test ALTER name TYPE ascii"); }
@Test( groups = "unit", expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The new column name 'add' is not allowed because it is a reserved keyword") public void should_fail_if_new_renamed_column_is_a_reserved_keyword() throws Exception { alterTable("test").renameColumn("col").to("add"); }
@Test( groups = "unit", expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The dropped column name 'add' is not allowed because it is a reserved keyword") public void should_fail_if_drop_column_is_a_reserved_keyword() throws Exception { alterTable("test").dropColumn("add").getQueryString(); }
.withOptions() .bloomFilterFPChance(0.01) .caching(Caching.ROWS_ONLY) alterTable("test").withOptions().caching(KeyCaching.NONE, rows(100));
@Test(groups = "unit") public void should_add_static_column() throws Exception { // When SchemaStatement statement = alterTable("test").addStaticColumn("stat").type(DataType.text()); // Then assertThat(statement.getQueryString()).isEqualTo("\n\tALTER TABLE test ADD stat text static"); } }
/** * Define the type of the added column. * * @param type the type of the added column. * @return the final statement. */ public SchemaStatement type(DataType type) { return SchemaStatement.fromQueryString( alter.buildInternal() + " ADD " + columnName + " " + type.toString() + (staticColumn ? " static" : "")); }
/** * Start building a new ALTER TABLE statement. * * @param keyspaceName the name of the keyspace to be used. * @param tableName the name of the table to be altered. * @return an in-construction ALTER TABLE statement. */ public static Alter alterTable(String keyspaceName, String tableName) { return new Alter(keyspaceName, tableName); }
@Test(groups = "unit") public void should_add_column_with_UDT_type() throws Exception { // When SchemaStatement statement = alterTable("test").addColumn("location").udtType(frozen("address")); // Then assertThat(statement.getQueryString()) .isEqualTo("\n\tALTER TABLE test ADD location frozen<address>"); }
@Test(groups = "unit") public void should_alter_column_type_with_keyspace() throws Exception { // When SchemaStatement statement = alterTable("ks", "test").alterColumn("name").type(DataType.ascii()); // Then assertThat(statement.getQueryString()) .isEqualTo("\n\tALTER TABLE ks.test ALTER name TYPE ascii"); }
@Test(groups = "unit") public void should_rename_column() throws Exception { // When SchemaStatement statement = alterTable("test").renameColumn("name").to("description"); // Then assertThat(statement.getQueryString()) .isEqualTo("\n\tALTER TABLE test RENAME name TO description"); }
@Test(groups = "unit") public void should_drop_column() throws Exception { // When SchemaStatement statement = alterTable("test").dropColumn("name"); // Then assertThat(statement.getQueryString()).isEqualTo("\n\tALTER TABLE test DROP name"); }