Anonymous avatar Anonymous committed 449ae10

Add a drop_column alias, and make aliases not break if subclasses override the methods they point to.

Comments (0)

Files changed (2)

 from django.dispatch import dispatcher
 from django.conf import settings
 
+
+def alias(attrname):
+    """
+    Returns a function which calls 'attrname' - for function aliasing.
+    We can't just use foo = bar, as this breaks subclassing.
+    """
+    def func(self, *args, **kwds):
+        return getattr(self, attrname)(*args, **kwds)
+    return func
+
+
 class DatabaseOperations(object):
 
     """
         
         self.execute('CREATE TABLE %s (%s);' % (qn(table_name), ', '.join([col for col in columns if col])))
     
-    add_table = create_table # Alias for consistency's sake
+    add_table = alias('create_table') # Alias for consistency's sake
 
 
     def rename_table(self, old_table_name, table_name):
         params = (qn(table_name), )
         self.execute('DROP TABLE %s;' % params)
     
-    drop_table = delete_table
+    drop_table = alias('delete_table')
 
 
     def add_column(self, table_name, name, field, keep_default=True):
         sql = self.drop_index_string % {"index_name": name, "table_name": table_name}
         self.execute(sql)
 
+    drop_index = alias('delete_index')
+
 
     def delete_column(self, table_name, name):
         """
         qn = connection.ops.quote_name
         params = (qn(table_name), qn(name))
         self.execute('ALTER TABLE %s DROP COLUMN %s CASCADE;' % params, [])
+    
+    drop_column = alias('delete_column')
 
 
     def rename_column(self, table_name, old, new):
         db.rollback_transaction()
         db.start_transaction()
         # Remove the table
-        db.delete_table("test1")
+        db.drop_table("test1")
         # Make sure it went
         try:
             cursor.execute("SELECT * FROM test1")
         db.rollback_transaction()
         db.delete_table("test2")
     
+    def test_table_rename(self):
+        """
+        Test column renaming
+        """
+        cursor = connection.cursor()
+        db.create_table("testtr", [('spam', models.BooleanField(default=False))])
+        db.start_transaction()
+        # Make sure we can select the column
+        cursor.execute("SELECT spam FROM testtr")
+        # Rename it
+        db.rename_table("testtr", "testtr2")
+        cursor.execute("SELECT spam FROM testtr2")
+        try:
+            cursor.execute("SELECT spam FROM testtr")
+            self.fail("Just-renamed column could be selected!")
+        except:
+            pass
+        db.rollback_transaction()
+        db.delete_table("testtr2")
+    
     def test_index(self):
         """
         Test the index operations
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.