summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlfred Perlstein <alfred@freebsd.org>2014-12-31 19:16:51 -0800
committerTim Graham <timograham@gmail.com>2015-01-03 12:07:28 -0500
commit01487684123342f052e139291175f4ea3d11a09b (patch)
tree4b0945e2b00a8d2fba1e7d179a4a5ea96959a9a1
parent5f8761c6396f0d27b8948f55700ec176fedbf5d5 (diff)
[1.7.x] Fixed #23749 -- Documented how to use the database alias in RunPython.
Thanks Markus Holtermann for review and feedback. Backport of db3f7c15cbf4c9025e83065d1302d0e61d570331 from master
-rw-r--r--docs/ref/schema-editor.txt17
-rwxr-xr-xdocs/topics/migrations.txt67
2 files changed, 84 insertions, 0 deletions
diff --git a/docs/ref/schema-editor.txt b/docs/ref/schema-editor.txt
index e4d3fa01c0..94456d060e 100644
--- a/docs/ref/schema-editor.txt
+++ b/docs/ref/schema-editor.txt
@@ -149,3 +149,20 @@ If the database has the ``supports_combined_alters``, Django will try and
do as many of these in a single database call as possible; otherwise, it will
issue a separate ALTER statement for each change, but will not issue ALTERs
where no change is required (as South often did).
+
+Attributes
+==========
+
+All attributes should be considered read-only unless stated otherwise.
+
+connection
+----------
+
+.. attribute:: SchemaEditor.connection
+
+A connection object to the database. A useful attribute of the connection is
+``alias`` which can be used to determine the name of the database being
+accessed.
+
+This is useful when doing data migrations for :ref:`migrations with multiple
+databases <data-migrations-and-multiple-databases>`.
diff --git a/docs/topics/migrations.txt b/docs/topics/migrations.txt
index 7594cdf11d..308ed3f703 100755
--- a/docs/topics/migrations.txt
+++ b/docs/topics/migrations.txt
@@ -435,6 +435,73 @@ You can pass a second callable to
want executed when migrating backwards. If this callable is omitted, migrating
backwards will raise an exception.
+.. _data-migrations-and-multiple-databases:
+
+Data migrations and multiple databases
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+When using multiple databases, you may need to figure out whether or not to
+run a migration against a particular database. For example, you may want to
+**only** run a migration on a particular database.
+
+In order to do that you can check the database connection's alias inside a
+``RunPython`` operation by looking at the ``schema_editor.connection.alias``
+attribute::
+
+ from django.db import migrations
+
+ def forwards(apps, schema_editor):
+ if not schema_editor.connection.alias == 'default':
+ return
+ # Your migration code goes here
+
+ class Migration(migrations.Migration):
+
+ dependencies = [
+ # Dependencies to other migrations
+ ]
+
+ operations = [
+ migrations.RunPython(forwards),
+ ]
+
+You can also use your database router's ``allow_migrate()`` method, but keep in
+mind that the imported router needs to stay around as long as it is referenced
+inside a migration:
+
+.. snippet::
+ :filename: myapp/dbrouters.py
+
+ class MyRouter(object):
+
+ def allow_migrate(self, db, model):
+ return db == 'default'
+
+Then, to leverage this in your migrations, do the following::
+
+ from django.db import migrations
+
+ from myappname.dbrouters import MyRouter
+
+ def forwards(apps, schema_editor):
+ MyModel = apps.get_model("myappname", "MyModel")
+ if not MyRouter().allow_migrate(schema_editor.connection.alias, MyModel):
+ return
+ # Your migration code goes here
+
+ class Migration(migrations.Migration):
+
+ dependencies = [
+ # Dependencies to other migrations
+ ]
+
+ operations = [
+ migrations.RunPython(forwards),
+ ]
+
+More advanced migrations
+~~~~~~~~~~~~~~~~~~~~~~~~
+
If you're interested in the more advanced migration operations, or want
to be able to write your own, see the :doc:`migration operations reference
</ref/migration-operations>`.