Skip to content

Commit

Permalink
Use sqlite_version_info rather than version_info.
Browse files Browse the repository at this point in the history
Fixes failing tests because Sqlite's DROP COLUMN does not handle
anything like updating/dropping an affected index, etc.

Fixes #2759
  • Loading branch information
coleifer committed Jul 28, 2023
1 parent 83c6b78 commit f107a90
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions playhouse/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ def _fix_index(self, sql, column_to_update, new_column):

@operation
def drop_column(self, table, column_name, cascade=True, legacy=False):
if sqlite3.version_info >= (3, 25, 0) and not legacy:
if sqlite3.sqlite_version_info >= (3, 25, 0) and not legacy:
ctx = self.make_context()
(self._alter_table(ctx, table)
.literal(' DROP COLUMN ')
Expand All @@ -846,7 +846,7 @@ def drop_column(self, table, column_name, cascade=True, legacy=False):

@operation
def rename_column(self, table, old_name, new_name, legacy=False):
if sqlite3.version_info >= (3, 25, 0) and not legacy:
if sqlite3.sqlite_version_info >= (3, 25, 0) and not legacy:
return (self
._alter_table(self.make_context(), table)
.literal(' RENAME COLUMN ')
Expand Down
17 changes: 10 additions & 7 deletions tests/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ class Meta:
self.assertEqual(foreign_key.dest_table, 'person')

def test_drop_foreign_key(self):
migrate(self.migrator.drop_column('page', 'user_id'))
kw = {'legacy': True} if IS_SQLITE else {}
migrate(self.migrator.drop_column('page', 'user_id', **kw))
columns = self.database.get_columns('page')
self.assertEqual(
sorted(column.name for column in columns),
Expand Down Expand Up @@ -664,19 +665,19 @@ def test_alter_column_type(self):
@requires_sqlite
def test_valid_column_required(self):
self.assertRaises(
ValueError,
(OperationalError, ValueError),
migrate,
self.migrator.drop_column('page', 'column_does_not_exist'))

self.assertRaises(
ValueError,
(OperationalError, ValueError),
migrate,
self.migrator.rename_column('page', 'xx', 'yy'))

@requires_sqlite
@requires_models(IndexModel)
def test_table_case_insensitive(self):
migrate(self.migrator.drop_column('PaGe', 'name'))
migrate(self.migrator.drop_column('PaGe', 'name', legacy=True))
column_names = self.get_column_names('page')
self.assertEqual(column_names, set(['id', 'user_id']))

Expand All @@ -685,7 +686,8 @@ def test_table_case_insensitive(self):
column_names = self.get_column_names('page')
self.assertEqual(column_names, set(['id', 'user_id', 'testing']))

migrate(self.migrator.drop_column('indeX_mOdel', 'first_name'))
migrate(self.migrator.drop_column('indeX_mOdel', 'first_name',
legacy=True))
indexes = self.migrator.database.get_indexes('index_model')
self.assertEqual(len(indexes), 1)
self.assertEqual(indexes[0].name, 'index_model_data')
Expand Down Expand Up @@ -759,7 +761,8 @@ def test_index_preservation(self):
migrate(self.migrator.rename_column(
'index_model',
'first_name',
'first'))
'first',
legacy=True))

queries = [x.msg for x in self.history]
self.assertEqual(queries, [
Expand Down Expand Up @@ -818,7 +821,7 @@ def test_modify_fk_constraint(self):
self.reset_sql_history()
new_fk = ForeignKeyField(User, User.id, null=True, on_delete='CASCADE')
migrate(
self.migrator.drop_column('page', 'user_id'),
self.migrator.drop_column('page', 'user_id', legacy=True),
self.migrator.add_column('page', 'user_id', new_fk))

queries = [x.msg for x in self.history]
Expand Down
3 changes: 2 additions & 1 deletion tests/regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1365,9 +1365,10 @@ class TestFKMigrationRegression(ModelTestCase):

def test_fk_migration(self):
migrator = SchemaMigrator.from_database(self.database)
kw = {'legacy': True} if IS_SQLITE else {}
migrate(migrator.drop_column(
FKMB._meta.table_name,
FKMB.fkma.column_name))
FKMB.fkma.column_name, **kw))

migrate(migrator.add_column(
FKMB._meta.table_name,
Expand Down

0 comments on commit f107a90

Please sign in to comment.