Changes between Initial Version and Version 1 of Ticket #23749, comment 20


Ignore:
Timestamp:
Jan 2, 2015, 4:12:39 PM (10 years ago)
Author:
Markus Holtermann

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #23749, comment 20

    initial v1  
    1 {{{#!diff
    2 diff --git a/docs/ref/schema-editor.txt b/docs/ref/schema-editor.txt
    3 index 54dd3bf..c503296 100644
    4 --- a/docs/ref/schema-editor.txt
    5 +++ b/docs/ref/schema-editor.txt
    6 @@ -158,9 +158,9 @@ below.
    7  
    8  .. attribute:: SchemaEditor.connection
    9  
    10 -A connection object to the database. A useful attribute of the
    11 -connection is ``alias`` which can be used to determine the name of
    12 -the database being accessed.
    13 +A connection object to the database. A useful attribute of the connection is
    14 +``alias`` which can be used to determine the name of the database being
    15 +accessed.
    16  
    17 -This in turn is useful when doing data migrations for
    18 -:ref:`migrations with multiple database backends <data-migrations-and-multiple-databases>`.
    19 +This in turn is useful when doing data migrations for :ref:`migrations with
    20 +multiple database backends <data-migrations-and-multiple-databases>`.
    21 diff --git a/docs/topics/migrations.txt b/docs/topics/migrations.txt
    22 index 746f4d2..a743bf2 100644
    23 --- a/docs/topics/migrations.txt
    24 +++ b/docs/topics/migrations.txt
    25 @@ -472,71 +472,72 @@ backwards will raise an exception.
    26  Data migrations and multiple databases
    27  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    28  
    29 -One of the challenges that can be encountered is figuring out whether
    30 -or not to run a migration when using multiple databases.
    31 -
    32 -For example, you may want to **only** run a migration against a
    33 -particular database.
    34 -
    35 -In order to do that you can filter inside of your data migration
    36 -by looking at the ``schema_editor.connection.alias`` attribute.
    37 -However it is better to have your ``dbrouter`` leverage the same
    38 -``allow_migrate`` call by making the following method in your
    39 -router::
    40 -
    41 -        class MyRouter(object):
    42 -
    43 -            def allow_migrate(self, db, model):
    44 -                # your typical migration code here
    45 -
    46 -            def runpython_ok(self, apps, schema_editor, model):
    47 -                db = schema_editor.connection.alias
    48 -                if self.allow_migrate(db, model):
    49 -                    return True
    50 -                return False
    51 -
    52 -Then to leverage this in your migrations, do the following::
    53 -
    54 -        # -*- coding: utf-8 -*-
    55 -        from __future__ import unicode_literals
    56 -
    57 -        from django.db import models, migrations
    58 -        from myappname.dbrouter import MyRouter
    59 -        import sys
    60 -
    61 -        def forwards(apps, schema_editor):
    62 -            # only run this for the correct databases
    63 -            MyModel = apps.get_model("myappname", "MyModel")
    64 -            if not MyRouter().runpython_ok(apps, schema_editor, MyModel):
    65 -                return
    66 -
    67 -            """
    68 -            This is a global configuration model so there should be only
    69 -            row.
    70 -            Get the first one -or- create the initial one.
    71 -            Only set the default ports if it has not been set by user
    72 -            already.
    73 -            """
    74 -            try:
    75 -                my_model = MyModel.objects.order_by("-id").first()
    76 -            except IndexError:
    77 -                my_model = MyModel.objects.create()
    78 -            if my_model.my_model_ports == "":
    79 -                my_model.my_model_ports = "80, 8080, 3128"
    80 -                my_model.save()
    81 -
    82 -        def backwards(apps, schema_editor):
    83 -            pass
    84 -
    85 -        class Migration(migrations.Migration):
    86 -
    87 -            dependencies = [
    88 -                # Dependencies to other migrations
    89 -            ]
    90 -
    91 -            operations = [
    92 -                migrations.RunPython(forwards, backwards),
    93 -            ]
    94 +One of the challenges that can be encountered is figuring out whether or not to
    95 +run a migration when using multiple databases.
    96 +
    97 +For example, you may want to **only** run a migration against a particular
    98 +database.
    99 +
    100 +In order to do that you can check the connection alias inside a ``RunPython``
    101 +forwards and backwards operation by looking at the
    102 +``schema_editor.connection.alias`` attribute.
    103 +
    104 +    # -*- coding: utf-8 -*-
    105 +    from __future__ import unicode_literals
    106 +
    107 +    from django.db import migrations
    108 +
    109 +    def forwards(apps, schema_editor):
    110 +        if not schema_editor.connection.alias == 'master-db':
    111 +            return
    112 +        # Your migration code goes here
    113 +
    114 +    class Migration(migrations.Migration):
    115 +
    116 +        dependencies = [
    117 +            # Dependencies to other migrations
    118 +        ]
    119 +
    120 +        operations = [
    121 +            migrations.RunPython(forwards),
    122 +        ]
    123 +
    124 +You can also use your database router's ``allow_migrate`` method, but keep in
    125 +mind that the imported router needs to stay around as long as it is referenced
    126 +inside a migration:
    127 +
    128 +.. snippet::
    129 +    :filename: myapp/dbrouters.py
    130 +
    131 +    class MyRouter(object):
    132 +
    133 +        def allow_migrate(self, db, model):
    134 +            return db == 'default':
    135 +
    136 +Then, to leverage this in your migrations, do the following::
    137 +
    138 +    # -*- coding: utf-8 -*-
    139 +    from __future__ import unicode_literals
    140 +
    141 +    from django.db import models, migrations
    142 +
    143 +    from myappname.dbrouters import MyRouter
    144 +
    145 +    def forwards(apps, schema_editor):
    146 +        MyModel = apps.get_model("myappname", "MyModel")
    147 +        if not MyRouter().allow_migrate(schema_editor.connection.alias, MyModel):
    148 +            return
    149 +        # Your migration code goes here
    150 +
    151 +    class Migration(migrations.Migration):
    152 +
    153 +        dependencies = [
    154 +            # Dependencies to other migrations
    155 +        ]
    156 +
    157 +        operations = [
    158 +            migrations.RunPython(forwards),
    159 +        ]
    160 
    161  More advanced migrations
    162  ~~~~~~~~~~~~~~~~~~~~~~~~
    163 }}}
     1Some suggested changes.
Back to Top