fix for issue 96: deleting a column in sqlite shouldn't delete all indexes

bonus: remove_from_table now removes indexes
This commit is contained in:
chrisw 2010-09-09 09:15:28 +01:00
parent dceff55ff4
commit 5cf42fbf76
4 changed files with 51 additions and 10 deletions

View File

@ -11,6 +11,7 @@ Fixed bugs
to :mod:`migrate.exceptions`
- cleared up test output and improved testing of deprecation warnings.
- some documentation fixes
- fixed bug with column dropping in sqlite (issue 96)
0.6 (11.07.2010)
---------------------------

View File

@ -35,11 +35,10 @@ class SQLiteHelper(SQLiteCommon):
table = self._to_table(column.table)
table_name = self.preparer.format_table(table)
# we remove all constraints, indexes so it doesnt recreate them
ixbackup = copy(table.indexes)
consbackup = copy(table.constraints)
table.indexes = set()
table.constraints = set()
# we remove all indexes so as not to have
# problems during copy and re-create
for index in table.indexes:
index.drop()
self.append('ALTER TABLE %s RENAME TO migration_tmp' % table_name)
self.execute()
@ -52,10 +51,6 @@ class SQLiteHelper(SQLiteCommon):
self.append('DROP TABLE migration_tmp')
self.execute()
# restore indexes, constraints
table.indexes = ixbackup
table.constraints = consbackup
class SQLiteColumnGenerator(SQLiteSchemaGenerator, SQLiteCommon,
ansisql.ANSIColumnGenerator):
"""SQLite ColumnGenerator"""

View File

@ -568,9 +568,20 @@ populated with defaults
self._set_parent(table)
def remove_from_table(self, table, unset_table=True):
# TODO: remove indexes, primary keys, constraints, etc
# TODO: remove primary keys, constraints, etc
if unset_table:
self.table = None
to_drop = set()
for index in table.indexes:
columns = []
for col in index.columns:
if col.name!=self.name:
columns.append(col)
if columns:
index.columns=columns
else:
to_drop.add(index)
table.indexes = table.indexes - to_drop
if table.c.contains_column(self):
table.c.remove(self)

View File

@ -299,7 +299,41 @@ class TestAddDropColumn(fixture.DB):
# TODO: test quoting
# TODO: test non-autoname constraints
@fixture.usedb()
def test_drop_doesnt_delete_other_indexes(self):
# add two indexed columns
self.table.drop()
self.meta.clear()
self.table = Table(
self.table_name, self.meta,
Column('id', Integer, primary_key=True),
Column('d1', String(10), index=True),
Column('d2', String(10), index=True),
)
self.table.create()
# paranoid check
if SQLA_06:
self.refresh_table()
self.assertEqual(
sorted([i.name for i in self.table.indexes]),
[u'ix_tmp_adddropcol_d1', u'ix_tmp_adddropcol_d2']
)
# delete one
self.table.c.d2.drop()
# ensure the other index is still there
if SQLA_06:
self.refresh_table()
self.assertEqual(
sorted([i.name for i in self.table.indexes]),
[u'ix_tmp_adddropcol_d1']
)
else:
# a crude test for 0.5.x
Index('ix_tmp_adddropcol_d1',self.table.c.d1).drop()
class TestRename(fixture.DB):
"""Tests for table and index rename methods"""
level = fixture.DB.CONNECT