cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alek...@apache.org
Subject [3/5] git commit: Fix saving triggers to schema
Date Tue, 11 Mar 2014 12:24:56 GMT
Fix saving triggers to schema

patch by Sam Tunnicliffe; reviewed by Aleksey Yeschenko for
CASSANDRA-6789


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/553401d2
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/553401d2
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/553401d2

Branch: refs/heads/cassandra-2.1
Commit: 553401d2fef2a8ab66b2da7a79d865be4dd669d9
Parents: 3f38361
Author: Sam Tunnicliffe <sam@beobal.com>
Authored: Tue Mar 11 14:48:53 2014 +0300
Committer: Aleksey Yeschenko <aleksey@apache.org>
Committed: Tue Mar 11 14:48:53 2014 +0300

----------------------------------------------------------------------
 CHANGES.txt                                     |   4 +
 .../org/apache/cassandra/config/CFMetaData.java |   3 +
 .../cassandra/triggers/TriggersSchemaTest.java  | 126 +++++++++++++++++++
 3 files changed, 133 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/553401d2/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 920f073..39656ff 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,3 +1,7 @@
+2.0.7
+ * Fix saving triggers to schema (CASSANDRA-6789)
+
+
 2.0.6
  * Avoid race-prone second "scrub" of system keyspace (CASSANDRA-6797)
  * Pool CqlRecordWriter clients by inetaddress rather than Range 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/553401d2/src/java/org/apache/cassandra/config/CFMetaData.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/config/CFMetaData.java b/src/java/org/apache/cassandra/config/CFMetaData.java
index a319930..ff40e65 100644
--- a/src/java/org/apache/cassandra/config/CFMetaData.java
+++ b/src/java/org/apache/cassandra/config/CFMetaData.java
@@ -1532,6 +1532,9 @@ public final class CFMetaData
     {
         toSchemaNoColumnsNoTriggers(rm, timestamp);
 
+        for (TriggerDefinition td : triggers.values())
+            td.toSchema(rm, cfName, timestamp);
+
         for (ColumnDefinition cd : column_metadata.values())
             cd.toSchema(rm, cfName, getColumnDefinitionComparator(cd), timestamp);
     }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/553401d2/test/unit/org/apache/cassandra/triggers/TriggersSchemaTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/triggers/TriggersSchemaTest.java b/test/unit/org/apache/cassandra/triggers/TriggersSchemaTest.java
