diff --git a/setup.py b/setup.py index 27c53ff..05c1f77 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ with open('requirements.txt') as f: setup( name='django-clickhouse', - version='1.1.0', + version='1.1.1', packages=['django_clickhouse', 'django_clickhouse.management.commands'], package_dir={'': 'src'}, url='https://github.com/carrotquest/django-clickhouse', diff --git a/src/django_clickhouse/management/commands/clickhouse_migrate.py b/src/django_clickhouse/management/commands/clickhouse_migrate.py index eef9aa5..8927141 100644 --- a/src/django_clickhouse/management/commands/clickhouse_migrate.py +++ b/src/django_clickhouse/management/commands/clickhouse_migrate.py @@ -3,7 +3,7 @@ Django command that applies migrations for ClickHouse database """ import json -from django.conf import settings +from django.apps import apps as django_apps from django.core.management import BaseCommand, CommandParser from ...configuration import config @@ -25,10 +25,10 @@ class Command(BaseCommand): parser.add_argument('--database', '-d', nargs='?', type=str, required=False, choices=config.DATABASES.keys(), help='ClickHouse database alias key from CLICKHOUSE_DATABASES django setting.' - ' By defaults migrations are applied to all databases.') + ' By default migrations are applied to all databases.') def handle(self, *args, **options) -> None: - apps = [options['app_label']] if options['app_label'] else list(settings.INSTALLED_APPS) + apps = [options['app_label']] if options['app_label'] else [app.name for app in django_apps.get_app_configs()] databases = [options['database']] if options['database'] else list(config.DATABASES.keys()) kwargs = {'up_to': options['migration_number']} if options['migration_number'] else {} diff --git a/src/django_clickhouse/migrations.py b/src/django_clickhouse/migrations.py index f38c261..edc4f03 100644 --- a/src/django_clickhouse/migrations.py +++ b/src/django_clickhouse/migrations.py @@ -112,7 +112,7 @@ def clickhouse_migrate(sender, **kwargs): return if kwargs.get('using', DJANGO_DEFAULT_DB_ALIAS) != DJANGO_DEFAULT_DB_ALIAS: - # Не надо выполнять синхронизацию для каждого шарда. Только один раз. + # Don't call sync for every database. Just once. return app_name = kwargs['app_config'].name @@ -124,9 +124,7 @@ def clickhouse_migrate(sender, **kwargs): class MigrationHistory(ClickHouseModel): """ A model for storing which migrations were already applied to database. - This """ - db_alias = StringField() package_name = StringField() module_name = StringField() diff --git a/src/django_clickhouse/tasks.py b/src/django_clickhouse/tasks.py index 3609119..df4ea89 100644 --- a/src/django_clickhouse/tasks.py +++ b/src/django_clickhouse/tasks.py @@ -3,7 +3,7 @@ import importlib from typing import Type, Union from celery import shared_task -from django.conf import settings +from django.apps import apps as django_apps from infi.clickhouse_orm.utils import import_submodules from django_clickhouse.clickhouse_models import ClickHouseModel @@ -32,8 +32,8 @@ def clickhouse_auto_sync() -> None: :return: None """ # Import all model modules - for app in settings.INSTALLED_APPS: - package_name = "%s.%s" % (app, config.MODELS_MODULE) + for app in django_apps.get_app_configs(): + package_name = "%s.%s" % (app.name, config.MODELS_MODULE) try: module = importlib.import_module(package_name) if hasattr(module, '__path__'): diff --git a/tests/apps.py b/tests/apps.py new file mode 100644 index 0000000..75d106e --- /dev/null +++ b/tests/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UnitTestAppConfig(AppConfig): + name = 'tests' + verbose_name = "Unit test app" diff --git a/tests/settings.py b/tests/settings.py index 1382d38..4d83af1 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -56,7 +56,9 @@ LOGGING = { INSTALLED_APPS = [ "src", - "tests" + + # This app is included with config in order to test all is working fine here + "tests.apps.UnitTestAppConfig" ] CLICKHOUSE_DATABASES = { diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 70a8c27..715bd22 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -63,15 +63,17 @@ class MigrateAppTest(TestCase): @override_settings(CLICKHOUSE_MIGRATE_WITH_DEFAULT_DB=False) @mock.patch('django_clickhouse.management.commands.clickhouse_migrate.migrate_app', return_value=True) class MigrateDjangoCommandTest(TestCase): + APP_LABELS = ('src', 'tests') + def setUp(self) -> None: self.cmd = Command() def test_handle_all(self, migrate_app_mock): self.cmd.handle(verbosity=3, app_label=None, database=None, migration_number=None) - self.assertEqual(len(config.DATABASES.keys()) * len(settings.INSTALLED_APPS), migrate_app_mock.call_count) + self.assertEqual(len(config.DATABASES.keys()) * len(self.APP_LABELS), migrate_app_mock.call_count) for db_alias in config.DATABASES.keys(): - for app_label in settings.INSTALLED_APPS: + for app_label in self.APP_LABELS: migrate_app_mock.assert_any_call(app_label, db_alias, verbosity=3) def test_handle_app(self, migrate_app_mock): @@ -85,7 +87,7 @@ class MigrateDjangoCommandTest(TestCase): self.cmd.handle(verbosity=3, database='default', app_label=None, migration_number=None) self.assertEqual(len(settings.INSTALLED_APPS), migrate_app_mock.call_count) - for app_label in settings.INSTALLED_APPS: + for app_label in self.APP_LABELS: migrate_app_mock.assert_any_call(app_label, 'default', verbosity=3) def test_handle_app_and_database(self, migrate_app_mock):