Fix querysets using the SystemPart model

This commit is contained in:
Itai Shirav 2019-06-13 08:38:06 +03:00
parent 9dd1a8f409
commit da87a151df
5 changed files with 25 additions and 6 deletions

View File

@ -13,6 +13,7 @@ Unreleased
- Add UUIDField (kpotehin) - Add UUIDField (kpotehin)
- Add `log_statements` parameter to database initializer - Add `log_statements` parameter to database initializer
- Fix test_merge which fails on ClickHouse v19.8.3 - Fix test_merge which fails on ClickHouse v19.8.3
- Fix querysets using the SystemPart model
v1.0.4 v1.0.4
------ ------

View File

@ -361,6 +361,9 @@ class Database(object):
if '$' in query: if '$' in query:
mapping = dict(db="`%s`" % self.db_name) mapping = dict(db="`%s`" % self.db_name)
if model_class: if model_class:
if model_class.is_system_model():
mapping['table'] = model_class.table_name()
else:
mapping['table'] = "`%s`.`%s`" % (self.db_name, model_class.table_name()) mapping['table'] = "`%s`.`%s`" % (self.db_name, model_class.table_name())
query = Template(query).safe_substitute(mapping) query = Template(query).safe_substitute(mapping)
return query return query

View File

@ -344,9 +344,12 @@ class QuerySet(object):
""" """
distinct = 'DISTINCT ' if self._distinct else '' distinct = 'DISTINCT ' if self._distinct else ''
final = ' FINAL' if self._final else '' final = ' FINAL' if self._final else ''
table_name = self._model_cls.table_name()
if not self._model_cls.is_system_model():
table_name = '`%s`' % table_name
params = (distinct, self.select_fields_as_sql(), self._model_cls.table_name(), final) params = (distinct, self.select_fields_as_sql(), table_name, final)
sql = u'SELECT %s%s\nFROM `%s`%s' % params sql = u'SELECT %s%s\nFROM %s%s' % params
if self._prewhere_q and not self._prewhere_q.is_empty: if self._prewhere_q and not self._prewhere_q.is_empty:
sql += '\nPREWHERE ' + self.conditions_as_sql(prewhere=True) sql += '\nPREWHERE ' + self.conditions_as_sql(prewhere=True)

View File

@ -19,8 +19,8 @@ class SystemPart(Model):
""" """
OPERATIONS = frozenset({'DETACH', 'DROP', 'ATTACH', 'FREEZE', 'FETCH'}) OPERATIONS = frozenset({'DETACH', 'DROP', 'ATTACH', 'FREEZE', 'FETCH'})
readonly = True _readonly = True
system = True _system = True
database = StringField() # Name of the database where the table that this part belongs to is located. database = StringField() # Name of the database where the table that this part belongs to is located.
table = StringField() # Name of the table that this part belongs to. table = StringField() # Name of the table that this part belongs to.

View File

@ -13,6 +13,7 @@ from infi.clickhouse_orm.system_models import SystemPart
class SystemTest(unittest.TestCase): class SystemTest(unittest.TestCase):
def setUp(self): def setUp(self):
self.database = Database('test-db', log_statements=True) self.database = Database('test-db', log_statements=True)
@ -54,6 +55,12 @@ class SystemPartTest(unittest.TestCase):
return dirnames return dirnames
raise unittest.SkipTest('Cannot find backups dir') raise unittest.SkipTest('Cannot find backups dir')
def test_is_read_only(self):
self.assertTrue(SystemPart.is_read_only())
def test_is_system_model(self):
self.assertTrue(SystemPart.is_system_model())
def test_get_all(self): def test_get_all(self):
parts = SystemPart.get(self.database) parts = SystemPart.get(self.database)
self.assertEqual(len(list(parts)), 2) self.assertEqual(len(list(parts)), 2)
@ -62,7 +69,8 @@ class SystemPartTest(unittest.TestCase):
parts = list(SystemPart.get_active(self.database)) parts = list(SystemPart.get_active(self.database))
self.assertEqual(len(parts), 2) self.assertEqual(len(parts), 2)
parts[0].detach() parts[0].detach()
self.assertEqual(len(list(SystemPart.get_active(self.database))), 1) parts = list(SystemPart.get_active(self.database))
self.assertEqual(len(parts), 1)
def test_get_conditions(self): def test_get_conditions(self):
parts = list(SystemPart.get(self.database, conditions="table='testtable'")) parts = list(SystemPart.get(self.database, conditions="table='testtable'"))
@ -101,6 +109,10 @@ class SystemPartTest(unittest.TestCase):
# TODO Not tested, as I have no replication set # TODO Not tested, as I have no replication set
pass pass
def test_query(self):
SystemPart.objects_in(self.database).count()
list(SystemPart.objects_in(self.database).filter(table='testtable'))
class TestTable(Model): class TestTable(Model):
date_field = DateField() date_field = DateField()