Skip to content

Commit

Permalink
Merge pull request #67 from hstanev/master
Browse files Browse the repository at this point in the history
Check if migrations are allowed for the model before applying it
  • Loading branch information
gregmuellegger committed Nov 26, 2015
2 parents 2f7a2c2 + 07593fb commit a7796a9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
7 changes: 7 additions & 0 deletions sortedm2m/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ def get_apps_from_state(migration_state):
return migration_state.render()
else:
return migration_state.apps


def allow_migrate_model(self, connection_alias, model):
if django.VERSION < (1, 8):
return self.allowed_to_migrate(connection_alias, model)
else:
return self.allow_migrate_model(connection_alias, model)
71 changes: 37 additions & 34 deletions sortedm2m/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sortedm2m.fields import SORT_VALUE_FIELD_NAME
from .compat import get_field
from .compat import get_apps_from_state
from .compat import allow_migrate_model


class AlterSortedManyToManyField(AlterField):
Expand All @@ -13,27 +14,28 @@ class AlterSortedManyToManyField(AlterField):
def database_forwards(self, app_label, schema_editor, from_state, to_state):
to_apps = get_apps_from_state(to_state)
to_model = to_apps.get_model(app_label, self.model_name)
to_field = get_field(to_model, self.name)
if allow_migrate_model(self, schema_editor.connection.alias, to_model):
to_field = get_field(to_model, self.name)

from_apps = get_apps_from_state(from_state)
from_model = from_apps.get_model(app_label, self.model_name)
from_field = get_field(from_model, self.name)
from_apps = get_apps_from_state(from_state)
from_model = from_apps.get_model(app_label, self.model_name)
from_field = get_field(from_model, self.name)

to_m2m_model = to_field.rel.through
from_m2m_model = from_field.rel.through
to_m2m_model = to_field.rel.through
from_m2m_model = from_field.rel.through

# M2M -> SortedM2M
if getattr(to_field, 'sorted', False):
self.add_sort_value_field(schema_editor, to_m2m_model)
# SortedM2M -> M2M
elif getattr(from_field, 'sorted', False):
self.remove_sort_value_field(schema_editor, from_m2m_model)
else:
raise TypeError(
'{operation} should only be used when changing a '
'SortedManyToManyField into a ManyToManyField or a '
'ManyToManyField into a SortedManyToManyField.'
.format(operation=self.__class__.__name__))
# M2M -> SortedM2M
if getattr(to_field, 'sorted', False):
self.add_sort_value_field(schema_editor, to_m2m_model)
# SortedM2M -> M2M
elif getattr(from_field, 'sorted', False):
self.remove_sort_value_field(schema_editor, from_m2m_model)
else:
raise TypeError(
'{operation} should only be used when changing a '
'SortedManyToManyField into a ManyToManyField or a '
'ManyToManyField into a SortedManyToManyField.'
.format(operation=self.__class__.__name__))

def database_backwards(self, app_label, schema_editor, from_state, to_state):
from_apps = get_apps_from_state(from_state)
Expand All @@ -42,25 +44,26 @@ def database_backwards(self, app_label, schema_editor, from_state, to_state):

to_apps = get_apps_from_state(to_state)
to_model = to_apps.get_model(app_label, self.model_name)
to_field = get_field(to_model, self.name)

from_m2m_model = from_field.rel.through
to_m2m_model = to_field.rel.through
if allow_migrate_model(self, schema_editor.connection.alias, to_model):
to_field = get_field(to_model, self.name)
from_m2m_model = from_field.rel.through
to_m2m_model = to_field.rel.through

# The `to_state` is the OLDER state.
# The `to_state` is the OLDER state.

# M2M -> SortedM2M (backwards)
if getattr(to_field, 'sorted', False):
self.add_sort_value_field(schema_editor, to_m2m_model)
# SortedM2M -> M2M (backwards)
elif getattr(from_field, 'sorted', False):
self.remove_sort_value_field(schema_editor, from_m2m_model)
else:
raise TypeError(
'{operation} should only be used when changing a '
'SortedManyToManyField into a ManyToManyField or a '
'ManyToManyField into a SortedManyToManyField.'
.format(operation=self.__class__.__name__))
# M2M -> SortedM2M (backwards)
if getattr(to_field, 'sorted', False):
self.add_sort_value_field(schema_editor, to_m2m_model)
# SortedM2M -> M2M (backwards)
elif getattr(from_field, 'sorted', False):
self.remove_sort_value_field(schema_editor, from_m2m_model)
else:
raise TypeError(
'{operation} should only be used when changing a '
'SortedManyToManyField into a ManyToManyField or a '
'ManyToManyField into a SortedManyToManyField.'
.format(operation=self.__class__.__name__))

def add_sort_value_field(self, schema_editor, model):
field = self.make_sort_by_field(model)
Expand Down

0 comments on commit a7796a9

Please sign in to comment.