Code

Ticket #11398: pre_syncdb.diff

File pre_syncdb.diff, 2.8 KB (added by yrashk@…, 5 years ago)

Patch

Line 
1--- a/django/core/management/commands/syncdb.py
2+++ b/django/core/management/commands/syncdb.py
3@@ -19,7 +19,7 @@ class Command(NoArgsCommand):
4     def handle_noargs(self, **options):
5         from django.db import connection, transaction, models
6         from django.conf import settings
7-        from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
8+        from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal, emit_pre_sync_signal
9 
10         verbosity = int(options.get('verbosity', 1))
11         interactive = options.get('interactive')
12@@ -54,6 +54,20 @@ class Command(NoArgsCommand):
13         created_models = set()
14         pending_references = {}
15 
16+        models_to_be_created = []
17+        for app in models.get_apps():
18+            app_name = app.__name__.split('.')[-2]
19+            model_list = models.get_models(app)
20+            for model in model_list:
21+                 if connection.introspection.table_name_converter(model._meta.db_table) in tables:
22+                    continue
23+                 models_to_be_created.append(model)
24+
25+        # Send the post_syncdb signal, so individual apps can do whatever they need
26+        # to do at this point.
27+        emit_pre_sync_signal(models_to_be_created, verbosity, interactive)
28+                       
29+
30         # Create the tables for each model
31         for app in models.get_apps():
32             app_name = app.__name__.split('.')[-2]
33diff --git a/django/core/management/sql.py b/django/core/management/sql.py
34--- a/django/core/management/sql.py
35+++ b/django/core/management/sql.py
36@@ -203,3 +203,15 @@ def emit_post_sync_signal(created_models, verbosity, interactive):
37         models.signals.post_syncdb.send(sender=app, app=app,
38             created_models=created_models, verbosity=verbosity,
39             interactive=interactive)
40+
41+def emit_pre_sync_signal(models_to_be_created, verbosity, interactive):
42+    from django.db import models
43+    from django.dispatch import dispatcher
44+    # Emit the pre_sync signal for every application.
45+    for app in models.get_apps():
46+        app_name = app.__name__.split('.')[-2]
47+        if verbosity >= 2:
48+           print "Running pre-sync handlers for application", app_name
49+        models.signals.pre_syncdb.send(sender=app, app=app,
50+            models_to_be_created=models_to_be_created, verbosity=verbosity,
51+            interactive=interactive)
52diff --git a/django/db/models/signals.py b/django/db/models/signals.py
53--- a/django/db/models/signals.py
54+++ b/django/db/models/signals.py
55@@ -12,3 +12,4 @@ pre_delete = Signal(providing_args=["instance"])
56 post_delete = Signal(providing_args=["instance"])
57 
58 post_syncdb = Signal(providing_args=["class", "app", "created_models", "verbosity", "interactive"])
59+pre_syncdb = Signal(providing_args=["class", "app", "models_to_be_created", "verbosity", "interactive"])