Skip to content

Commit

Permalink
Rename pre_ and post_syncdb to *_migrate, with aliases from old names
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgodwin committed Jul 30, 2013
1 parent 086389f commit 68e0a16
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 74 deletions.
4 changes: 2 additions & 2 deletions django/contrib/auth/management/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def get_default_username(check_db=True):
return ''
return default_username

signals.post_syncdb.connect(create_permissions,
signals.post_migrate.connect(create_permissions,
dispatch_uid="django.contrib.auth.management.create_permissions")
signals.post_syncdb.connect(create_superuser,
signals.post_migrate.connect(create_superuser,
sender=auth_app, dispatch_uid="django.contrib.auth.management.create_superuser")
2 changes: 1 addition & 1 deletion django/contrib/contenttypes/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def update_all_contenttypes(verbosity=2, **kwargs):
for app in get_apps():
update_contenttypes(app, None, verbosity, **kwargs)

signals.post_syncdb.connect(update_contenttypes)
signals.post_migrate.connect(update_contenttypes)

if __name__ == "__main__":
update_all_contenttypes()
2 changes: 1 addition & 1 deletion django/contrib/gis/db/backends/spatialite/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def create_test_db(self, verbosity=1, autoclobber=False):

# We need to then do a flush to ensure that any data installed by
# custom SQL has been removed. The only test data should come from
# test fixtures, or autogenerated from post_syncdb triggers.
# test fixtures, or autogenerated from post_migrate triggers.
# This has the side effect of loading initial data (which was
# intentionally skipped in the syncdb).
call_command('flush',
Expand Down
2 changes: 1 addition & 1 deletion django/contrib/sites/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ def create_default_site(app, created_models, verbosity, db, **kwargs):

Site.objects.clear_cache()

signals.post_syncdb.connect(create_default_site, sender=site_app)
signals.post_migrate.connect(create_default_site, sender=site_app)
22 changes: 11 additions & 11 deletions django/core/management/commands/flush.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core.management import call_command
from django.core.management.base import NoArgsCommand, CommandError
from django.core.management.color import no_style
from django.core.management.sql import sql_flush, emit_post_sync_signal
from django.core.management.sql import sql_flush, emit_post_migrate_signal
from django.utils.importlib import import_module
from django.utils.six.moves import input
from django.utils import six
Expand All @@ -23,8 +23,8 @@ class Command(NoArgsCommand):
help='Tells Django not to load any initial data after database synchronization.'),
)
help = ('Returns the database to the state it was in immediately after '
'syncdb was executed. This means that all data will be removed '
'from the database, any post-synchronization handlers will be '
'migrate was first executed. This means that all data will be removed '
'from the database, any post-migration handlers will be '
're-executed, and the initial_data fixture will be re-installed.')

def handle_noargs(self, **options):
Expand All @@ -35,7 +35,7 @@ def handle_noargs(self, **options):
# The following are stealth options used by Django's internals.
reset_sequences = options.get('reset_sequences', True)
allow_cascade = options.get('allow_cascade', False)
inhibit_post_syncdb = options.get('inhibit_post_syncdb', False)
inhibit_post_migrate = options.get('inhibit_post_migrate', False)

self.style = no_style()

Expand All @@ -54,7 +54,7 @@ def handle_noargs(self, **options):
if interactive:
confirm = input("""You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the %r database,
and return each table to the state it was in after syncdb.
and return each table to a fresh state.
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict['NAME'])
Expand All @@ -77,8 +77,8 @@ def handle_noargs(self, **options):
"The full error: %s") % (connection.settings_dict['NAME'], e)
six.reraise(CommandError, CommandError(new_msg), sys.exc_info()[2])

if not inhibit_post_syncdb:
self.emit_post_syncdb(verbosity, interactive, db)
if not inhibit_post_migrate:
self.emit_post_migrate(verbosity, interactive, db)

# Reinstall the initial_data fixture.
if options.get('load_initial_data'):
Expand All @@ -89,13 +89,13 @@ def handle_noargs(self, **options):
self.stdout.write("Flush cancelled.\n")

@staticmethod
def emit_post_syncdb(verbosity, interactive, database):
# Emit the post sync signal. This allows individual applications to
# respond as if the database had been sync'd from scratch.
def emit_post_migrate(verbosity, interactive, database):
# Emit the post migrate signal. This allows individual applications to
# respond as if the database had been migrated from scratch.
all_models = []
for app in models.get_apps():
all_models.extend([
m for m in models.get_models(app, include_auto_created=True)
if router.allow_syncdb(database, m)
])
emit_post_sync_signal(set(all_models), verbosity, interactive, database)
emit_post_migrate_signal(set(all_models), verbosity, interactive, database)
21 changes: 14 additions & 7 deletions django/core/management/commands/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core.management import call_command
from django.core.management.base import BaseCommand, CommandError
from django.core.management.color import color_style, no_style
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal, emit_pre_sync_signal
from django.core.management.sql import custom_sql_for_model, emit_post_migrate_signal, emit_pre_migrate_signal
from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
from django.db.migrations.executor import MigrationExecutor
from django.db.migrations.loader import AmbiguityError
Expand Down Expand Up @@ -99,10 +99,14 @@ def handle(self, *args, **options):

# Run the syncdb phase.
# If you ever manage to get rid of this, I owe you many, many drinks.
# Note that pre_migrate is called from inside here, as it needs
# the list of models about to be installed.
if run_syncdb:
if self.verbosity >= 1:
self.stdout.write(self.style.MIGRATE_HEADING("Synchronizing apps without migrations:"))
self.sync_apps(connection, executor.loader.unmigrated_apps)
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
else:
created_models = []

# Migrate!
if self.verbosity >= 1:
Expand All @@ -113,6 +117,10 @@ def handle(self, *args, **options):
else:
executor.migrate(targets, plan, fake=options.get("fake", False))

# Send the post_migrate signal, so individual apps can do whatever they need
# to do at this point.
emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)

def migration_progress_callback(self, action, migration):
if self.verbosity >= 1:
if action == "apply_start":
Expand Down Expand Up @@ -159,7 +167,7 @@ def model_installed(model):
)

create_models = set([x for x in itertools.chain(*manifest.values())])
emit_pre_sync_signal(create_models, self.verbosity, self.interactive, connection.alias)
emit_pre_migrate_signal(create_models, self.verbosity, self.interactive, connection.alias)

# Create the tables for each model
if self.verbosity >= 1:
Expand Down Expand Up @@ -188,10 +196,6 @@ def model_installed(model):
# If you can prove we don't need this, remove it.
transaction.set_dirty(using=connection.alias)

# Send the post_syncdb signal, so individual apps can do whatever they need
# to do at this point.
emit_post_sync_signal(created_models, self.verbosity, self.interactive, connection.alias)

# The connection may have been closed by a syncdb handler.
cursor = connection.cursor()

Expand Down Expand Up @@ -220,6 +224,7 @@ def model_installed(model):

if self.verbosity >= 1:
self.stdout.write(" Installing indexes...\n")

# Install SQL indices for all newly created models
for app_name, model_list in manifest.items():
for model in model_list:
Expand All @@ -238,3 +243,5 @@ def model_installed(model):
# Load initial_data fixtures (unless that has been disabled)
if self.load_initial_data:
call_command('loaddata', 'initial_data', verbosity=self.verbosity, database=connection.alias, skip_validation=True)

return created_models
16 changes: 8 additions & 8 deletions django/core/management/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,25 @@ def custom_sql_for_model(model, style, connection):
return output


def emit_pre_sync_signal(create_models, verbosity, interactive, db):
# Emit the pre_sync signal for every application.
def emit_pre_migrate_signal(create_models, verbosity, interactive, db):
# Emit the pre_migrate signal for every application.
for app in models.get_apps():
app_name = app.__name__.split('.')[-2]
if verbosity >= 2:
print("Running pre-sync handlers for application %s" % app_name)
models.signals.pre_syncdb.send(sender=app, app=app,
print("Running pre-migrate handlers for application %s" % app_name)
models.signals.pre_migrate.send(sender=app, app=app,
create_models=create_models,
verbosity=verbosity,
interactive=interactive,
db=db)


def emit_post_sync_signal(created_models, verbosity, interactive, db):
# Emit the post_sync signal for every application.
def emit_post_migrate_signal(created_models, verbosity, interactive, db):
# Emit the post_migrate signal for every application.
for app in models.get_apps():
app_name = app.__name__.split('.')[-2]
if verbosity >= 2:
print("Running post-sync handlers for application %s" % app_name)
models.signals.post_syncdb.send(sender=app, app=app,
print("Running post-migrate handlers for application %s" % app_name)
models.signals.post_migrate.send(sender=app, app=app,
created_models=created_models, verbosity=verbosity,
interactive=interactive, db=db)
2 changes: 1 addition & 1 deletion django/db/backends/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def create_test_db(self, verbosity=1, autoclobber=False):

# We need to then do a flush to ensure that any data installed by
# custom SQL has been removed. The only test data should come from
# test fixtures, or autogenerated from post_syncdb triggers.
# test fixtures, or autogenerated from post_migrate triggers.
# This has the side effect of loading initial data (which was
# intentionally skipped in the syncdb).
call_command('flush',
Expand Down
6 changes: 4 additions & 2 deletions django/db/models/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
pre_delete = Signal(providing_args=["instance", "using"], use_caching=True)
post_delete = Signal(providing_args=["instance", "using"], use_caching=True)

pre_syncdb = Signal(providing_args=["app", "create_models", "verbosity", "interactive", "db"])
post_syncdb = Signal(providing_args=["class", "app", "created_models", "verbosity", "interactive", "db"], use_caching=True)
pre_migrate = Signal(providing_args=["app", "create_models", "verbosity", "interactive", "db"])
pre_syncdb = pre_migrate
post_migrate = Signal(providing_args=["class", "app", "created_models", "verbosity", "interactive", "db"], use_caching=True)
post_syncdb = post_migrate

m2m_changed = Signal(providing_args=["action", "instance", "reverse", "model", "pk_set", "using"], use_caching=True)
11 changes: 5 additions & 6 deletions django/test/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,16 +718,15 @@ def _pre_setup(self):
"""Performs any pre-test setup. This includes:
* If the class has an 'available_apps' attribute, restricting the app
cache to these applications, then firing post_syncdb -- it must run
cache to these applications, then firing post_migrate -- it must run
with the correct set of applications for the test case.
* If the class has a 'fixtures' attribute, installing these fixtures.
"""
super(TransactionTestCase, self)._pre_setup()
if self.available_apps is not None:
cache.set_available_apps(self.available_apps)
for db_name in self._databases_names(include_mirrors=False):
flush.Command.emit_post_syncdb(
verbosity=0, interactive=False, database=db_name)
flush.Command.emit_post_migrate(verbosity=0, interactive=False, database=db_name)
try:
self._fixture_setup()
except Exception:
Expand Down Expand Up @@ -772,7 +771,7 @@ def _post_teardown(self):
"""Performs any post-test things. This includes:
* Flushing the contents of the database, to leave a clean slate. If
the class has an 'available_apps' attribute, post_syncdb isn't fired.
the class has an 'available_apps' attribute, post_migrate isn't fired.
* Force-closing the connection, so the next test gets a clean cursor.
"""
try:
Expand All @@ -790,14 +789,14 @@ def _post_teardown(self):
cache.unset_available_apps()

def _fixture_teardown(self):
# Allow TRUNCATE ... CASCADE and don't emit the post_syncdb signal
# Allow TRUNCATE ... CASCADE and don't emit the post_migrate signal
# when flushing only a subset of the apps
for db_name in self._databases_names(include_mirrors=False):
call_command('flush', verbosity=0, interactive=False,
database=db_name, skip_validation=True,
reset_sequences=False,
allow_cascade=self.available_apps is not None,
inhibit_post_syncdb=self.available_apps is not None)
inhibit_post_migrate=self.available_apps is not None)

def assertQuerysetEqual(self, qs, values, transform=repr, ordered=True):
items = six.moves.map(transform, qs)
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/contrib/sites.txt
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ To enable the sites framework, follow these steps:
3. Run :djadmin:`migrate`.

``django.contrib.sites`` registers a
:data:`~django.db.models.signals.post_syncdb` signal handler which creates a
:data:`~django.db.models.signals.post_migrate` signal handler which creates a
default site named ``example.com`` with the domain ``example.com``. This site
will also be created after Django creates the test database. To set the
correct name and domain for your project, you can use an :doc:`initial data
Expand Down
Loading

0 comments on commit 68e0a16

Please sign in to comment.