Database migrations¶
Typically a web app would need interact with one or more databases. Also, in a Django app, this interaction typically happens through an ORM. Each ORM, depending on the databases' it supports, could define its own migration strategy to apply data model changes.
Since this package could potentially interact with multiple ORMs' through their respective manager
classes,
it becomes essential for the manager
class to know the migration strategy that the particular ORM uses.
Also it could be that an ORM does not have a defined migration strategy out of the box, in which case the app which uses that ORM would have to define one.
Among the ORM libraries that the package supports, only django_orm
has a well defined migration
strategy. For the others, the app would have to implement a migration_asst
class and register it with the
respective manager
class.
Running migrations¶
The below snippet should be invoked from manage.py shell
from tenant_router.utils import migrate_to_all
migrate_to_all()
The following is a convenient one-liner to execute from shell
$ python manage.py shell -c "from tenant_router.utils import migrate_to_all; migrate_to_all()"
Warning
Running the python manage.py migrate
sub-command would no longer work as
this is tied to only the django_orm
. Other ORMs' would get left out from the
migration process.
Defining a custom MIGRATION_ASST
class¶
The below snippet illustrates how to write a custom migration_asst
class and register
it with the respective orm manager
class.
## Example of a custom 'migration_asst' class
from tenant_router.orm_backends.base import BaseOrmMigrationAsst
class CustomMigrationAsst(BaseOrmMigrationAsst):
def perform_migrate(self):
# Called by the 'manager' class internally when
# 'migrate_to_all' is called. Should implement the
# strategy for migrating to all databases supported
# by this ORM.
pass
## Registering the class with a 'manager' in 'settings.py'
TENANT_ROUTER_ORM_SETTINGS = {
'django_orm': {
'MANAGER': 'tenant_router.orm_backends.django_orm.manager.DjangoOrmManager',
'SETTINGS_KEY': 'DATABASES',
'OPTIONS': {
'MIGRATION_ASST': {
# Mandatory
'CLASS': 'path.to.CustomMigrationAsst',
# Optional
# Any additional kwargs to be passed to the class
'OPTIONS': {}
}
}
}
}