Skip to content

Commit 1e9d11d

Browse files
committed
cqlengine.management create keyspace API with explicit strategies
1 parent 46ba806 commit 1e9d11d

2 files changed

Lines changed: 34 additions & 27 deletions

File tree

cassandra/cqlengine/management.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import logging
1818
import six
1919

20+
from cassandra.metadata import KeyspaceMetadata
2021
from cassandra.cqlengine import SizeTieredCompactionStrategy, LeveledCompactionStrategy
2122
from cassandra.cqlengine.connection import execute, get_cluster
2223
from cassandra.cqlengine.exceptions import CQLEngineException
@@ -32,10 +33,9 @@
3233
schema_columnfamilies = NamedTable('system', 'schema_columnfamilies')
3334

3435

35-
def create_keyspace(name, strategy_class, replication_factor, durable_writes=True, **replication_values):
36+
def create_keyspace_simple(name, replication_factor, durable_writes=True):
3637
"""
37-
*Deprecated - this will likely be repaced with something specialized per replication strategy.*
38-
Creates a keyspace
38+
Creates a keyspace with SimpleStrategy for replica placement
3939
4040
If the keyspace already exists, it will not be modified.
4141
@@ -45,35 +45,40 @@ def create_keyspace(name, strategy_class, replication_factor, durable_writes=Tru
4545
*There are plans to guard schema-modifying functions with an environment-driven conditional.*
4646
4747
:param str name: name of keyspace to create
48-
:param str strategy_class: keyspace replication strategy class (:attr:`~.SimpleStrategy` or :attr:`~.NetworkTopologyStrategy`
4948
:param int replication_factor: keyspace replication factor, used with :attr:`~.SimpleStrategy`
5049
:param bool durable_writes: Write log is bypassed if set to False
51-
:param \*\*replication_values: Additional values to ad to the replication options map
5250
"""
51+
_create_keyspace(name, durable_writes, 'SimpleStrategy',
52+
{'replication_factor': replication_factor})
53+
54+
55+
def create_keyspace_network_topology(name, dc_replication_map, durable_writes=True):
56+
"""
57+
Creates a keyspace with NetworkTopologyStrategy for replica placement
58+
59+
If the keyspace already exists, it will not be modified.
60+
61+
**This function should be used with caution, especially in production environments.
62+
Take care to execute schema modifications in a single context (i.e. not concurrently with other clients).**
63+
64+
*There are plans to guard schema-modifying functions with an environment-driven conditional.*
65+
66+
:param str name: name of keyspace to create
67+
:param dict dc_replication_map: map of dc_names: replication_factor
68+
:param bool durable_writes: Write log is bypassed if set to False
69+
"""
70+
_create_keyspace(name, durable_writes, 'NetworkTopologyStrategy', dc_replication_map)
71+
72+
73+
def _create_keyspace(name, durable_writes, strategy_class, strategy_options):
5374
cluster = get_cluster()
5475

5576
if name not in cluster.metadata.keyspaces:
56-
# try the 1.2 method
57-
replication_map = {
58-
'class': strategy_class,
59-
'replication_factor': replication_factor
60-
}
61-
replication_map.update(replication_values)
62-
if strategy_class.lower() != 'simplestrategy':
63-
# Although the Cassandra documentation states for `replication_factor`
64-
# that it is "Required if class is SimpleStrategy; otherwise,
65-
# not used." we get an error if it is present.
66-
replication_map.pop('replication_factor', None)
67-
68-
query = """
69-
CREATE KEYSPACE {}
70-
WITH REPLICATION = {}
71-
""".format(name, json.dumps(replication_map).replace('"', "'"))
72-
73-
if strategy_class != 'SimpleStrategy':
74-
query += " AND DURABLE_WRITES = {}".format('true' if durable_writes else 'false')
75-
76-
execute(query)
77+
log.info("Creating keyspace %s ", name)
78+
ks_meta = KeyspaceMetadata(name, durable_writes, strategy_class, strategy_options)
79+
execute(ks_meta.as_cql_query())
80+
else:
81+
log.info("Not creating keyspace %s because it already exists", name)
7782

7883

7984
def delete_keyspace(name):

docs/api/cassandra/cqlengine/management.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
A collection of functions for managing keyspace and table schema.
77

8-
.. autofunction:: create_keyspace
8+
.. autofunction:: create_keyspace_simple
9+
10+
.. autofunction:: create_keyspace_network_topology
911

1012
.. autofunction:: delete_keyspace
1113

0 commit comments

Comments
 (0)