diff --git a/CHANGELOG.md b/CHANGELOG.md index 96e3724..0063bc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ------ diff --git a/src/infi/clickhouse_orm/database.py b/src/infi/clickhouse_orm/database.py index d9eac52..8aaae47 100644 --- a/src/infi/clickhouse_orm/database.py +++ b/src/infi/clickhouse_orm/database.py @@ -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 diff --git a/src/infi/clickhouse_orm/query.py b/src/infi/clickhouse_orm/query.py index ab7a705..16de5ba 100644 --- a/src/infi/clickhouse_orm/query.py +++ b/src/infi/clickhouse_orm/query.py @@ -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) diff --git a/src/infi/clickhouse_orm/system_models.py b/src/infi/clickhouse_orm/system_models.py index 7341d14..d51ec3b 100644 --- a/src/infi/clickhouse_orm/system_models.py +++ b/src/infi/clickhouse_orm/system_models.py @@ -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. diff --git a/tests/test_system_models.py b/tests/test_system_models.py index 01229c8..1d8b8cc 100644 --- a/tests/test_system_models.py +++ b/tests/test_system_models.py @@ -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()