Code

Ticket #1397: syncdb_initial_sql_data.diff

File syncdb_initial_sql_data.diff, 2.5 KB (added by anonymous, 8 years ago)

makes the django_package thingy work again

Line 
1Index: django/branches/magic-removal/django/core/management.py
2===================================================================
3--- django/branches/magic-removal/django/core/management.py     (revision 2402)
4+++ django/branches/magic-removal/django/core/management.py     (working copy)
5@@ -26,6 +26,12 @@
6 
7 INVALID_PROJECT_NAMES = ('django', 'test')
8 
9+def _get_app_label(app):
10+    from django.db.models import get_models
11+    app_models = get_models(app)
12+    app_label = app_models[0]._meta.app_label
13+    return app_label
14+
15 def _get_packages_insert(app_label):
16     from django.db import backend
17     return "INSERT INTO %s (%s, %s) VALUES ('%s', '%s');" % \
18@@ -338,6 +344,8 @@
19     app_models = get_models(app)
20     app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
21 
22+    output.append(_get_packages_insert(_get_app_label(app)))
23+
24     for klass in app_models:
25         opts = klass._meta
26 
27@@ -415,7 +423,10 @@
28 
29     pending_references = []
30 
31+    initial_data = []
32+
33     for app in models.get_apps():
34+        is_new_app = True
35         model_list = models.get_models(app)
36         for model in model_list:
37             # Create the model's database table, if it doesn't already exist.
38@@ -420,6 +431,7 @@
39         for model in model_list:
40             # Create the model's database table, if it doesn't already exist.
41             if model._meta.db_table in table_list:
42+                is_new_app = False
43                 continue
44             field_metadata, table_metadata, references = sql_for_table(model)
45             pending_references.extend(references)
46@@ -429,6 +441,10 @@
47             print "Creating table %s" % model._meta.db_table
48             cursor.execute(sql)
49 
50+        if is_new_app:
51+            # This is a new app, get initial sql data
52+            initial_data.append((_get_app_label(app), get_sql_initial_data(app)))
53+
54         for model in model_list:
55             # Create the many-to-many join table, if it doesn't already exist.
56             for f in model._meta.many_to_many:
57@@ -454,6 +470,13 @@
58                 sql = '\n'.join(table_output)
59                 print "Creating table %s" % f.m2m_db_table()
60                 cursor.execute(sql)
61+               
62+    # Add initial sql data for the newly installed apps
63+    if len(initial_data) > 0:
64+        for label, sql_list in initial_data:
65+            print "Adding initial sql data for %s" % label
66+            for sql in sql_list:
67+                cursor.execute(sql)
68 
69     # Create the pending references.
70     # Take care of any ALTER TABLE statements to add constraints