Commits

Anonymous committed a5562a1

Properly stop syncdb doing its magic.

Comments (0)

Files changed (2)

         "varchar": "VARCHAR",
         "text": "TEXT",
         "integer": "INT",
+        "boolean": "BOOLEAN",
+        "serial": "SERIAL",
     }
 
     def __init__(self):
         """
         cursor = connection.cursor()
         if self.debug:
-            print "   = %s" % sql
+            print "   = %s" % sql, params
         return cursor.execute(sql, params)
 
 
+    def get_column_value(self, column, name):
+        """
+        Gets a column's something value from either a list or dict.
+        Useful for when both are passed into create_table in the column list.
+        """
+        defaults = {
+            "type_param": 0,
+            "unique": False,
+            "null": True,
+            "related_to": None,
+            "default": None,
+        }
+        if isinstance(column, (list, tuple)):
+            try:
+                return column[{
+                    "name": 0,
+                    "type": 1,
+                    "type_param": 2,
+                    "unique": 3,
+                    "null": 4,
+                    "related_to": 5,
+                    "default": 6,
+                }[name]]
+            except IndexError:
+                return defaults[name]
+        else:
+            return column.get(name, defaults.get(name, None))
+
+
     def create_table(self, table_name, columns):
         """
         Creates the table 'table_name'. 'columns' is a list of columns
         positional arguments).
         """
         qn = connection.ops.quote_name
+        defaults = tuple(self.get_column_value(column, "default") for column in columns)
         params = (
             qn(table_name),
             ", ".join([
-                self.column_sql(*column)
+                self.column_sql(
+                    column_name = self.get_column_value(column, "name"),
+                    type_name = self.get_column_value(column, "type"),
+                    type_param = self.get_column_value(column, "type_param"),
+                    unique = self.get_column_value(column, "unique"),
+                    null = self.get_column_value(column, "null"),
+                    related_to = self.get_column_value(column, "related_to"),
+                )
                 for column in columns
             ]),
         )
-        self.execute('CREATE TABLE %s (%s);' % params)
+        self.execute('CREATE TABLE %s (%s);' % params, defaults)
 
 
     def rename_table(self, old_table_name, table_name):
         self.execute('DROP TABLE %s;' % params)
 
 
-    def add_column(self, table_name, column_name, type_name, type_param=None, unique=False, null=True, related_to=None):
+    def add_column(self, table_name, column_name, type_name, type_param=None, unique=False, null=True, related_to=None, default=None):
         """
         Adds the column 'column_name' to the table 'table_name'.
         The column will have type 'type_name', which is one of the generic
             self.column_sql(column_name, type_name, type_param, unique, null, related_to),
         )
         sql = 'ALTER TABLE %s ADD COLUMN %s;' % params
-        self.execute(sql)
+        self.execute(sql, (default,))
 
 
     def column_sql(self, column_name, type_name, type_param=None, unique=False, null=True, related_to=None):
                 related_to[1],  # Column name
                 connection.ops.deferrable_sql(), # Django knows this
             )) or "",
+            "DEFAULT %s",
         )
-        return '%s %s %s %s' % params
+        return '%s %s %s %s %s' % params
 
 
     def delete_column(self, table_name, column_name):

management/commands/syncdb.py

 from django.core.management.base import NoArgsCommand
 from django.core.management.color import no_style
+from django.utils.datastructures import SortedDict
 from optparse import make_option
 from south import migration
 from django.core.management.commands import syncdb
 from django.conf import settings
+from django.db import models
+from django.db.models.loading import cache
 import sys
 
 class Command(NoArgsCommand):
     help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created, except those which use migrations."
 
     def handle_noargs(self, **options):
-        from django.db import models
         # Work out what uses migrations and so doesn't need syncing
         apps_needing_sync = []
         apps_migrated = []
                 apps_migrated.append(app_name)
         # Run syncdb on only the ones needed
         old_installed, settings.INSTALLED_APPS = settings.INSTALLED_APPS, apps_needing_sync
+        old_app_store, cache.app_store = cache.app_store, SortedDict([
+            (k, v) for (k, v) in cache.app_store.items()
+            if k in apps_needing_sync
+        ])
         syncdb.Command().execute(**options)
         settings.INSTALLED_APPS = old_installed
+        cache.app_store = old_app_store
         # Be obvious about what we did
         print "\nSynced:\n > %s" % "\n > ".join(apps_needing_sync)
         print "\nNot synced (use migrations):\n - %s" % "\n - ".join(apps_migrated)