Skip to content

Commit

Permalink
Fixed issue when INSTALLED_APPS settings contains AppConfig classes i…
Browse files Browse the repository at this point in the history
…nstead of module paths (#35)

Fixed issue when INSTALLED_APPS settings contains AppConfig classes instead of module paths
  • Loading branch information
M1ha-Shvn authored Oct 1, 2021
1 parent 0a7f0c1 commit 12069db
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
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
Expand All @@ -25,10 +25,10 @@ def add_arguments(self, parser: CommandParser) -> None:

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 {}

Expand Down
4 changes: 1 addition & 3 deletions src/django_clickhouse/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions src/django_clickhouse/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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
Expand Down Expand Up @@ -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__'):
Expand Down
6 changes: 6 additions & 0 deletions tests/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class UnitTestAppConfig(AppConfig):
name = 'tests'
verbose_name = "Unit test app"
4 changes: 3 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@

INSTALLED_APPS = [
"src",
"tests"

# This app is included with config in order to test all is working fine here
"tests.apps.UnitTestAppConfig"
]

CLICKHOUSE_DATABASES = {
Expand Down
8 changes: 5 additions & 3 deletions tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ def test_readonly_connections(self):
@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):
Expand All @@ -85,7 +87,7 @@ def test_handle_database(self, migrate_app_mock):
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):
Expand Down

0 comments on commit 12069db

Please sign in to comment.