bernardopires / django-tenant-schemas

Tenant support for Django using PostgreSQL schemas.
https://django-tenant-schemas.readthedocs.org/en/latest/
MIT License
1.45k stars 424 forks source link

NoMigrations exception thrown when creating a new tenant #250

Open aldon-cmd opened 9 years ago

aldon-cmd commented 9 years ago

I have traced an AttributeError exception to the _clear_south_cache() function inside migrate_schemas.py. The exception is thrown because my catalogue and reviews apps are duplicated in the generator returned by migration.all_migrations().I am not sure why they are duplicated while other apps possessing migrations are not. Hence, the function will eventually try to delete an attribute which does not exist. The exception is only thrown when trying to create a tenant schema which happens after calling tenant.save() in python's terminal. After handling that exception a NoMigrations exception is thrown by south even though the migrations folder exists with a init file inside it. You can see a traceback of the NoMigrations exception here https://gist.github.com/wiel/8ab644307bb54c6d77d0.

I am able to sync the public schema without any problems and the AttributeError exception was hidden by this exception https://gist.github.com/wiel/c47335e181eb15b4f1b2

#handling the AttributeError exception
#tenant_schemas\management\commands\legacy\migrate_schemas.py
    def _clear_south_cache(self):  
        for mig in list(migration.all_migrations()):
            try:
                delattr(mig._application, "migrations")
            except AttributeError:
                pass
        Migrations._clear_cache()
SHARED_APPS = (
    'tenant_schemas',  # mandatory
    'custom',
    'compressor',
    'apps.help',
    'south',
    'endless_pagination',

    'oscar',
    'apps.address',
    'apps.shipping',
    'apps.customer',

    'haystack',
    'treebeard',
    'sorl.thumbnail',

    'apps.analytics',
    'apps.checkout',
    'apps.offer',
    'apps.order',
    'apps.partner',
    'apps.payment',
    'apps.wishlists', 
    'apps.promotions',
    'apps.search',   
    'apps.basket',
    'apps.catalogue',
    'apps.dashboard',
    'oscar.apps.catalogue.reviews',
    'oscar.apps.voucher',
    'oscar.apps.dashboard.reports',
    'oscar.apps.dashboard.users',
    'oscar.apps.dashboard.orders',
    'oscar.apps.dashboard.promotions',
    'oscar.apps.dashboard.catalogue',
    'oscar.apps.dashboard.offers',
    'oscar.apps.dashboard.partners',
    'oscar.apps.dashboard.pages',
    'oscar.apps.dashboard.ranges',
    'oscar.apps.dashboard.reviews',
    'oscar.apps.dashboard.vouchers',
    'oscar.apps.dashboard.communications',

   'django.contrib.contenttypes',
   'django.contrib.auth',

    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'django.contrib.staticfiles',
    'django.contrib.admindocs',
    'django.contrib.flatpages',
) 

TENANT_APPS = (

    'django.contrib.contenttypes',
    'django.contrib.auth',
    'south',
    'apps.analytics',
    'apps.checkout',
    'apps.offer',
    'apps.order',
    'apps.partner',
    'apps.payment',
    'apps.wishlists', 
    'apps.promotions',
    'apps.search',   
    'apps.basket',
    'apps.catalogue',
    'apps.dashboard',
    'oscar.apps.catalogue.reviews',
    'oscar.apps.voucher',
    'oscar.apps.dashboard.reports',
    'oscar.apps.dashboard.users',
    'oscar.apps.dashboard.orders',
    'oscar.apps.dashboard.promotions',
    'oscar.apps.dashboard.catalogue',
    'oscar.apps.dashboard.offers',
    'oscar.apps.dashboard.partners',
    'oscar.apps.dashboard.pages',
    'oscar.apps.dashboard.ranges',
    'oscar.apps.dashboard.reviews',
    'oscar.apps.dashboard.vouchers',
    'oscar.apps.dashboard.communications',
) 

INSTALLED_APPS = SHARED_APPS + TENANT_APPS + ('tenant_schemas',)

TENANT_MODEL = "custom.Client"
aldon-cmd commented 8 years ago

i got around this issue by catching the AttributeError exception and then doing nothing to handle it

#/tenant_schemas/management/commands/legacy/migration_schemas.py
    def _clear_south_cache(self):
        for mig in list(migration.all_migrations()):
            try:
                delattr(mig._application, "migrations")
            except AttributeError:
                pass
        Migrations._clear_cache()
rodolfomartinez commented 7 years ago

Is this is a good enough solution for prod?

goodtune commented 7 years ago

This was dropped in v1.6.1 (Django < 1.8 and therefore South support was dropped in v1.6.0; see #342).