Make tests pass

This commit is contained in:
Itai Shirav 2018-04-21 13:14:15 +03:00
parent b6229125a3
commit 66eda2214c
4 changed files with 19 additions and 37 deletions

View File

@ -328,8 +328,12 @@ class Database(object):
return pytz.utc return pytz.utc
def _get_server_version(self, as_tuple=True): def _get_server_version(self, as_tuple=True):
r = self._send('SELECT version();') try:
ver = r.text r = self._send('SELECT version();')
ver = r.text
except ServerError as e:
logger.exception('Cannot determine server version (%s), assuming 1.1.0', e)
ver = '1.1.0'
return tuple(int(n) for n in ver.split('.')) if as_tuple else ver return tuple(int(n) for n in ver.split('.')) if as_tuple else ver
def _is_connection_readonly(self): def _is_connection_readonly(self):

View File

@ -197,7 +197,7 @@ class Merge(Engine):
self.table_regex = table_regex self.table_regex = table_regex
def create_table_sql(self, db): def create_table_sql(self, db):
self.db_name = None + return "Merge(`%s`, '%s')" % (db.db_name, self.table_regex) return "Merge(`%s`, '%s')" % (db.db_name, self.table_regex)
class Distributed(Engine): class Distributed(Engine):
@ -210,20 +210,17 @@ class Distributed(Engine):
See full documentation here See full documentation here
https://clickhouse.yandex/docs/en/table_engines/distributed.html https://clickhouse.yandex/docs/en/table_engines/distributed.html
""" """
def __init__(self, cluster, table=None, db_name=None, sharding_key=None): def __init__(self, cluster, table=None, sharding_key=None):
""" """
:param cluster: what cluster to access data from :param cluster: what cluster to access data from
:param table: underlying table that actually stores data. :param table: underlying table that actually stores data.
If you are not specifying any table here, ensure that it can be inferred If you are not specifying any table here, ensure that it can be inferred
from your model's superclass (see models.DistributedModel.fix_engine_table) from your model's superclass (see models.DistributedModel.fix_engine_table)
:param db_name: which database to access data from
By default it is 'currentDatabase()'
:param sharding_key: how to distribute data among shards when inserting :param sharding_key: how to distribute data among shards when inserting
straightly into Distributed table, optional straightly into Distributed table, optional
""" """
self.cluster = cluster self.cluster = cluster
self.table = table self.table = table
self.db_name = db_name
self.sharding_key = sharding_key self.sharding_key = sharding_key
@property @property
@ -238,23 +235,17 @@ class Distributed(Engine):
return table return table
def set_db_name(self, db_name): def create_table_sql(self, db):
assert isinstance(db_name, six.string_types), "'db_name' parameter must be string"
self.db_name = db_name
def create_table_sql(self):
name = self.__class__.__name__ name = self.__class__.__name__
params = self._build_sql_params() params = self._build_sql_params(db)
return '%s(%s)' % (name, ', '.join(params)) return '%s(%s)' % (name, ', '.join(params))
def _build_sql_params(self): def _build_sql_params(self, db):
db_name = ("`%s`" % self.db_name) if self.db_name else 'currentDatabase()'
if self.table_name is None: if self.table_name is None:
raise ValueError("Cannot create {} engine: specify an underlying table".format( raise ValueError("Cannot create {} engine: specify an underlying table".format(
self.__class__.__name__)) self.__class__.__name__))
params = [self.cluster, db_name, self.table_name] params = ["`%s`" % p for p in [self.cluster, db.db_name, self.table_name]]
if self.sharding_key: if self.sharding_key:
params.append(self.sharding_key) params.append(self.sharding_key)
return params return params

View File

@ -304,7 +304,6 @@ class DistributedModel(Model):
def set_database(self, db): def set_database(self, db):
assert isinstance(self.engine, Distributed), "engine must be engines.Distributed instance" assert isinstance(self.engine, Distributed), "engine must be engines.Distributed instance"
res = super(DistributedModel, self).set_database(db) res = super(DistributedModel, self).set_database(db)
self.engine.set_db_name(db.db_name)
return res return res
@classmethod @classmethod
@ -359,16 +358,13 @@ class DistributedModel(Model):
cls.engine.table = storage_models[0] cls.engine.table = storage_models[0]
@classmethod @classmethod
def create_table_sql(cls, db_name): def create_table_sql(cls, db):
assert isinstance(cls.engine, Distributed), "engine must be engines.Distributed instance" assert isinstance(cls.engine, Distributed), "engine must be engines.Distributed instance"
cls.engine.set_db_name(db_name)
cls.fix_engine_table() cls.fix_engine_table()
parts = [ parts = [
'CREATE TABLE IF NOT EXISTS `{0}`.`{1}` AS `{0}`.`{2}`'.format( 'CREATE TABLE IF NOT EXISTS `{0}`.`{1}` AS `{0}`.`{2}`'.format(
db_name, cls.table_name(), cls.engine.table_name), db.db_name, cls.table_name(), cls.engine.table_name),
'ENGINE = ' + cls.engine.create_table_sql()] 'ENGINE = ' + cls.engine.create_table_sql(db)]
return '\n'.join(parts) return '\n'.join(parts)
cls.engine.set_db_name(db_name)
return super(MergeModel, cls).create_table_sql(db_name)

View File

@ -1,18 +1,9 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import unittest import unittest
<<<<<<< HEAD from infi.clickhouse_orm.system_models import SystemPart
from infi.clickhouse_orm.database import Database, DatabaseException, ServerError from infi.clickhouse_orm.database import Database, DatabaseException, ServerError
from infi.clickhouse_orm.models import Model, MergeModel, DistributedModel from infi.clickhouse_orm.models import Model, MergeModel, DistributedModel
||||||| merged common ancestors
from infi.clickhouse_orm.database import Database, DatabaseException
from infi.clickhouse_orm.models import Model, MergeModel
=======
from infi.clickhouse_orm.system_models import SystemPart
from infi.clickhouse_orm.database import Database, DatabaseException
from infi.clickhouse_orm.models import Model, MergeModel
>>>>>>> 7fb05896926acab163a1f373092bf22cc0f3cb4f
from infi.clickhouse_orm.fields import * from infi.clickhouse_orm.fields import *
from infi.clickhouse_orm.engines import * from infi.clickhouse_orm.engines import *
@ -167,15 +158,15 @@ class DistributedTestCase(_EnginesHelperTestCase):
engine = Distributed('my_cluster') engine = Distributed('my_cluster')
with self.assertRaises(ValueError) as cm: with self.assertRaises(ValueError) as cm:
engine.create_table_sql() engine.create_table_sql(self.database)
exc = cm.exception exc = cm.exception
self.assertEqual(str(exc), 'Cannot create Distributed engine: specify an underlying table') self.assertEqual(str(exc), 'Cannot create Distributed engine: specify an underlying table')
def test_with_table_name(self): def test_with_table_name(self):
engine = Distributed('my_cluster', 'foo') engine = Distributed('my_cluster', 'foo')
sql = engine.create_table_sql() sql = engine.create_table_sql(self.database)
self.assertEqual(sql, 'Distributed(my_cluster, currentDatabase(), foo)') self.assertEqual(sql, 'Distributed(`my_cluster`, `test-db`, `foo`)')
class TestModel(SampleModel): class TestModel(SampleModel):
engine = TinyLog() engine = TinyLog()