From 5455d6d9b47d130f2717fd496a1561d3307853cb Mon Sep 17 00:00:00 2001 From: M1ha Date: Thu, 22 Nov 2018 16:59:39 +0500 Subject: [PATCH] Fixed bug in migrations, if migrations directory doesn't exist in module --- src/django_clickhouse/migrations.py | 27 ++++++++++++++------------- src/django_clickhouse/utils.py | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/django_clickhouse/migrations.py b/src/django_clickhouse/migrations.py index e9a274f..1f2dbe4 100644 --- a/src/django_clickhouse/migrations.py +++ b/src/django_clickhouse/migrations.py @@ -10,7 +10,7 @@ from django.db import DEFAULT_DB_ALIAS as DJANGO_DEFAULT_DB_ALIAS from infi.clickhouse_orm.migrations import MigrationHistory from infi.clickhouse_orm.utils import import_submodules -from django_clickhouse.utils import lazy_class_import +from django_clickhouse.utils import lazy_class_import, module_exists from .configuration import config from .database import connections @@ -48,22 +48,23 @@ def migrate_app(app_label, db_alias, up_to=9999): """ db = connections[db_alias] migrations_package = "%s.%s" % (app_label, config.MIGRATIONS_PACKAGE) - - applied_migrations = db._get_applied_migrations(migrations_package) - modules = import_submodules(migrations_package) - unapplied_migrations = set(modules.keys()) - applied_migrations + if module_exists(migrations_package): + applied_migrations = db._get_applied_migrations(migrations_package) + modules = import_submodules(migrations_package) - for name in sorted(unapplied_migrations): - migration = modules[name].Migration() - migration.apply(db_alias) + unapplied_migrations = set(modules.keys()) - applied_migrations - db.insert([ - MigrationHistory(package_name=migrations_package, module_name=name, applied=datetime.date.today()) - ]) + for name in sorted(unapplied_migrations): + migration = modules[name].Migration() + migration.apply(db_alias) - if int(name[:4]) >= up_to: - break + db.insert([ + MigrationHistory(package_name=migrations_package, module_name=name, applied=datetime.date.today()) + ]) + + if int(name[:4]) >= up_to: + break @receiver(post_migrate) diff --git a/src/django_clickhouse/utils.py b/src/django_clickhouse/utils.py index 8710fc7..93077ad 100644 --- a/src/django_clickhouse/utils.py +++ b/src/django_clickhouse/utils.py @@ -4,8 +4,7 @@ from typing import Union, Any, Optional, TypeVar, Set import pytz import six from importlib import import_module - -from infi.clickhouse_orm.database import Database +from importlib.util import find_spec from .database import connections @@ -53,6 +52,17 @@ def format_datetime(dt, timezone_offset=0, day_end=False, db_alias=None): return server_dt.strftime("%Y-%m-%d %H:%M:%S") +def module_exists(module_name): # type: (str) -> bool + """ + Checks if moudle exists + :param module_name: Dot-separated module name + :return: Boolean + """ + # Python 3.4+ + spam_spec = find_spec(module_name) + return spam_spec is not None + + def lazy_class_import(obj): # type: (Union[str, Any]) -> Any """ If string is given, imports object by given module path.