Skip to content

Commit

Permalink
Fixes #5093 -- Use passed database alias in migrations (#5743)
Browse files Browse the repository at this point in the history
  • Loading branch information
czpython committed Oct 27, 2016
1 parent a8d3cb8 commit 4b6ba6c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
8 changes: 6 additions & 2 deletions cms/migrations/0005_auto_20140924_1039.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


class MP_AddHandler(object):

def __init__(self):
self.stmts = []

Expand Down Expand Up @@ -126,9 +127,12 @@ def process(self):


def move_to_mp(apps, schema_editor):
db_alias = schema_editor.connection.alias

Page = apps.get_model("cms", "Page")
CMSPlugin = apps.get_model("cms", "CMSPlugin")
pages = Page.objects.all().order_by('tree_id', 'level', 'lft')

pages = Page.objects.using(db_alias).order_by('tree_id', 'level', 'lft')

cache = {}
last_root = None
Expand All @@ -146,7 +150,7 @@ def move_to_mp(apps, schema_editor):
cache[page.pk] = page


plugins = CMSPlugin.objects.all().order_by('tree_id', 'level', 'lft')
plugins = CMSPlugin.objects.using(db_alias).order_by('tree_id', 'level', 'lft')

cache = {}
last_root = None
Expand Down
39 changes: 29 additions & 10 deletions cms/migrations/0010_migrate_use_structure.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,66 @@
# -*- coding: utf-8 -*-
import warnings
import functools

from django.conf import settings
from django.db import models, migrations


def _get_manager(model, db_alias):
return model.objects.db_manager(db_alias)


def forwards(apps, schema_editor):
db_alias = schema_editor.connection.alias

get_manager = functools.partial(_get_manager, db_alias=db_alias)

ContentType = apps.get_model('contenttypes', 'ContentType')
Permission = apps.get_model('auth', 'Permission')
Group = apps.get_model('auth', 'Group')
user_model = apps.get_model(settings.AUTH_USER_MODEL)
ph_model = apps.get_model('cms', 'Placeholder')
page_model = apps.get_model('cms', 'Page')

try:
ph_ctype = ContentType.objects.get_for_model(ph_model)
page_ctype = ContentType.objects.get_for_model(page_model)
permission, __ = Permission.objects.get_or_create(
ph_ctype = get_manager(ContentType).get_for_model(ph_model)
page_ctype = get_manager(ContentType).get_for_model(page_model)
permission, __ = get_manager(Permission).get_or_create(
codename='use_structure', content_type=ph_ctype, name=u"Can use Structure mode")
page_permission, __ = Permission.objects.get_or_create(
page_permission, __ = get_manager(Permission).get_or_create(
codename='change_page', content_type=page_ctype, name=u'Can change page'
)
for user in user_model.objects.filter(is_superuser=False, is_staff=True):
for user in get_manager(user_model).filter(is_superuser=False, is_staff=True):
if user.user_permissions.filter(codename='change_page', content_type_id=page_ctype.pk).exists():
user.user_permissions.add(permission.pk)
for group in Group.objects.all():
for group in get_manager(Group).all():
if page_permission in group.permissions.all():
group.permissions.add(permission.pk)
except Exception:
warnings.warn(u'Users not migrated to use_structure permission, please add the permission manually')


def backwards(apps, schema_editor):
db_alias = schema_editor.connection.alias

get_manager = functools.partial(_get_manager, db_alias=db_alias)

ContentType = apps.get_model('contenttypes', 'ContentType')
Permission = apps.get_model('auth', 'Permission')
Group = apps.get_model('auth', 'Group')
user_model = apps.get_model(settings.AUTH_USER_MODEL)
ph_model = apps.get_model('cms', 'Placeholder')
ph_ctype = ContentType.objects.get(app_label=ph_model._meta.app_label, model=ph_model._meta.model_name)
ph_ctype = get_manager(ContentType).get(
app_label=ph_model._meta.app_label,
model=ph_model._meta.model_name,
)

try:
permission, __ = Permission.objects.get_or_create(
permission, __ = get_manager(Permission).get_or_create(
codename='use_structure', content_type=ph_ctype, name=u"Can use Structure mode")
for user in user_model.objects.filter(is_superuser=False, is_staff=True):
for user in get_manager(user_model).filter(is_superuser=False, is_staff=True):
user.user_permissions.remove(permission.pk)
for group in Group.objects.all():
for group in get_manager(Group).all():
if permission in group.permissions.all():
group.permissions.remove(permission.pk)
except Exception:
Expand Down
22 changes: 16 additions & 6 deletions cms/migrations/0014_auto_20160404_1908.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@


def forwards(apps, schema_editor):
db_alias = schema_editor.connection.alias

ContentType = apps.get_model('contenttypes', 'ContentType')
Permission = apps.get_model('auth', 'Permission')
page_model = apps.get_model('cms', 'Page')
Expand All @@ -13,14 +15,22 @@ def forwards(apps, schema_editor):
# Calling get_for_model directly causes Django to create
# the Content Type for Page if it does not exist.
# See django-cms#5589 & django#2342
content_type_exists = ContentType.objects.filter(
app_label=page_opts.app_label,
model=page_opts.model_name,
).exists()
content_type_exists = (
ContentType
.objects
.using(db_alias)
.filter(app_label=page_opts.app_label, model=page_opts.model_name)
.exists()
)

if content_type_exists:
page_ctype = ContentType.objects.get_for_model(page_model)
Permission.objects.filter(
page_ctype = (
ContentType
.objects
.db_manager(db_alias)
.get_for_model(page_model)
)
Permission.objects.using(db_alias).filter(
name='',
codename='change_page',
content_type_id=page_ctype.pk,
Expand Down

0 comments on commit 4b6ba6c

Please sign in to comment.