From 0391482ec7644f67afdaf2558b1c091071f6ca01 Mon Sep 17 00:00:00 2001 From: M1ha Date: Mon, 18 Sep 2017 15:53:15 +0500 Subject: [PATCH] Fixed 2 bugs in Merge engine: 1) If database name contained some signes (+, -) create merge table failed. 2) When merge table was created and set_database was not previously called, "currentDatabase()" was also used, which could lead to errors on multiple databases. --- src/infi/clickhouse_orm/engines.py | 5 +++-- src/infi/clickhouse_orm/models.py | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/infi/clickhouse_orm/engines.py b/src/infi/clickhouse_orm/engines.py index 777bd2e..57ca1ce 100644 --- a/src/infi/clickhouse_orm/engines.py +++ b/src/infi/clickhouse_orm/engines.py @@ -146,10 +146,11 @@ class Merge(Engine): self.table_regex = table_regex # Use current database as default - self.db_name = 'currentDatabase()' + self.db_name = None def create_table_sql(self): - return "Merge(%s, '%s')" % (self.db_name, self.table_regex) + db_name = ("`%s`" % self.db_name) if self.db_name else 'currentDatabase()' + return "Merge(%s, '%s')" % (db_name, self.table_regex) def set_db_name(self, db_name): assert isinstance(db_name, six.string_types), "'db_name' parameter must be string" diff --git a/src/infi/clickhouse_orm/models.py b/src/infi/clickhouse_orm/models.py index 33cfde2..0e65b9f 100644 --- a/src/infi/clickhouse_orm/models.py +++ b/src/infi/clickhouse_orm/models.py @@ -274,3 +274,9 @@ class MergeModel(Model): res = super(MergeModel, self).set_database(db) self.engine.set_db_name(db.db_name) return res + + @classmethod + def create_table_sql(cls, db_name): + assert isinstance(cls.engine, Merge), "engine must be engines.Merge instance" + cls.engine.set_db_name(db_name) + return super(MergeModel, cls).create_table_sql(db_name)