475 | | One of the challenges that can be encountered is figuring out whether |
476 | | or not to run a migration when using multiple databases. |
477 | | |
478 | | For example, you may want to **only** run a migration against a |
479 | | particular database. |
480 | | |
481 | | In order to do that you can filter inside of your data migration |
482 | | by looking at the ``schema_editor.connection.alias`` attribute. |
483 | | However it is better to have your ``dbrouter`` leverage the same |
484 | | ``allow_migrate`` call by making the following method in your |
485 | | router:: |
486 | | |
487 | | class MyRouter(object): |
488 | | |
489 | | def allow_migrate(self, db, model): |
490 | | # your typical migration code here |
491 | | |
492 | | def runpython_ok(self, apps, schema_editor, model): |
493 | | db = schema_editor.connection.alias |
494 | | if self.allow_migrate(db, model): |
495 | | return True |
496 | | return False |
497 | | |
498 | | Then to leverage this in your migrations, do the following:: |
499 | | |
500 | | # -*- coding: utf-8 -*- |
501 | | from __future__ import unicode_literals |
502 | | |
503 | | from django.db import models, migrations |
504 | | from myappname.dbrouter import MyRouter |
505 | | import sys |
506 | | |
507 | | def forwards(apps, schema_editor): |
508 | | # only run this for the correct databases |
509 | | MyModel = apps.get_model("myappname", "MyModel") |
510 | | if not MyRouter().runpython_ok(apps, schema_editor, MyModel): |
511 | | return |
512 | | |
513 | | """ |
514 | | This is a global configuration model so there should be only |
515 | | row. |
516 | | Get the first one -or- create the initial one. |
517 | | Only set the default ports if it has not been set by user |
518 | | already. |
519 | | """ |
520 | | try: |
521 | | my_model = MyModel.objects.order_by("-id").first() |
522 | | except IndexError: |
523 | | my_model = MyModel.objects.create() |
524 | | if my_model.my_model_ports == "": |
525 | | my_model.my_model_ports = "80, 8080, 3128" |
526 | | my_model.save() |
527 | | |
528 | | def backwards(apps, schema_editor): |
529 | | pass |
530 | | |
531 | | class Migration(migrations.Migration): |
532 | | |
533 | | dependencies = [ |
534 | | # Dependencies to other migrations |
535 | | ] |
536 | | |
537 | | operations = [ |
538 | | migrations.RunPython(forwards, backwards), |
539 | | ] |
| 475 | One of the challenges that can be encountered is figuring out whether or not to |
| 476 | run a migration when using multiple databases. |
| 477 | |
| 478 | For example, you may want to **only** run a migration against a particular |
| 479 | database. |
| 480 | |
| 481 | In order to do that you can check the connection alias inside a ``RunPython`` |
| 482 | forwards and backwards operation by looking at the |
| 483 | ``schema_editor.connection.alias`` attribute. |
| 484 | |
| 485 | # -*- coding: utf-8 -*- |
| 486 | from __future__ import unicode_literals |
| 487 | |
| 488 | from django.db import migrations |
| 489 | |
| 490 | def forwards(apps, schema_editor): |
| 491 | if not schema_editor.connection.alias == 'master-db': |
| 492 | return |
| 493 | # Your migration code goes here |
| 494 | |
| 495 | class Migration(migrations.Migration): |
| 496 | |
| 497 | dependencies = [ |
| 498 | # Dependencies to other migrations |
| 499 | ] |
| 500 | |
| 501 | operations = [ |
| 502 | migrations.RunPython(forwards), |
| 503 | ] |
| 504 | |
| 505 | You can also use your database router's ``allow_migrate`` method, but keep in |
| 506 | mind that the imported router needs to stay around as long as it is referenced |
| 507 | inside a migration: |
| 508 | |
| 509 | .. snippet:: |
| 510 | :filename: myapp/dbrouters.py |
| 511 | |
| 512 | class MyRouter(object): |
| 513 | |
| 514 | def allow_migrate(self, db, model): |
| 515 | return db == 'default': |
| 516 | |
| 517 | Then, to leverage this in your migrations, do the following:: |
| 518 | |
| 519 | # -*- coding: utf-8 -*- |
| 520 | from __future__ import unicode_literals |
| 521 | |
| 522 | from django.db import models, migrations |
| 523 | |
| 524 | from myappname.dbrouters import MyRouter |
| 525 | |
| 526 | def forwards(apps, schema_editor): |
| 527 | MyModel = apps.get_model("myappname", "MyModel") |
| 528 | if not MyRouter().allow_migrate(schema_editor.connection.alias, MyModel): |
| 529 | return |
| 530 | # Your migration code goes here |
| 531 | |
| 532 | class Migration(migrations.Migration): |
| 533 | |
| 534 | dependencies = [ |
| 535 | # Dependencies to other migrations |
| 536 | ] |
| 537 | |
| 538 | operations = [ |
| 539 | migrations.RunPython(forwards), |
| 540 | ] |