[英]Is there a tool to check database integrity in Django?
為我們的Django站點供電的MySQL數據庫產生了一些完整性問題; 例如,引用不存在的行的外鍵。 我不會談論我們是如何陷入這種混亂的,但我現在正在研究如何解決這個問題。
基本上, 我正在尋找一個掃描Django站點中所有模型的腳本,並檢查所有外鍵和其他約束是否正確 。 希望問題的數量足夠小,以便手動修復。
我可以自己編寫代碼,但我希望這里有人有更好的主意。
我發現了django-check-constraints,但它不太符合要求:現在,我不需要一些東西來防止這些問題,但要找到它們以便在采取其他步驟之前手動修復它們。
其他限制:
(稍后,我們將轉換為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
我正在將其作為社區維基,因為我歡迎對我的代碼進行任何改進!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.