6

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

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

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

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

其他约束:

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

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

4

2 回答 2

8

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

用途:./manage.py checkdb用于全面检查;在应用程序中使用--exclude app.Model-e app.Model排除模型。Modelapp

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,因为我欢迎对我的代码进行任何和所有改进!

于 2011-01-19T14:10:34.427 回答
2

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

于 2017-09-28T00:56:13.517 回答