Code

Ticket #7589: gis_post_create_sql.diff

File gis_post_create_sql.diff, 3.1 KB (added by jbronn, 6 years ago)

Modification of Django management commands to support GeoDjango

Line 
1Index: commands/sqlcustom.py
2===================================================================
3--- commands/sqlcustom.py       (revision 7813)
4+++ commands/sqlcustom.py       (working copy)
5@@ -7,4 +7,4 @@
6 
7     def handle_app(self, app, **options):
8         from django.core.management.sql import sql_custom
9-        return u'\n'.join(sql_custom(app)).encode('utf-8')
10+        return u'\n'.join(sql_custom(app, self.style)).encode('utf-8')
11Index: commands/syncdb.py
12===================================================================
13--- commands/syncdb.py  (revision 7813)
14+++ commands/syncdb.py  (working copy)
15@@ -112,7 +112,7 @@
16             app_name = app.__name__.split('.')[-2]
17             for model in models.get_models(app):
18                 if model in created_models:
19-                    custom_sql = custom_sql_for_model(model)
20+                    custom_sql = custom_sql_for_model(model, self.style)
21                     if custom_sql:
22                         if verbosity >= 1:
23                             print "Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name)
24Index: sql.py
25===================================================================
26--- sql.py      (revision 7813)
27+++ sql.py      (working copy)
28@@ -219,7 +219,7 @@
29     statements = connection.ops.sql_flush(style, tables, sequence_list())
30     return statements
31 
32-def sql_custom(app):
33+def sql_custom(app, style):
34     "Returns a list of the custom table modifying SQL statements for the given app."
35     from django.db.models import get_models
36     output = []
37@@ -228,7 +228,7 @@
38     app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
39 
40     for model in app_models:
41-        output.extend(custom_sql_for_model(model))
42+        output.extend(custom_sql_for_model(model, style))
43 
44     return output
45 
46@@ -242,7 +242,7 @@
47 
48 def sql_all(app, style):
49     "Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module."
50-    return sql_create(app, style) + sql_custom(app) + sql_indexes(app, style)
51+    return sql_create(app, style) + sql_custom(app, style) + sql_indexes(app, style)
52 
53 def sql_model_create(model, style, known_models=set()):
54     """
55@@ -426,7 +426,7 @@
56 
57     return final_output
58 
59-def custom_sql_for_model(model):
60+def custom_sql_for_model(model, style):
61     from django.db import models
62     from django.conf import settings
63 
64@@ -434,6 +434,14 @@
65     app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql'))
66     output = []
67 
68+    # Post-creation SQL should come before any initial SQL data is loaded.
69+    # However, this should not be done for fields that are part of a
70+    # a parent model (via model inheritance).
71+    nm = opts.init_name_map()
72+    post_sql_fields = [f for f in opts.fields if nm[f.name][1] is None and hasattr(f, '_post_create_sql')]
73+    for f in post_sql_fields:
74+        output.extend(f._post_create_sql(style, model._meta.db_table))
75+
76     # Some backends can't execute more than one SQL statement at a time,
77     # so split into separate statements.
78     statements = re.compile(r";[ \t]*$", re.M)