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 `log_statements` parameter to database initializer
- Fix test_merge which fails on ClickHouse v19.8.3
- Fix querysets using the SystemPart model
v1.0.4
------

View File

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

View File

@ -344,9 +344,12 @@ class QuerySet(object):
"""
distinct = 'DISTINCT ' if self._distinct 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)
sql = u'SELECT %s%s\nFROM `%s`%s' % params
params = (distinct, self.select_fields_as_sql(), table_name, final)
sql = u'SELECT %s%s\nFROM %s%s' % params
if self._prewhere_q and not self._prewhere_q.is_empty:
sql += '\nPREWHERE ' + self.conditions_as_sql(prewhere=True)

View File

@ -19,8 +19,8 @@ class SystemPart(Model):
"""
OPERATIONS = frozenset({'DETACH', 'DROP', 'ATTACH', 'FREEZE', 'FETCH'})
readonly = True
system = True
_readonly = True
_system = True
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.

View File

@ -13,6 +13,7 @@ from infi.clickhouse_orm.system_models import SystemPart
class SystemTest(unittest.TestCase):
def setUp(self):
self.database = Database('test-db', log_statements=True)
@ -54,6 +55,12 @@ class SystemPartTest(unittest.TestCase):
return dirnames
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):
parts = SystemPart.get(self.database)
self.assertEqual(len(list(parts)), 2)
@ -62,7 +69,8 @@ class SystemPartTest(unittest.TestCase):
parts = list(SystemPart.get_active(self.database))
self.assertEqual(len(parts), 2)
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):
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
pass
def test_query(self):
SystemPart.objects_in(self.database).count()
list(SystemPart.objects_in(self.database).filter(table='testtable'))
class TestTable(Model):
date_field = DateField()