/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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.apache.cassandra.cql3; import org.apache.cassandra.db.ColumnFamilyStore; import org.apache.cassandra.db.Keyspace; import org.junit.Test; import static org.junit.Assert.assertEquals; public class AlterTableTest extends CQLTester { @Test public void testAddList() throws Throwable { createTable("CREATE TABLE %s (id text PRIMARY KEY, content text);"); execute("ALTER TABLE %s ADD myCollection list<text>;"); execute("INSERT INTO %s (id, content , myCollection) VALUES ('test', 'first test', ['first element']);"); assertRows(execute("SELECT * FROM %s;"), row("test", "first test", list("first element"))); } @Test public void testDropList() throws Throwable { createTable("CREATE TABLE %s (id text PRIMARY KEY, content text, myCollection list<text>);"); execute("INSERT INTO %s (id, content , myCollection) VALUES ('test', 'first test', ['first element']);"); execute("ALTER TABLE %s DROP myCollection;"); assertRows(execute("SELECT * FROM %s;"), row("test", "first test")); } @Test public void testAddMap() throws Throwable { createTable("CREATE TABLE %s (id text PRIMARY KEY, content text);"); execute("ALTER TABLE %s ADD myCollection map<text, text>;"); execute("INSERT INTO %s (id, content , myCollection) VALUES ('test', 'first test', { '1' : 'first element'});"); assertRows(execute("SELECT * FROM %s;"), row("test", "first test", map("1", "first element"))); } @Test public void testDropMap() throws Throwable { createTable("CREATE TABLE %s (id text PRIMARY KEY, content text, myCollection map<text, text>);"); execute("INSERT INTO %s (id, content , myCollection) VALUES ('test', 'first test', { '1' : 'first element'});"); execute("ALTER TABLE %s DROP myCollection;"); assertRows(execute("SELECT * FROM %s;"), row("test", "first test")); } @Test public void testDropListAndAddListWithSameName() throws Throwable { createTable("CREATE TABLE %s (id text PRIMARY KEY, content text, myCollection list<text>);"); execute("INSERT INTO %s (id, content , myCollection) VALUES ('test', 'first test', ['first element']);"); execute("ALTER TABLE %s DROP myCollection;"); execute("ALTER TABLE %s ADD myCollection list<text>;"); assertRows(execute("SELECT * FROM %s;"), row("test", "first test", null)); execute("UPDATE %s set myCollection = ['second element'] WHERE id = 'test';"); assertRows(execute("SELECT * FROM %s;"), row("test", "first test", list("second element"))); } @Test public void testDropListAndAddMapWithSameName() throws Throwable { createTable("CREATE TABLE %s (id text PRIMARY KEY, content text, myCollection list<text>);"); execute("INSERT INTO %s (id, content , myCollection) VALUES ('test', 'first test', ['first element']);"); execute("ALTER TABLE %s DROP myCollection;"); assertInvalid("ALTER TABLE %s ADD myCollection map<int, int>;"); } @Test public void testChangeStrategyWithUnquotedAgrument() throws Throwable { createTable("CREATE TABLE %s (id text PRIMARY KEY);"); assertInvalidSyntaxMessage("no viable alternative at input '}'", "ALTER TABLE %s WITH caching = {'keys' : 'all', 'rows_per_partition' : ALL};"); } @Test // tests CASSANDRA-7976 public void testAlterIndexInterval() throws Throwable { String tableName = createTable("CREATE TABLE IF NOT EXISTS %s (id uuid, album text, artist text, data blob, PRIMARY KEY (id))"); ColumnFamilyStore cfs = Keyspace.open(KEYSPACE).getColumnFamilyStore(tableName); alterTable("ALTER TABLE %s WITH min_index_interval=256 AND max_index_interval=512"); assertEquals(256, cfs.metadata.getMinIndexInterval()); assertEquals(512, cfs.metadata.getMaxIndexInterval()); alterTable("ALTER TABLE %s WITH caching = 'none'"); assertEquals(256, cfs.metadata.getMinIndexInterval()); assertEquals(512, cfs.metadata.getMaxIndexInterval()); } }