繁体   English   中英

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

[英]Is there a tool to check database integrity in Django?

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

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

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

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

其他限制:

  • Django 1.1.1和升级已经决心打破局面
  • MySQL 5.0.51(Debian Lenny),目前有MyISAM
  • Python 2.5,可能是可升级的,但我现在不想

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

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

用法: ./manage.py checkdb进行全面检查; 使用--exclude app.Model-e app.Model来排除app app的模型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

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

托马斯的答案很棒,但现在有点过时了。 我已将其更新支持Django 1.8+ 的要点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM