Commits

Andrew Godwin committed 00b730d

Fix some unit tests for sqlite, and add in column deleting support.

  • Participants
  • Parent commits fb20345
  • Branches 0.7

Comments (0)

Files changed (2)

File south/db/sqlite3.py

     
     backend_name = "sqlite3"
 
-    # SQLite ignores foreign key constraints. I wish I could.
+    # SQLite ignores several constraints. I wish I could.
     supports_foreign_keys = False
+    has_check_constraints = False
 
     # You can't add UNIQUE columns with an ALTER TABLE.
     def add_column(self, table_name, name, field, *args, **kwds):
         Given a table and three sets of changes (renames, deletes, alters),
         recreates it with the modified schema.
         """
+        # Dry runs get skipped completely
+        if self.dry_run:
+            return
         # Temporary table's name
         temp_name = "_south_new_" + table_name
-        
         # Work out the (possibly new) definitions of each column
         definitions = {}
         cursor = self._get_connection().cursor()
         self._copy_data(table_name, temp_name, renames)
         # Delete the old table, move our new one over it
         self.delete_table(table_name)
-        self.rename_table(temp_name)
+        self.rename_table(temp_name, table_name)
     
     def _copy_data(self, src, dst, field_renames={}):
         "Used to copy data into a new table"
         # Make a list of all the fields to select
         cursor = self._get_connection().cursor()
-        q_fields = [column_info[0] for column_info in self._get_connection().introspection.get_table_description(cursor, table_name)]
+        q_fields = [column_info[0] for column_info in self._get_connection().introspection.get_table_description(cursor, dst)]
         # Make sure renames are done correctly
         for old, new in field_renames.items():
             q_fields[q_fields.index(new)] = "%s AS %s" % (old, self.quote_name(new))
         raise NotImplementedError
 
     def delete_column(self, table_name, column_name):
-        raise NotImplementedError
+        self._remake_table(table_name, deleted=[column_name])
     
     def rename_column(self, table_name, old, new):
         """
     # No cascades on deletes
     def delete_table(self, table_name, cascade=True):
         generic.DatabaseOperations.delete_table(self, table_name, False)
-
-    

File south/tests/db.py

         Tests that going from a PostiveIntegerField to an IntegerField drops
         the constraint on the database.
         """
+        # Only applies to databases that support CHECK constraints
+        if not db.has_check_constraints:
+            return
+        
         db.create_table("test_alterc", [
             ('num', models.PositiveIntegerField()),
         ])
         # Add in some test values
-        db.execute("INSERT INTO test_alterc (num) VALUES (1), (2)")
+        db.execute("INSERT INTO test_alterc (num) VALUES (1)")
+        db.execute("INSERT INTO test_alterc (num) VALUES (2)")
         # Ensure that adding a negative number is bad
         db.start_transaction()
         try:
         db.create_unique("test_unique", ["spam"])
         db.start_transaction()
         # Test it works
-        db.execute("INSERT INTO test_unique2 (id) VALUES (1), (2)")
-        db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (true, 0, 1), (false, 1, 2)")
+        db.execute("INSERT INTO test_unique2 (id) VALUES (1)")
+        db.execute("INSERT INTO test_unique2 (id) VALUES (2)")
+        db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (true, 0, 1)")
+        db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (false, 1, 2)")
         try:
             db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (true, 2, 1)")
         except:
         db.create_unique("test_unique", ["eggs"])
         db.start_transaction()
         # Test similarly
-        db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (true, 0, 1), (false, 1, 2)")
+        db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (true, 0, 1)")
+        db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (false, 1, 2)")
         try:
             db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (true, 1, 1)")
         except:
         db.create_unique("test_unique", ["spam", "eggs", "ham_id"])
         db.start_transaction()
         # Test similarly
-        db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (true, 0, 1), (false, 1, 1)")
+        db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (true, 0, 1)")
+        db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (false, 1, 1)")
         try:
             db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (true, 0, 1)")
         except: