From f27d7db0270b7f7884227fa8a69c9ecdce45cc2d Mon Sep 17 00:00:00 2001 From: Adam Holmberg Date: Thu, 1 Oct 2015 13:56:55 -0500 Subject: [PATCH 1/6] meta: export_as_string for UserType PYTHON-405 --- cassandra/metadata.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cassandra/metadata.py b/cassandra/metadata.py index 8700c65f..87e99fd0 100644 --- a/cassandra/metadata.py +++ b/cassandra/metadata.py @@ -672,7 +672,7 @@ class KeyspaceMetadata(object): for field_type in user_type.field_types: if field_type.cassname == 'UserType' and field_type.typename in types: self.resolve_user_types(field_type.typename, types, user_type_strings) - user_type_strings.append(user_type.as_cql_query(formatted=True)) + user_type_strings.append(user_type.export_as_string()) def _add_table_metadata(self, table_metadata): self._drop_table_metadata(table_metadata.name) @@ -765,9 +765,12 @@ class UserType(object): fields.append("%s %s" % (protect_name(field_name), field_type.cql_parameterized_type())) ret += field_join.join("%s%s" % (padding, field) for field in fields) - ret += "\n);" if formatted else ");" + ret += "\n)" if formatted else ")" return ret + def export_as_string(self): + return self.as_cql_query(formatted=True) + ';' + class Aggregate(object): """ From dfe97db082799a925f7203054b5adc33a28cf987 Mon Sep 17 00:00:00 2001 From: Adam Holmberg Date: Thu, 1 Oct 2015 14:08:55 -0500 Subject: [PATCH 2/6] meta: export_as_string for Function and Aggregate PYTHON-405 --- cassandra/metadata.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cassandra/metadata.py b/cassandra/metadata.py index 87e99fd0..041aa85f 100644 --- a/cassandra/metadata.py +++ b/cassandra/metadata.py @@ -635,8 +635,8 @@ class KeyspaceMetadata(object): """ cql = "\n\n".join([self.as_cql_query()] + self.user_type_strings() - + [f.as_cql_query(True) for f in self.functions.values()] - + [a.as_cql_query(True) for a in self.aggregates.values()] + + [f.export_as_string() for f in self.functions.values()] + + [a.export_as_string() for a in self.aggregates.values()] + [t.export_as_string() for t in self.tables.values()]) if self._exc_info: import traceback @@ -855,6 +855,9 @@ class Aggregate(object): return ret + def export_as_string(self): + return self.as_cql_query(formatted=True) + ';' + @property def signature(self): return SignatureDescriptor.format_signature(self.name, self.type_signature) @@ -943,7 +946,10 @@ class Function(object): "%(on_null)s ON NULL INPUT%(sep)s" \ "RETURNS %(typ)s%(sep)s" \ "LANGUAGE %(lang)s%(sep)s" \ - "AS $$%(body)s$$;" % locals() + "AS $$%(body)s$$" % locals() + + def export_as_string(self): + return self.as_cql_query(formatted=True) + ';' @property def signature(self): From bfa75d6c2d7a85b0bab27c9ed2f97c7e753c812e Mon Sep 17 00:00:00 2001 From: Adam Holmberg Date: Thu, 1 Oct 2015 14:10:01 -0500 Subject: [PATCH 3/6] meta: improved format for Function and Aggregate CQL --- cassandra/metadata.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cassandra/metadata.py b/cassandra/metadata.py index 041aa85f..336437c4 100644 --- a/cassandra/metadata.py +++ b/cassandra/metadata.py @@ -838,7 +838,7 @@ class Aggregate(object): If `formatted` is set to :const:`True`, extra whitespace will be added to make the query more readable. """ - sep = '\n' if formatted else ' ' + sep = '\n ' if formatted else ' ' keyspace = protect_name(self.keyspace) name = protect_name(self.name) type_list = ', '.join(self.type_signature) @@ -932,7 +932,7 @@ class Function(object): If `formatted` is set to :const:`True`, extra whitespace will be added to make the query more readable. """ - sep = '\n' if formatted else ' ' + sep = '\n ' if formatted else ' ' keyspace = protect_name(self.keyspace) name = protect_name(self.name) arg_list = ', '.join(["%s %s" % (protect_name(n), t) From 1395401898f47d62d9bfebe858a1d5d288ca3169 Mon Sep 17 00:00:00 2001 From: Adam Holmberg Date: Thu, 1 Oct 2015 14:52:42 -0500 Subject: [PATCH 4/6] meta: export_as_string for Trigger --- cassandra/metadata.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cassandra/metadata.py b/cassandra/metadata.py index 336437c4..9ef94065 100644 --- a/cassandra/metadata.py +++ b/cassandra/metadata.py @@ -1571,6 +1571,9 @@ class TriggerMetadata(object): ) return ret + def export_as_string(self): + return self.as_cql_query() + ';' + class _SchemaParser(object): From 3b228cb73a6a8f9878e70903da05b4df99457296 Mon Sep 17 00:00:00 2001 From: GregBestland Date: Wed, 14 Oct 2015 15:33:40 -0500 Subject: [PATCH 5/6] Fixing issue with ; delimeter on as_cql_query for keyspace. Changing all_as_cql to internal method to avoid confusion --- cassandra/metadata.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cassandra/metadata.py b/cassandra/metadata.py index 9ef94065..fb512eb8 100644 --- a/cassandra/metadata.py +++ b/cassandra/metadata.py @@ -656,7 +656,7 @@ class KeyspaceMetadata(object): ret = "CREATE KEYSPACE %s WITH replication = %s " % ( protect_name(self.name), self.replication_strategy.export_for_schema()) - return ret + (' AND durable_writes = %s;' % ("true" if self.durable_writes else "false")) + return ret + (' AND durable_writes = %s' % ("true" if self.durable_writes else "false")) def user_type_strings(self): user_type_strings = [] @@ -1079,18 +1079,18 @@ class TableMetadata(object): (self.keyspace_name, self.name) for line in traceback.format_exception(*self._exc_info): ret += line - ret += "\nApproximate structure, for reference:\n(this should not be used to reproduce this schema)\n\n%s\n*/" % self.all_as_cql() + ret += "\nApproximate structure, for reference:\n(this should not be used to reproduce this schema)\n\n%s\n*/" % self._all_as_cql() elif not self.is_cql_compatible: # If we can't produce this table with CQL, comment inline ret = "/*\nWarning: Table %s.%s omitted because it has constructs not compatible with CQL (was created via legacy API).\n" % \ (self.keyspace_name, self.name) - ret += "\nApproximate structure, for reference:\n(this should not be used to reproduce this schema)\n\n%s\n*/" % self.all_as_cql() + ret += "\nApproximate structure, for reference:\n(this should not be used to reproduce this schema)\n\n%s\n*/" % self._all_as_cql() else: - ret = self.all_as_cql() + ret = self._all_as_cql() return ret - def all_as_cql(self): + def _all_as_cql(self): ret = self.as_cql_query(formatted=True) ret += ";" From 30f2a2a895ba8bcdd69d684810743a88f874337a Mon Sep 17 00:00:00 2001 From: Adam Holmberg Date: Thu, 15 Oct 2015 09:26:08 -0500 Subject: [PATCH 6/6] semicolon following keyspace CQL in export_as_string --- cassandra/metadata.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cassandra/metadata.py b/cassandra/metadata.py index fb512eb8..9750741b 100644 --- a/cassandra/metadata.py +++ b/cassandra/metadata.py @@ -633,7 +633,7 @@ class KeyspaceMetadata(object): Returns a CQL query string that can be used to recreate the entire keyspace, including user-defined types and tables. """ - cql = "\n\n".join([self.as_cql_query()] + cql = "\n\n".join([self.as_cql_query() + ';'] + self.user_type_strings() + [f.export_as_string() for f in self.functions.values()] + [a.export_as_string() for a in self.aggregates.values()]