Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
db3f7c15
Kaydet (Commit)
db3f7c15
authored
Ock 01, 2015
tarafından
Alfred Perlstein
Kaydeden (comit)
Tim Graham
Ock 03, 2015
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #23749 -- Documented how to use the database alias in RunPython.
Thanks Markus Holtermann for review and feedback.
üst
b7381788
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
0 deletions
+84
-0
schema-editor.txt
docs/ref/schema-editor.txt
+17
-0
migrations.txt
docs/topics/migrations.txt
+67
-0
No files found.
docs/ref/schema-editor.txt
Dosyayı görüntüle @
db3f7c15
...
@@ -149,3 +149,20 @@ If the database has the ``supports_combined_alters``, Django will try and
...
@@ -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
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
issue a separate ALTER statement for each change, but will not issue ALTERs
where no change is required (as South often did).
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>`.
docs/topics/migrations.txt
Dosyayı görüntüle @
db3f7c15
...
@@ -467,6 +467,73 @@ You can pass a second callable to
...
@@ -467,6 +467,73 @@ You can pass a second callable to
want executed when migrating backwards. If this callable is omitted, migrating
want executed when migrating backwards. If this callable is omitted, migrating
backwards will raise an exception.
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
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
to be able to write your own, see the :doc:`migration operations reference
</ref/migration-operations>`.
</ref/migration-operations>`.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment