Commits

aarranz  committed 0520281

Test renaming of columns/tables with foreign keys

  • Participants
  • Parent commits 8e3b957
  • Branches mysql-fixes

Comments (0)

Files changed (1)

File south/tests/db.py

         db.rollback_transaction()
         db.delete_table("test_rn")
         db.start_transaction()
-    
+
+    def test_rename_column_with_fk(self):
+        """
+        Test column with foreign key renaming
+        """
+
+        if not db.supports_foreign_keys:
+            return
+
+        cursor = connection.cursor()
+        try:
+            db.create_table("test_fk", [('id', models.IntegerField(primary_key=True))])
+            db.create_table("test_rn", [
+                ('spam', models.BooleanField(default=False)),
+                ('eggs', models.ForeignKey(db.mock_model('TestFK', 'test_fk')))
+            ])
+            db.execute_deferred_sql()
+            # Make sure we can select the column
+            cursor.execute("SELECT eggs_id FROM test_rn")
+            # Insert test data
+            cursor.execute("INSERT INTO test_fk (id) VALUES (1)")
+            cursor.execute("INSERT INTO test_rn (eggs_id) VALUES (1)")
+            # Rename it
+            db.rename_column("test_rn", "eggs_id", "ham_id")
+            cursor.execute("SELECT ham_id FROM test_rn")
+            db.commit_transaction()
+            db.start_transaction()
+            try:
+                cursor.execute("SELECT eggs_id FROM test_rn")
+            except:
+                pass
+            else:
+                self.fail("Just-renamed column could be selected!")
+
+            db.commit_transaction()
+            db.start_transaction()
+            val = db.execute("SELECT ham_id FROM test_rn")
+            self.assertEqual(len(val), 1)
+
+            try:
+                db.execute("INSERT INTO test_rn (ham_id) VALUES (2)")
+                db.commit_transaction()
+            except:
+                pass
+            else:
+                self.fail("Foreign key constraint is gone!")
+        finally:
+            db.rollback_transaction()
+            db.delete_table("test_rn")
+            db.delete_table("test_fk")
+            db.start_transaction()
+
     def test_dry_rename(self):
         """
         Test column renaming while --dry-run is turned on (should do nothing)
         db.delete_table("testtr2")
         db.start_transaction()
     
+    def test_table_rename_with_fk(self):
+        """
+        Test table with foreign keys renaming
+        """
+
+        if not db.supports_foreign_keys:
+            return
+
+        cursor = connection.cursor()
+        try:
+            db.create_table("test_fk", [('id', models.IntegerField(primary_key=True))])
+            db.create_table("test_rn", [
+                ('spam', models.BooleanField(default=False)),
+                ('eggs', models.ForeignKey(db.mock_model('TestFK', 'test_fk')))
+            ])
+            db.execute_deferred_sql()
+            # Make sure we can select the column
+            cursor.execute("SELECT eggs_id FROM test_rn")
+            # Insert test data
+            db.execute("INSERT INTO test_fk (id) VALUES (1)")
+            db.execute("INSERT INTO test_rn (eggs_id) VALUES (1)")
+            # Rename it
+            db.rename_table("test_rn", "test_rn2")
+            cursor.execute("SELECT eggs_id FROM test_rn2")
+            db.commit_transaction()
+            db.start_transaction()
+            try:
+                cursor.execute("SELECT eggs_id FROM test_rn")
+            except:
+                pass
+            else:
+                self.fail("Just-renamed column could be selected!")
+
+            db.commit_transaction()
+            db.start_transaction()
+
+            val = db.execute("SELECT eggs_id FROM test_rn2")
+            self.assertEqual(len(val), 1)
+
+            try:
+                db.execute("INSERT INTO test_rn2 (eggs_id) VALUES (2)")
+                db.commit_transaction()
+            except:
+                pass
+            else:
+                self.fail("Foreign key constraint is gone!")
+
+        finally:
+            db.rollback_transaction()
+            db.delete_table("test_rn2")
+            db.delete_table("test_fk")
+            db.start_transaction()
+
     def test_percents_in_defaults(self):
         """
         Test that % in a default gets escaped to %%.