Created
May 6, 2022 12:38
-
-
Save paudirac/090b731143042dfb48395c4ec72a396f to your computer and use it in GitHub Desktop.
django migrations multiple databases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LegacyRouter: | |
route_app_labels = {'legacy',} | |
legacy_database = 'legacy' | |
def db_for_read(self, model, **hints): | |
if model._meta.app_label in self.route_app_labels: | |
return self.legacy_database | |
return None | |
def db_for_write(self, model, **hints): | |
if model._meta.app_label in self.route_app_labels: | |
return self.legacy_database | |
return None | |
def allow_relation(self, obj1, obj2, **hints): | |
return None | |
def allow_migrate(self, db, app_label, model_name=None, **hints): | |
is_legacy_db = db == self.legacy_database | |
is_legacy_app = app_label in self.route_app_labels | |
if is_legacy_db: | |
return is_legacy_app | |
else: | |
return not is_legacy_app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATABASES = { | |
'default': { | |
'ENGINE': 'django.db.backends.mysql', | |
'NAME': 'producto', | |
}, | |
'legacy': { | |
'ENGINE': 'django.db.backends.mysql', | |
'NAME': 'legacy-schema', | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment