有没有工具来检查Django中的数据库完整性?

时间:2011-01-19 11:25:24

标签: mysql database django integrity

为我们的Django站点供电的MySQL数据库产生了一些完整性问题;例如引用不存在的行的外键。我不会讨论我们是如何陷入这种混乱的,但我现在正在研究如何解决这个问题。

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

我可以自己编写代码,但我希望有人能有更好的想法。

我找到了django-check-constraints但它不太合适:现在,我不需要一些东西来防止这些问题,但要找到它们以便在采取其他步骤之前手动修复它们。 / p>

其他限制:

  • Django 1.1.1 并且升级已经确定要破解
  • MySQL 5.0.51(Debian Lenny),目前有 MyISAM 表格
  • Python 2.5,可能是可升级的,但我现在不想现在

(稍后,我们将转换为InnoDB以获得正确的事务支持,以及数据库级别的外键约束,以防止将来出现类似问题。但这不是此问题的主题。)

2 个答案:

答案 0 :(得分:8)

我自己掀起了一些东西。以下管理脚本应保存在myapp/management/commands/checkdb.py中。确保中间目录具有__init__.py文件。

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

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

我正在将其作为社区维基,因为我欢迎对我的代码进行任何改进!

答案 1 :(得分:1)

托马斯的答案很棒,但现在有点过时了。 我更新了它as a gist以支持Django 1.8 +。