Fixed #21477 -- Renamed db to using in pre/post_migrate signals.

This commit is contained in:
Aymeric Augustin 2014-01-12 22:20:53 +01:00
parent d674fe6dee
commit d562527a16
6 changed files with 20 additions and 20 deletions

View File

@ -60,7 +60,7 @@ def _check_permission_clashing(custom, builtin, ctype):
pool.add(codename) pool.add(codename)
def create_permissions(app_config, verbosity=2, interactive=True, db=DEFAULT_DB_ALIAS, **kwargs): def create_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs):
if not app_config.models_module: if not app_config.models_module:
return return
@ -69,7 +69,7 @@ def create_permissions(app_config, verbosity=2, interactive=True, db=DEFAULT_DB_
except LookupError: except LookupError:
return return
if not router.allow_migrate(db, Permission): if not router.allow_migrate(using, Permission):
return return
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
@ -82,7 +82,7 @@ def create_permissions(app_config, verbosity=2, interactive=True, db=DEFAULT_DB_
for klass in app_config.get_models(): for klass in app_config.get_models():
# Force looking up the content types in the current database # Force looking up the content types in the current database
# before creating foreign keys to them. # before creating foreign keys to them.
ctype = ContentType.objects.db_manager(db).get_for_model(klass) ctype = ContentType.objects.db_manager(using).get_for_model(klass)
ctypes.add(ctype) ctypes.add(ctype)
for perm in _get_all_permissions(klass._meta, ctype): for perm in _get_all_permissions(klass._meta, ctype):
searched_perms.append((ctype, perm)) searched_perms.append((ctype, perm))
@ -90,7 +90,7 @@ def create_permissions(app_config, verbosity=2, interactive=True, db=DEFAULT_DB_
# Find all the Permissions that have a content_type for a model we're # Find all the Permissions that have a content_type for a model we're
# looking for. We don't need to check for codenames since we already have # looking for. We don't need to check for codenames since we already have
# a list of the ones we're going to create. # a list of the ones we're going to create.
all_perms = set(Permission.objects.using(db).filter( all_perms = set(Permission.objects.using(using).filter(
content_type__in=ctypes, content_type__in=ctypes,
).values_list( ).values_list(
"content_type", "codename" "content_type", "codename"
@ -113,13 +113,13 @@ def create_permissions(app_config, verbosity=2, interactive=True, db=DEFAULT_DB_
verbose_name_max_length, verbose_name_max_length,
) )
) )
Permission.objects.using(db).bulk_create(perms) Permission.objects.using(using).bulk_create(perms)
if verbosity >= 2: if verbosity >= 2:
for perm in perms: for perm in perms:
print("Adding permission '%s'" % perm) print("Adding permission '%s'" % perm)
def create_superuser(app_config, verbosity=2, interactive=True, db=DEFAULT_DB_ALIAS, **kwargs): def create_superuser(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs):
try: try:
apps.get_model('auth', 'Permission') apps.get_model('auth', 'Permission')
except LookupError: except LookupError:
@ -139,7 +139,7 @@ def create_superuser(app_config, verbosity=2, interactive=True, db=DEFAULT_DB_AL
confirm = input('Please enter either "yes" or "no": ') confirm = input('Please enter either "yes" or "no": ')
continue continue
if confirm == 'yes': if confirm == 'yes':
call_command("createsuperuser", interactive=True, database=db) call_command("createsuperuser", interactive=True, database=using)
break break

View File

@ -6,7 +6,7 @@ from django.utils import six
from django.utils.six.moves import input from django.utils.six.moves import input
def update_contenttypes(app_config, verbosity=2, interactive=True, db=DEFAULT_DB_ALIAS, **kwargs): def update_contenttypes(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs):
""" """
Creates content types for models in the given app, removing any model Creates content types for models in the given app, removing any model
entries that no longer have a matching model class. entries that no longer have a matching model class.
@ -19,7 +19,7 @@ def update_contenttypes(app_config, verbosity=2, interactive=True, db=DEFAULT_DB
except LookupError: except LookupError:
return return
if not router.allow_migrate(db, ContentType): if not router.allow_migrate(using, ContentType):
return return
ContentType.objects.clear_cache() ContentType.objects.clear_cache()
@ -36,7 +36,7 @@ def update_contenttypes(app_config, verbosity=2, interactive=True, db=DEFAULT_DB
# Get all the content types # Get all the content types
content_types = dict( content_types = dict(
(ct.model, ct) (ct.model, ct)
for ct in ContentType.objects.using(db).filter(app_label=app_label) for ct in ContentType.objects.using(using).filter(app_label=app_label)
) )
to_remove = [ to_remove = [
ct ct
@ -53,7 +53,7 @@ def update_contenttypes(app_config, verbosity=2, interactive=True, db=DEFAULT_DB
for (model_name, model) in six.iteritems(app_models) for (model_name, model) in six.iteritems(app_models)
if model_name not in content_types if model_name not in content_types
] ]
ContentType.objects.using(db).bulk_create(cts) ContentType.objects.using(using).bulk_create(cts)
if verbosity >= 2: if verbosity >= 2:
for ct in cts: for ct in cts:
print("Adding content type '%s | %s'" % (ct.app_label, ct.model)) print("Adding content type '%s | %s'" % (ct.app_label, ct.model))

View File

@ -217,7 +217,7 @@ def emit_pre_migrate_signal(create_models, verbosity, interactive, db):
app_config=app_config, app_config=app_config,
verbosity=verbosity, verbosity=verbosity,
interactive=interactive, interactive=interactive,
db=db) using=db)
# For backwards-compatibility -- remove in Django 1.9. # For backwards-compatibility -- remove in Django 1.9.
models.signals.pre_syncdb.send( models.signals.pre_syncdb.send(
sender=app_config.models_module, sender=app_config.models_module,
@ -240,7 +240,7 @@ def emit_post_migrate_signal(created_models, verbosity, interactive, db):
app_config=app_config, app_config=app_config,
verbosity=verbosity, verbosity=verbosity,
interactive=interactive, interactive=interactive,
db=db) using=db)
# For backwards-compatibility -- remove in Django 1.9. # For backwards-compatibility -- remove in Django 1.9.
models.signals.post_syncdb.send( models.signals.post_syncdb.send(
sender=app_config.models_module, sender=app_config.models_module,

View File

@ -62,8 +62,8 @@ post_delete = ModelSignal(providing_args=["instance", "using"], use_caching=True
m2m_changed = ModelSignal(providing_args=["action", "instance", "reverse", "model", "pk_set", "using"], use_caching=True) m2m_changed = ModelSignal(providing_args=["action", "instance", "reverse", "model", "pk_set", "using"], use_caching=True)
pre_migrate = Signal(providing_args=["app_config", "verbosity", "interactive", "db"]) pre_migrate = Signal(providing_args=["app_config", "verbosity", "interactive", "using"])
post_migrate = Signal(providing_args=["app_config", "verbosity", "interactive", "db"]) post_migrate = Signal(providing_args=["app_config", "verbosity", "interactive", "using"])
pre_syncdb = Signal(providing_args=["app", "create_models", "verbosity", "interactive", "db"]) pre_syncdb = Signal(providing_args=["app", "create_models", "verbosity", "interactive", "db"])
post_syncdb = Signal(providing_args=["class", "app", "created_models", "verbosity", "interactive", "db"]) post_syncdb = Signal(providing_args=["class", "app", "created_models", "verbosity", "interactive", "db"])

View File

@ -408,7 +408,7 @@ Arguments sent with this signal:
For example, the :mod:`django.contrib.auth` app only prompts to create a For example, the :mod:`django.contrib.auth` app only prompts to create a
superuser when ``interactive`` is ``True``. superuser when ``interactive`` is ``True``.
``db`` ``using``
The alias of database on which a command will operate. The alias of database on which a command will operate.
pre_syncdb pre_syncdb
@ -459,7 +459,7 @@ Arguments sent with this signal:
For example, the :mod:`django.contrib.auth` app only prompts to create a For example, the :mod:`django.contrib.auth` app only prompts to create a
superuser when ``interactive`` is ``True``. superuser when ``interactive`` is ``True``.
``db`` ``using``
The alias of database on which a command will operate. The alias of database on which a command will operate.
post_migrate post_migrate

View File

@ -6,7 +6,7 @@ from django.utils import six
APP_CONFIG = apps.get_app_config('migrate_signals') APP_CONFIG = apps.get_app_config('migrate_signals')
PRE_MIGRATE_ARGS = ['app_config', 'verbosity', 'interactive', 'db'] PRE_MIGRATE_ARGS = ['app_config', 'verbosity', 'interactive', 'using']
MIGRATE_DATABASE = 'default' MIGRATE_DATABASE = 'default'
MIGRATE_VERBOSITY = 1 MIGRATE_VERBOSITY = 1
MIGRATE_INTERACTIVE = False MIGRATE_INTERACTIVE = False
@ -35,7 +35,7 @@ class OneTimeReceiver(object):
def __call__(self, signal, sender, **kwargs): def __call__(self, signal, sender, **kwargs):
# Although test runner calls migrate for several databases, # Although test runner calls migrate for several databases,
# testing for only one of them is quite sufficient. # testing for only one of them is quite sufficient.
if kwargs['db'] == MIGRATE_DATABASE: if kwargs['using'] == MIGRATE_DATABASE:
self.call_counter = self.call_counter + 1 self.call_counter = self.call_counter + 1
self.call_args = kwargs self.call_args = kwargs
# we need to test only one call of migrate # we need to test only one call of migrate
@ -74,4 +74,4 @@ class MigrateSignalTests(TestCase):
self.assertEqual(args['app_config'], APP_CONFIG) self.assertEqual(args['app_config'], APP_CONFIG)
self.assertEqual(args['verbosity'], MIGRATE_VERBOSITY) self.assertEqual(args['verbosity'], MIGRATE_VERBOSITY)
self.assertEqual(args['interactive'], MIGRATE_INTERACTIVE) self.assertEqual(args['interactive'], MIGRATE_INTERACTIVE)
self.assertEqual(args['db'], 'default') self.assertEqual(args['using'], 'default')