Skip to content

post_migrate_handler #208

Closed
Closed
@zN3utr4l

Description

@zN3utr4l

Python version
3.8

Django version
3.2.16

Package version
0.22.2

Current behavior (bug description)
The current behavior of the post-migration signal (post_migrate_handler) does not take into account any routing of the databases

from django.conf import settings


class DatabaseAppsRouter(object):
    """

        arouter to control all database operations on models for different
        databases.

        in case an app is not set in settings.DATABASE_APPS_MAPPING, the router
        will fallback to the `default` database.

        Settings example:

        DATABASE_APPS_MAPPING = {'app1': 'db1', 'app2': 'db2'}
    """

    def db_for_read(self, model, **hints):
        """Point all read operations to the specific database"""
        if model._meta.app_label in settings.DATABASE_APPS_MAPPING:
            return settings.DATABASE_APPS_MAPPING[model._meta.app_label]

        return None

    def db_for_write(self, model, **hints):
        """Point all write operations to the specific database"""
        if model._meta.app_label in settings.DATABASE_APPS_MAPPING:
            return settings.DATABASE_APPS_MAPPING[model._meta.app_label]

        return None

    def allow_relation(self, obj1, obj2, **hints):
        """Allow any relation between apps that use the same database"""
        db1 = settings.DATABASE_APPS_MAPPING[obj1._meta.app_label]
        db2 = settings.DATABASE_APPS_MAPPING[obj2._meta.app_label]

        if db1 and db2:
            return db1 == db2

        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """Make sure that apps only appear in the related database"""

        if db in settings.DATABASE_APPS_MAPPING.values():
            return settings.DATABASE_APPS_MAPPING[app_label] == db

        elif app_label in settings.DATABASE_APPS_MAPPING:
            return False

        return None

DATABASES = {
    'default': {
        'ENGINE': 'timescale.db.backends.postgresql',
        'NAME': TIMESCALE_DB_HOST_FORMATTED['Database'],
        'USER': TIMESCALE_DB_HOST_FORMATTED['Username'],
        'PASSWORD': TIMESCALE_DB_HOST_FORMATTED['Password'],
        'HOST': TIMESCALE_DB_HOST_FORMATTED['Host'],
        'PORT': TIMESCALE_DB_PORT,
        'OPTIONS': {
            'client_encoding': 'UTF8',
            # 'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
        }
    },
    'collector': {
        'ENGINE': 'djongo',
        'NAME': 'Database',
        'ENFORCE_SCHEMA': True,
        'CLIENT': {
            'host': MONGO_DB_HOST
        }
    }
}


DATABASE_APPS_MAPPING = {
    'contenttypes': 'default',
    'auth': 'default',
    'admin': 'default',
    'django_cache': 'default',
    'sessions': 'default',
    'staticfiles': 'default',
    'admin_interface': 'default',
    'collector': 'djongo(mongodb)',
    'main': 'default',
}

DATABASE_ROUTERS = ['routers.DatabaseAppsRouter']

Workaround

def apply_theme(sender, **kwargs):
    from django.conf import settings
    from admin_interface.models import Theme
    from admin_interface.cache import del_cached_active_theme

    del_cached_active_theme()
    theme = Theme.get_active_theme('default')
    theme.logo = ....
    theme.favicon = ....
    theme.save()


class MainConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'main'
    verbose_name = 'analyser configuration'

    def ready(self) -> None:
        from admin_interface.models import Theme

        Theme.post_migrate_handler = apply_theme
        if 'admin_interface_theme' in connection.introspection.table_names():
            apply_theme(None)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions