Ticket #23749: patch

File patch, 5.3 KB (added by Markus Holtermann, 10 years ago)
  • docs/ref/schema-editor.txt

    diff --git a/docs/ref/schema-editor.txt b/docs/ref/schema-editor.txt
    index 54dd3bf..c503296 100644
    a b below.  
    158158
    159159.. attribute:: SchemaEditor.connection
    160160
    161 A connection object to the database. A useful attribute of the
    162 connection is ``alias`` which can be used to determine the name of
    163 the database being accessed.
     161A connection object to the database. A useful attribute of the connection is
     162``alias`` which can be used to determine the name of the database being
     163accessed.
    164164
    165 This in turn is useful when doing data migrations for
    166 :ref:`migrations with multiple database backends <data-migrations-and-multiple-databases>`.
     165This in turn is useful when doing data migrations for :ref:`migrations with
     166multiple database backends <data-migrations-and-multiple-databases>`.
  • docs/topics/migrations.txt

    diff --git a/docs/topics/migrations.txt b/docs/topics/migrations.txt
    index 746f4d2..a743bf2 100644
    a b backwards will raise an exception.  
    472472Data migrations and multiple databases
    473473~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    474474
    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             ]
     475One of the challenges that can be encountered is figuring out whether or not to
     476run a migration when using multiple databases.
     477
     478For example, you may want to **only** run a migration against a particular
     479database.
     480
     481In order to do that you can check the connection alias inside a ``RunPython``
     482forwards 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
     505You can also use your database router's ``allow_migrate`` method, but keep in
     506mind that the imported router needs to stay around as long as it is referenced
     507inside 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
     517Then, 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        ]
    540541
    541542More advanced migrations
    542543~~~~~~~~~~~~~~~~~~~~~~~~
Back to Top