mirror of
https://github.com/carrotquest/django-clickhouse.git
synced 2024-11-22 00:56:37 +03:00
Fixed bug in migrations, if migrations directory doesn't exist in module
This commit is contained in:
parent
ea5d9a7dfd
commit
5455d6d9b4
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue
Block a user