Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion piccolo/apps/migrations/auto/migration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,11 @@ async def _run_add_tables(self, backwards: bool = False):

if backwards:
for _Table in reversed(sorted_table_classes):
await self._run_query(_Table.alter().drop_table(cascade=True))
await self._run_query(
_Table.alter().drop_table(
cascade=_Table._meta.db.engine_type != "sqlite"
)
)
else:
for _Table in sorted_table_classes:
await self._run_query(_Table.create_table())
Expand Down
31 changes: 31 additions & 0 deletions tests/apps/migrations/auto/test_migration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,37 @@ def test_add_table(self, get_app_config: MagicMock):
)
self.assertEqual(self.table_exists("musician"), False)

@engines_only("sqlite")
@patch.object(BaseMigrationManager, "get_app_config")
def test_add_table_sqlite(self, get_app_config: MagicMock):
"""
Make sure a table can be added, then the migration reversed, on
SQLite. Reversing used to fail, because it tried to run
`DROP TABLE ... CASCADE`, which SQLite doesn't support.

https://github.com/piccolo-orm/piccolo/issues/1420

"""
self.run_sync("DROP TABLE IF EXISTS musician;")

manager = MigrationManager()
manager.add_table(class_name="Musician", tablename="musician")
manager.add_column(
table_class_name="Musician",
tablename="musician",
column_name="name",
column_class_name="Varchar",
)
asyncio.run(manager.run())
self.assertTrue(self.table_exists("musician"))

# Reverse
get_app_config.return_value = AppConfig(
app_name="music", migrations_folder_path=""
)
asyncio.run(manager.run(backwards=True))
self.assertFalse(self.table_exists("musician"))

@engines_only("postgres", "cockroach")
def test_add_column(self) -> None:
"""
Expand Down
Loading