summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2015-02-03 13:09:54 -0500
committerTim Graham <timograham@gmail.com>2015-02-03 13:09:54 -0500
commit570912a97d5051fa3aeacd9d16c3be9afcf92198 (patch)
tree645cd0e293de1e5b5e2f6f78152c2feee7a9661e /docs
parent66f5aa9fa5d53ddd7fbdb7ddac39c429f0c1b4fd (diff)
Added a "Writing migrations" how-to.
Diffstat (limited to 'docs')
-rw-r--r--docs/howto/index.txt1
-rw-r--r--docs/howto/writing-migrations.txt69
-rw-r--r--docs/index.txt3
-rw-r--r--docs/topics/migrations.txt69
4 files changed, 78 insertions, 64 deletions
diff --git a/docs/howto/index.txt b/docs/howto/index.txt
index 091afe69bc..f06cd6b627 100644
--- a/docs/howto/index.txt
+++ b/docs/howto/index.txt
@@ -26,6 +26,7 @@ you quickly accomplish common tasks.
static-files/index
static-files/deployment
windows
+ writing-migrations
.. seealso::
diff --git a/docs/howto/writing-migrations.txt b/docs/howto/writing-migrations.txt
new file mode 100644
index 0000000000..7edea84329
--- /dev/null
+++ b/docs/howto/writing-migrations.txt
@@ -0,0 +1,69 @@
+===========================
+Writing database migrations
+===========================
+
+This document explains how to structure and write database migrations for
+different scenarios you might encounter. For introductory material on
+migrations, see :doc:`the topic guide </topics/migrations>`.
+
+.. _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),
+ ]
+
+.. versionadded:: 1.8
+
+You can also provide hints that will be passed to the :meth:`allow_migrate()`
+method of database routers as ``**hints``:
+
+.. snippet::
+ :filename: myapp/dbrouters.py
+
+ class MyRouter(object):
+
+ def allow_migrate(self, db, model, **hints):
+ if 'target_db' in hints:
+ return db == hints['target_db']
+ return True
+
+Then, to leverage this in your migrations, do the following::
+
+ from django.db import migrations
+
+ def forwards(apps, schema_editor):
+ # Your migration code goes here
+
+ class Migration(migrations.Migration):
+
+ dependencies = [
+ # Dependencies to other migrations
+ ]
+
+ operations = [
+ migrations.RunPython(forwards, hints={'target_db': 'default'}),
+ ]
diff --git a/docs/index.txt b/docs/index.txt
index c3a91c48a8..814b93418d 100644
--- a/docs/index.txt
+++ b/docs/index.txt
@@ -76,7 +76,8 @@ manipulating the data of your Web application. Learn more about it below:
* **Migrations:**
:doc:`Introduction to Migrations<topics/migrations>` |
:doc:`Operations reference <ref/migration-operations>` |
- :doc:`SchemaEditor <ref/schema-editor>`
+ :doc:`SchemaEditor <ref/schema-editor>` |
+ :doc:`Writing migrations <howto/writing-migrations>`
* **Advanced:**
:doc:`Managers <topics/db/managers>` |
diff --git a/docs/topics/migrations.txt b/docs/topics/migrations.txt
index 6896a34065..a0dc365e6d 100644
--- a/docs/topics/migrations.txt
+++ b/docs/topics/migrations.txt
@@ -476,74 +476,13 @@ 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),
- ]
-
-.. versionadded:: 1.8
-
-You can also provide hints that will be passed to the :meth:`allow_migrate()`
-method of database routers as ``**hints``:
-
-.. snippet::
- :filename: myapp/dbrouters.py
-
- class MyRouter(object):
-
- def allow_migrate(self, db, model, **hints):
- if 'target_db' in hints:
- return db == hints['target_db']
- return True
-
-Then, to leverage this in your migrations, do the following::
-
- from django.db import migrations
-
- def forwards(apps, schema_editor):
- # Your migration code goes here
-
- class Migration(migrations.Migration):
-
- dependencies = [
- # Dependencies to other migrations
- ]
-
- operations = [
- migrations.RunPython(forwards, hints={'target_db': 'default'}),
- ]
-
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>`.
+</ref/migration-operations>` and the "how-to" on :doc:`writing migrations
+</howto/writing-migrations>`.
.. _migration-squashing:
@@ -806,3 +745,7 @@ More information is available in the
:doc:`The Migrations Operations Reference </ref/migration-operations>`
Covers the schema operations API, special operations, and writing your
own operations.
+
+ :doc:`The Writing Migrations "how-to" </howto/writing-migrations>`
+ Explains how to structure and write database migrations for different
+ scenarios you might encounter.