【问题标题】:Is there a tool to check database integrity in Django?有没有工具可以检查 Django 中的数据库完整性?
【发布时间】:2011-06-11 16:51:37
【问题描述】:

为我们的 Django 站点提供支持的 MySQL 数据库出现了一些完整性问题;例如引用不存在的行的外键。我不会详细说明我们是如何陷入这种混乱的,但我现在正在研究如何解决它。

基本上,我正在寻找一个脚本来扫描 Django 站点中的所有模型,并检查所有外键和其他约束是否正确。希望问题的数量足够少,以便可以手动解决。

我可以自己编写代码,但我希望这里有人有更好的主意。

我找到了 django-check-constraints,但它并不完全符合要求:现在,我不需要任何东西来防止这些问题,而是找到它们以便在采取其他步骤之前手动修复它们。

其他限制:

  • Django 1.1.1和升级已经确定要破事了
  • MySQL 5.0.51 (Debian Lenny),目前带有 MyISAM
  • Python 2.5,可能可以升级,但我现在不想升级

(稍后,我们将转换为 InnoDB 以获得适当的事务支持,并可能在数据库级别进行外键约束,以防止将来出现类似问题。但这不是本问题的主题。)

【问题讨论】:

  • 你没有提到只使用 PostGreSQL,MySQL 是强制性的?
  • 不是强制性的,不。稍后我们肯定会考虑使用 PostgreSQL,但目前更改 DBMS 风险太大。

标签: mysql database django integrity


【解决方案1】:

我自己做了一些事情。下面的管理脚本应该保存在myapp/management/commands/checkdb.py。确保中间目录有一个__init__.py 文件。

用法:./manage.py checkdb 进行全面检查;在应用程序app中使用--exclude app.Model-e app.Model排除模型Model

from django.core.management.base import BaseCommand, CommandError
from django.core.management.base import NoArgsCommand
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from optparse import make_option
from lib.progress import with_progress_meter

def model_name(model):
    return '%s.%s' % (model._meta.app_label, model._meta.object_name)

class Command(BaseCommand):
    args = '[-e|--exclude app_name.ModelName]'
    help = 'Checks constraints in the database and reports violations on stdout'

    option_list = NoArgsCommand.option_list + (
        make_option('-e', '--exclude', action='append', type='string', dest='exclude'),
    )

    def handle(self, *args, **options):
        # TODO once we're on Django 1.2, write to self.stdout and self.stderr instead of plain print

        exclude = options.get('exclude', None) or []

        failed_instance_count = 0
        failed_model_count = 0
        for app in models.get_apps():
            for model in models.get_models(app):
                if model_name(model) in exclude:
                    print 'Skipping model %s' % model_name(model)
                    continue
                fail_count = self.check_model(app, model)
                if fail_count > 0:
                    failed_model_count += 1
                    failed_instance_count += fail_count
        print 'Detected %d errors in %d models' % (failed_instance_count, failed_model_count)

    def check_model(self, app, model):
        meta = model._meta
        if meta.proxy:
            print 'WARNING: proxy models not currently supported; ignored'
            return

        # Define all the checks we can do; they return True if they are ok,
        # False if not (and print a message to stdout)
        def check_foreign_key(model, field):
            foreign_model = field.related.parent_model
            def check_instance(instance):
                try:
                    # name: name of the attribute containing the model instance (e.g. 'user')
                    # attname: name of the attribute containing the id (e.g. 'user_id')
                    getattr(instance, field.name)
                    return True
                except ObjectDoesNotExist:
                    print '%s with pk %s refers via field %s to nonexistent %s with pk %s' % \
                        (model_name(model), str(instance.pk), field.name, model_name(foreign_model), getattr(instance, field.attname))
            return check_instance

        # Make a list of checks to run on each model instance
        checks = []
        for field in meta.local_fields + meta.local_many_to_many + meta.virtual_fields:
            if isinstance(field, models.ForeignKey):
                checks.append(check_foreign_key(model, field))

        # Run all checks
        fail_count = 0
        if checks:
            for instance in with_progress_meter(model.objects.all(), model.objects.count(), 'Checking model %s ...' % model_name(model)):
                for check in checks:
                    if not check(instance):
                        fail_count += 1
        return fail_count

我将其设为社区 wiki,因为我欢迎对我的代码进行任何和所有改进!

【讨论】:

  • 这看起来很棒。您应该联系 Django 核心开发人员并询问他们是否会考虑在 Django 本身中进行类似的操作。我认为这是一个巨大的缺失功能。
  • 在哪里可以找到 lib.progress with_progress_meter?干杯
【解决方案2】:

Thomas 的回答很棒,但现在有点过时了。 我已经更新了 as a gist 以支持 Django 1.8+。

【讨论】:

    猜你喜欢
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多