new file mode 100644
index 0000000..f9d71ee
--- /dev/null
+++ b/test/unit/org/apache/cassandra/triggers/TriggersSchemaTest.java
@@ -0,0 +1,126 @@
+/*
+ * 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.triggers;
+
+import java.util.Collections;
+
+import org.junit.Test;
+
+import org.apache.cassandra.SchemaLoader;
+import org.apache.cassandra.config.CFMetaData;
+import org.apache.cassandra.config.KSMetaData;
+import org.apache.cassandra.config.Schema;
+import org.apache.cassandra.config.TriggerDefinition;
+import org.apache.cassandra.locator.SimpleStrategy;
+import org.apache.cassandra.service.MigrationManager;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class TriggersSchemaTest extends SchemaLoader
+{
+    String ksName = "ks" + System.nanoTime();
+    String cfName = "cf" + System.nanoTime();
+    String triggerName = "trigger_" + System.nanoTime();
+    String triggerClass = "org.apache.cassandra.triggers.NoSuchTrigger.class";
+
+    @Test
+    public void newKsContainsCfWithTrigger() throws Exception
+    {
+        TriggerDefinition td = TriggerDefinition.create(triggerName, triggerClass);
+        CFMetaData cfm1 = CFMetaData.compile(String.format("CREATE TABLE %s (k int PRIMARY
KEY, v int)", cfName), ksName);
+        cfm1.addTriggerDefinition(td);
+        KSMetaData ksm = KSMetaData.newKeyspace(ksName,
+                                                SimpleStrategy.class,
+                                                Collections.singletonMap("replication_factor",
"1"),
+                                                true,
+                                                Collections.singletonList(cfm1));
+        MigrationManager.announceNewKeyspace(ksm);
+
+        CFMetaData cfm2 = Schema.instance.getCFMetaData(ksName, cfName);
+        assertFalse(cfm2.getTriggers().isEmpty());
+        assertEquals(1, cfm2.getTriggers().size());
+        assertEquals(td, cfm2.getTriggers().get(triggerName));
+    }
+
+    @Test
+    public void addNewCfWithTriggerToKs() throws Exception
+    {
+        KSMetaData ksm = KSMetaData.newKeyspace(ksName,
+                                                SimpleStrategy.class,
+                                                Collections.singletonMap("replication_factor",
"1"),
+                                                true,
+                                                Collections.EMPTY_LIST);
+        MigrationManager.announceNewKeyspace(ksm);
+
+        CFMetaData cfm1 = CFMetaData.compile(String.format("CREATE TABLE %s (k int PRIMARY
KEY, v int)", cfName), ksName);
+        TriggerDefinition td = TriggerDefinition.create(triggerName, triggerClass);
+        cfm1.addTriggerDefinition(td);
+
+        MigrationManager.announceNewColumnFamily(cfm1);
+
+        CFMetaData cfm2 = Schema.instance.getCFMetaData(ksName, cfName);
+        assertFalse(cfm2.getTriggers().isEmpty());
+        assertEquals(1, cfm2.getTriggers().size());
+        assertEquals(td, cfm2.getTriggers().get(triggerName));
+    }
+
+    @Test
+    public void addTriggerToCf() throws Exception
+    {
+        CFMetaData cfm1 = CFMetaData.compile(String.format("CREATE TABLE %s (k int PRIMARY
KEY, v int)", cfName), ksName);
+        KSMetaData ksm = KSMetaData.newKeyspace(ksName,
+                                                SimpleStrategy.class,
+                                                Collections.singletonMap("replication_factor",
"1"),
+                                                true,
+                                                Collections.singletonList(cfm1));
+        MigrationManager.announceNewKeyspace(ksm);
+
+        CFMetaData cfm2 = Schema.instance.getCFMetaData(ksName, cfName).clone();
+        TriggerDefinition td = TriggerDefinition.create(triggerName, triggerClass);
+        cfm2.addTriggerDefinition(td);
+        MigrationManager.announceColumnFamilyUpdate(cfm2, false);
+
+        CFMetaData cfm3 = Schema.instance.getCFMetaData(ksName, cfName);
+        assertFalse(cfm3.getTriggers().isEmpty());
+        assertEquals(1, cfm3.getTriggers().size());
+        assertEquals(td, cfm3.getTriggers().get(triggerName));
+    }
+
+    @Test
+    public void removeTriggerFromCf() throws Exception
+    {
+        TriggerDefinition td = TriggerDefinition.create(triggerName, triggerClass);
+        CFMetaData cfm1 = CFMetaData.compile(String.format("CREATE TABLE %s (k int PRIMARY
KEY, v int)", cfName), ksName);
+        cfm1.addTriggerDefinition(td);
+        KSMetaData ksm = KSMetaData.newKeyspace(ksName,
+                                                SimpleStrategy.class,
+                                                Collections.singletonMap("replication_factor",
"1"),
+                                                true,
+                                                Collections.singletonList(cfm1));
+        MigrationManager.announceNewKeyspace(ksm);
+
+        CFMetaData cfm2 = Schema.instance.getCFMetaData(ksName, cfName).clone();
+        cfm2.removeTrigger(triggerName);
+        MigrationManager.announceColumnFamilyUpdate(cfm2, false);
+
+        CFMetaData cfm3 = Schema.instance.getCFMetaData(ksName, cfName).clone();
+        assertTrue(cfm3.getTriggers().isEmpty());
+    }
+}


Mime
View raw message