如何在Django迁移中捕获异常?
我有一个迁移,由于各种遗留原因,我预计有时会失败。我希望能够捕获该错误并在这种情况下运行一些错误处理代码。
具体来说,我正在重命名一个表,有时目标表已经存在,我想合并旧表和新表的内容,然后删除旧表。
我正在运行Django 1.7(:(),我们计划升级到1.8,但它还没有发生。
我的迁移是:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('main', '0007_migration_name'),
]
operations = [
migrations.AlterModelTable(
name='table_name',
table='LegacyTableName',
),
]
当我运行时,我得到了
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File ".../django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File ".../django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File ".../django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File ".../django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File ".../django/core/management/commands/migrate.py", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File ".../django/db/migrations/executor.py", line 68, in migrate
self.apply_migration(migration, fake=fake)
File ".../django/db/migrations/executor.py", line 102, in apply_migration
migration.apply(project_state, schema_editor)
File ".../django/db/migrations/migration.py", line 108, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File ".../django/db/migrations/operations/models.py", line 236, in database_forwards
new_model._meta.db_table,
File ".../django/db/backends/schema.py", line 350, in alter_db_table
"new_table": self.quote_name(new_db_table),
File ".../django/db/backends/schema.py", line 111, in execute
cursor.execute(sql, params)
File ".../django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File ".../django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File ".../django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File ".../django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File ".../django/db/backends/mysql/base.py", line 129, in execute
return self.cursor.execute(query, args)
File ".../MySQLdb/cursors.py", line 226, in execute
self.errorhandler(self, exc, value)
File ".../MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorvalue
django.db.utils.OperationalError: (1050, "Table 'LegacyTableName' already exists")
迁移本身提供的所有内容都是operations
列表,the docs中似乎没有可选的错误处理参数。
如何捕获OperationalError,以便运行一些Python来合并表?
答案 0 :(得分:1)
尝试在Python中捕获数据库异常的问题在于它们可能不够具体 - 例如,OperationalError
可能由于各种原因而出现(其中只有一个是表名已经被更改)。
我建议你不要试图捕获异常,而是编写自己的迁移函数,进行必要的检查/修改。请参阅documentation on RunPython
。
这通常是您用于创建数据迁移,运行自定义数据更新和更改以及您需要访问ORM和/或Python代码的任何其他操作的操作。
在您的情况下,您将编写一个函数来检查表是否存在并对这两种情况执行某些操作。
编写这些函数时需要注意一些特定于数据库的问题,例如:
例如,在PostgreSQL上,您应该避免在同一次迁移中组合架构更改和RunPython操作,否则可能会遇到错误。