Commit 8d2ac948 authored by Andrew Godwin's avatar Andrew Godwin
Browse files

Fixed #22853: Swapped models are now ignored for migration operations.

parent 91f1b6dc
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
from __future__ import unicode_literals
from django.db import router


class Operation(object):
@@ -97,6 +98,17 @@ class Operation(object):
        """
        return self.references_model(model_name, app_label)

    def allowed_to_migrate(self, connection_alias, model):
        """
        Returns if we're allowed to migrate the model. Checks the router,
        if it's a proxy, and if it's swapped out.
        """
        return (
            router.allow_migrate(connection_alias, model) and
            not model._meta.proxy and
            not model._meta.swapped
        )

    def __repr__(self):
        return "<%s %s%s>" % (
            self.__class__.__name__,
+7 −8
Original line number Diff line number Diff line
from __future__ import unicode_literals

from django.db import router
from django.db.models.fields import NOT_PROVIDED
from django.utils import six
from .base import Operation
@@ -29,7 +28,7 @@ class AddField(Operation):
    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        from_model = from_state.render().get_model(app_label, self.model_name)
        to_model = to_state.render().get_model(app_label, self.model_name)
        if router.allow_migrate(schema_editor.connection.alias, to_model):
        if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
            field = to_model._meta.get_field_by_name(self.name)[0]
            if not self.preserve_default:
                field.default = self.field.default
@@ -42,7 +41,7 @@ class AddField(Operation):

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        from_model = from_state.render().get_model(app_label, self.model_name)
        if router.allow_migrate(schema_editor.connection.alias, from_model):
        if self.allowed_to_migrate(schema_editor.connection.alias, from_model):
            schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])

    def describe(self):
@@ -81,13 +80,13 @@ class RemoveField(Operation):

    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        from_model = from_state.render().get_model(app_label, self.model_name)
        if router.allow_migrate(schema_editor.connection.alias, from_model):
        if self.allowed_to_migrate(schema_editor.connection.alias, from_model):
            schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        from_model = from_state.render().get_model(app_label, self.model_name)
        to_model = to_state.render().get_model(app_label, self.model_name)
        if router.allow_migrate(schema_editor.connection.alias, to_model):
        if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
            schema_editor.add_field(from_model, to_model._meta.get_field_by_name(self.name)[0])

    def describe(self):
@@ -118,7 +117,7 @@ class AlterField(Operation):
    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        from_model = from_state.render().get_model(app_label, self.model_name)
        to_model = to_state.render().get_model(app_label, self.model_name)
        if router.allow_migrate(schema_editor.connection.alias, to_model):
        if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
            from_field = from_model._meta.get_field_by_name(self.name)[0]
            to_field = to_model._meta.get_field_by_name(self.name)[0]
            # If the field is a relatedfield with an unresolved rel.to, just
@@ -170,7 +169,7 @@ class RenameField(Operation):
    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        from_model = from_state.render().get_model(app_label, self.model_name)
        to_model = to_state.render().get_model(app_label, self.model_name)
        if router.allow_migrate(schema_editor.connection.alias, to_model):
        if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
            schema_editor.alter_field(
                from_model,
                from_model._meta.get_field_by_name(self.old_name)[0],
@@ -180,7 +179,7 @@ class RenameField(Operation):
    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        from_model = from_state.render().get_model(app_label, self.model_name)
        to_model = to_state.render().get_model(app_label, self.model_name)
        if router.allow_migrate(schema_editor.connection.alias, to_model):
        if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
            schema_editor.alter_field(
                from_model,
                from_model._meta.get_field_by_name(self.new_name)[0],
+10 −10
Original line number Diff line number Diff line
from __future__ import unicode_literals

from django.db import models, router
from django.db import models
from django.db.models.options import normalize_together
from django.db.migrations.state import ModelState
from django.db.migrations.operations.base import Operation
@@ -32,13 +32,13 @@ class CreateModel(Operation):
    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        apps = to_state.render()
        model = apps.get_model(app_label, self.name)
        if router.allow_migrate(schema_editor.connection.alias, model) and not model._meta.proxy:
        if self.allowed_to_migrate(schema_editor.connection.alias, model):
            schema_editor.create_model(model)

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        apps = from_state.render()
        model = apps.get_model(app_label, self.name)
        if router.allow_migrate(schema_editor.connection.alias, model) and not model._meta.proxy:
        if self.allowed_to_migrate(schema_editor.connection.alias, model):
            schema_editor.delete_model(model)

    def describe(self):
@@ -85,13 +85,13 @@ class DeleteModel(Operation):
    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        apps = from_state.render()
        model = apps.get_model(app_label, self.name)
        if router.allow_migrate(schema_editor.connection.alias, model) and not model._meta.proxy:
        if self.allowed_to_migrate(schema_editor.connection.alias, model):
            schema_editor.delete_model(model)

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        apps = to_state.render()
        model = apps.get_model(app_label, self.name)
        if router.allow_migrate(schema_editor.connection.alias, model) and not model._meta.proxy:
        if self.allowed_to_migrate(schema_editor.connection.alias, model):
            schema_editor.create_model(model)

    def references_model(self, name, app_label=None):
@@ -141,7 +141,7 @@ class RenameModel(Operation):
        new_apps = to_state.render()
        old_model = old_apps.get_model(app_label, self.old_name)
        new_model = new_apps.get_model(app_label, self.new_name)
        if router.allow_migrate(schema_editor.connection.alias, new_model):
        if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
            # Move the main table
            schema_editor.alter_db_table(
                new_model,
@@ -194,7 +194,7 @@ class AlterModelTable(Operation):
        new_apps = to_state.render()
        old_model = old_apps.get_model(app_label, self.name)
        new_model = new_apps.get_model(app_label, self.name)
        if router.allow_migrate(schema_editor.connection.alias, new_model):
        if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
            schema_editor.alter_db_table(
                new_model,
                old_model._meta.db_table,
@@ -231,7 +231,7 @@ class AlterUniqueTogether(Operation):
        new_apps = to_state.render()
        old_model = old_apps.get_model(app_label, self.name)
        new_model = new_apps.get_model(app_label, self.name)
        if router.allow_migrate(schema_editor.connection.alias, new_model):
        if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
            schema_editor.alter_unique_together(
                new_model,
                getattr(old_model._meta, "unique_together", set()),
@@ -268,7 +268,7 @@ class AlterIndexTogether(Operation):
        new_apps = to_state.render()
        old_model = old_apps.get_model(app_label, self.name)
        new_model = new_apps.get_model(app_label, self.name)
        if router.allow_migrate(schema_editor.connection.alias, new_model):
        if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
            schema_editor.alter_index_together(
                new_model,
                getattr(old_model._meta, "index_together", set()),
@@ -301,7 +301,7 @@ class AlterOrderWithRespectTo(Operation):
    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        from_model = from_state.render().get_model(app_label, self.name)
        to_model = to_state.render().get_model(app_label, self.name)
        if router.allow_migrate(schema_editor.connection.alias, to_model):
        if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
            # Remove a field if we need to
            if from_model._meta.order_with_respect_to and not to_model._meta.order_with_respect_to:
                schema_editor.remove_field(from_model, from_model._meta.get_field_by_name("_order")[0])
+6 −0
Original line number Diff line number Diff line
@@ -2374,6 +2374,12 @@ Default: 'auth.User'

The model to use to represent a User. See :ref:`auth-custom-user`.

.. warning::
    You cannot change the AUTH_USER_MODEL setting during the lifetime of
    a project (i.e. once you have made and migrated models that depend on it)
    without serious effort. It is intended to be set at the project start.
    See :ref:`auth-custom-user` for more details.

.. setting:: LOGIN_REDIRECT_URL

LOGIN_REDIRECT_URL
+2 −1
Original line number Diff line number Diff line
@@ -389,7 +389,8 @@ use as your User model.

   Changing this setting after you have tables created is not supported
   by :djadmin:`makemigrations` and will result in you having to manually
   write a set of migrations to fix your schema.
   fix your schema, port your data from the old user table, and possibly
   manually reapply some migrations.

Referencing the User model
--------------------------
Loading