Changed migrations routing, dividing it into replicated and not replicated operations

This commit is contained in:
M1ha 2018-12-18 15:45:10 +05:00
parent 2126bc25da
commit 2d4c8554cd
5 changed files with 15 additions and 7 deletions

View File

@ -47,7 +47,8 @@ class ClickHouseModel(with_metaclass(ClickHouseModelMeta, InfiModel)):
read_db_aliases = (config.DEFAULT_DB_ALIAS,)
write_db_aliases = (config.DEFAULT_DB_ALIAS,)
migrate_db_aliases = write_db_aliases
migrate_replicated_db_aliases = (config.DEFAULT_DB_ALIAS,)
migrate_non_replicated_db_aliases = (config.DEFAULT_DB_ALIAS,)
sync_enabled = False
sync_batch_size = None

View File

@ -34,7 +34,7 @@ class Migration:
model_class = getattr(op, 'model_class', None)
hints = getattr(op, 'hints', {})
if db_router.allow_migrate(db_alias, self.__module__, model=model_class, **hints):
if db_router.allow_migrate(db_alias, self.__module__, op, model=model_class, **hints):
database = database or connections[db_alias]
op.apply(database)

View File

@ -5,6 +5,7 @@ from typing import Optional
import random
import six
from infi.clickhouse_orm.migrations import Operation, DropTable, CreateTable
from .clickhouse_models import ClickHouseModel
from .configuration import config
@ -32,12 +33,13 @@ class DefaultRouter:
"""
return random.choice(model.write_db_aliases)
def allow_migrate(self, db_alias, app_label, model=None, **hints):
# type: (str, str, Optional[ClickHouseModel], **dict) -> bool
def allow_migrate(self, db_alias, app_label, operation, model=None, **hints):
# type: (str, str, Operation, Optional[ClickHouseModel], **dict) -> bool
"""
Checks if migration can be applied to given database
:param db_alias: Database alias to check
:param app_label: App from which migration is got
:param operation: Operation object to perform
:param model: Model migration is applied to
:param hints: Hints to make correct decision
:return: boolean
@ -50,4 +52,8 @@ class DefaultRouter:
if isinstance(hints['model'], six.string_types) else hints['model']
model = lazy_class_import(model)
return db_alias in model.migrate_db_aliases
if operation.__class__ not in {CreateTable, DropTable}:
return db_alias in model.migrate_replicated_db_aliases
else:
return db_alias in model.migrate_non_replicated_db_aliases

View File

@ -15,7 +15,8 @@ class ClickHouseTestModel(ClickHouseModel):
value = fields.Int32Field()
engine = ReplacingMergeTree('created_date', ('id',))
migrate_db_aliases = ('default', 'secondary')
migrate_replicated_db_aliases = ('default', 'secondary')
migrate_non_replicated_db_aliases = ('default', 'secondary')
class ClickHouseCollapseTestModel(ClickHouseModel):

View File

@ -8,7 +8,7 @@ from tests.clickhouse_models import ClickHouseTestModel
class NoMigrateRouter(DefaultRouter):
def allow_migrate(self, db_alias, app_label, model=None, **hints):
def allow_migrate(self, db_alias, app_label, operation, model=None, **hints):
return False