From 6e025c4a4d0f8ad558c3ed66b45a1f0087b860d1 Mon Sep 17 00:00:00 2001
From: iElectric <unknown>
Date: Tue, 11 May 2010 22:52:07 +0200
Subject: [PATCH] add firebird to test_db.cfg.tmpl; fix bug when dropping a
 column in firebird: also drop related constraint or index

---
 TODO                                    |  1 +
 migrate/changeset/databases/firebird.py | 13 +++++++++++++
 test-req.pip                            |  1 +
 test_db.cfg.tmpl                        |  2 +-
 tests/changeset/test_changeset.py       | 13 ++++---------
 5 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/TODO b/TODO
index a97c647..ba13f38 100644
--- a/TODO
+++ b/TODO
@@ -23,3 +23,4 @@ make_update_script_for_model:
 - managing output - logging
 - required_dbs tests
 - add story to changeset tutorial
+- verbose output on migration failures
diff --git a/migrate/changeset/databases/firebird.py b/migrate/changeset/databases/firebird.py
index a8831bf..297fe6c 100644
--- a/migrate/changeset/databases/firebird.py
+++ b/migrate/changeset/databases/firebird.py
@@ -18,6 +18,19 @@ class FBColumnDropper(ansisql.ANSIColumnDropper):
     """Firebird column dropper implementation."""
 
     def visit_column(self, column):
+        """Firebird supports 'DROP col' instead of 'DROP COLUMN col' syntax
+
+        Drop primary key and unique constraints if dropped column is referencing it."""
+        if column.primary_key:
+            if column.table.primary_key.columns.contains_column(column):
+                column.table.primary_key.drop()
+                # TODO: recreate primary key if it references more than this column
+        if column.unique or getattr(column, 'unique_name', None):
+            for cons in column.table.constraints:
+                if cons.contains_column(column):
+                    cons.drop()
+                    # TODO: recreate unique constraint if it refenrences more than this column
+
         table = self.start_alter_table(column)
         self.append('DROP %s' % self.preparer.format_column(column))
         self.execute()
diff --git a/test-req.pip b/test-req.pip
index 3c674e7..45d3025 100644
--- a/test-req.pip
+++ b/test-req.pip
@@ -6,3 +6,4 @@ pytz
 psycopg2
 pysqlite
 mysql-python
+http://sourceforge.net/projects/kinterbasdb/files/kinterbasdb/kinterbasdb-3.2/kinterbasdb-3.2.src.tar.gz/download
diff --git a/test_db.cfg.tmpl b/test_db.cfg.tmpl
index 1461967..59ea742 100644
--- a/test_db.cfg.tmpl
+++ b/test_db.cfg.tmpl
@@ -11,4 +11,4 @@ sqlite:///__tmp__
 postgresql://scott:tiger@localhost/test_migrate
 mysql://scott:tiger@localhost/test_migrate
 oracle://scott:tiger@localhost
-# TODO: add firebird
+firebird://scott:tiger@localhost//var/lib/firebird/databases/test_migrate
diff --git a/tests/changeset/test_changeset.py b/tests/changeset/test_changeset.py
index 999f88f..97db212 100644
--- a/tests/changeset/test_changeset.py
+++ b/tests/changeset/test_changeset.py
@@ -204,7 +204,7 @@ class TestAddDropColumn(fixture.DB):
             self.fail()
 
         col.drop()
-    
+
     @fixture.usedb(not_supported='mysql')
     def test_check(self):
         """Can create columns with check constraint"""
@@ -245,6 +245,7 @@ class TestAddDropColumn(fixture.DB):
 
         col.drop(self.table)
 
+# TODO: remove already attached columns with indexes, uniques, pks, fks ..
     @fixture.usedb()
     def test_index(self):
         """Can create columns with indexes"""
@@ -469,19 +470,13 @@ class TestColumnChange(fixture.DB):
         self.table.c.data.alter(Column('data', String(42)))
         self.refresh_table(self.table.name)
         self.assert_(isinstance(self.table.c.data.type, String))
-        if self.engine.name == 'firebird':
-            self.assertEquals(self.table.c.data.type.length, 42 * 4)
-        else:
-            self.assertEquals(self.table.c.data.type.length, 42)
+        self.assertEquals(self.table.c.data.type.length, 42)
 
         # Just the new type
         self.table.c.data.alter(type=String(43))
         self.refresh_table(self.table.name)
         self.assert_(isinstance(self.table.c.data.type, String))
-        if self.engine.name == 'firebird':
-            self.assertEquals(self.table.c.data.type.length, 43 * 4)
-        else:
-            self.assertEquals(self.table.c.data.type.length, 43)
+        self.assertEquals(self.table.c.data.type.length, 43)
 
         # Different type
         self.assert_(isinstance(self.table.c.id.type, Integer))