Skip to content

Commit f35a2a0

Browse files
committed
New script to check for violated database constraints (NOT NULL, UNIQUE, FOREIGN KEY)
- Legacy-Id: 2062
1 parent 5cbbf09 commit f35a2a0

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

test/check_database_constraints.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
2+
# All rights reserved. Contact: Pasi Eronen <pasi.eronen@nokia.com>
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions
6+
# are met:
7+
#
8+
# * Redistributions of source code must retain the above copyright
9+
# notice, this list of conditions and the following disclaimer.
10+
#
11+
# * Redistributions in binary form must reproduce the above
12+
# copyright notice, this list of conditions and the following
13+
# disclaimer in the documentation and/or other materials provided
14+
# with the distribution.
15+
#
16+
# * Neither the name of the Nokia Corporation and/or its
17+
# subsidiary(-ies) nor the names of its contributors may be used
18+
# to endorse or promote products derived from this software
19+
# without specific prior written permission.
20+
#
21+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
33+
from ietf import settings
34+
from django.core import management
35+
management.setup_environ(settings)
36+
37+
from django.db.models.fields.related import ForeignKey
38+
from django import db
39+
from django.db import models
40+
41+
cursor = db.connection.cursor()
42+
43+
def check_foreign_key(model, field):
44+
print "Checking foreign key", model._meta.db_table+"."+field.column
45+
print " points to:", field.rel.to._meta.db_table+"."+field.rel.field_name
46+
sql = "SELECT src.%s,src.%s FROM %s AS src LEFT OUTER JOIN %s as dst on src.%s=dst.%s WHERE src.%s IS NOT NULL AND dst.%s IS NULL;" % (model._meta.pk.column, field.column, model._meta.db_table, field.rel.to._meta.db_table, field.column, field.rel.get_related_field().column, field.column, field.rel.get_related_field().column)
47+
#print sql
48+
cursor.execute(sql)
49+
rows = cursor.fetchall()
50+
if len(rows) == 0:
51+
print " OK, no hanging rows found"
52+
else:
53+
print " ERROR, found", len(rows), "hanging rows"
54+
for row in rows[0:20]:
55+
print " ", row
56+
print " Use the following SQL to debug:"
57+
print sql
58+
59+
def check_not_null(model, field):
60+
print "Checking NULL values", model._meta.db_table+"."+field.column
61+
sql = "SELECT x.%s,x.%s FROM %s as x WHERE x.%s IS NULL" % (model._meta.pk.column, field.column, model._meta.db_table, field.column)
62+
cursor.execute(sql)
63+
rows = cursor.fetchall()
64+
if len(rows) == 0:
65+
print " OK"
66+
else:
67+
print " ERROR, found", len(rows), "NULL rows"
68+
for row in rows[0:20]:
69+
print " ", row
70+
print " Use the following SQL to debug:"
71+
print sql
72+
73+
def check_unique(model,field):
74+
print "Checking unique values", model._meta.db_table+"."+field.column
75+
sql = "SELECT %s FROM %s GROUP BY %s HAVING COUNT(*)>1" % (field.column, model._meta.db_table, field.column)
76+
cursor.execute(sql)
77+
rows = cursor.fetchall()
78+
if len(rows) == 0:
79+
print " OK"
80+
else:
81+
print " ERROR, found non-unique rows"
82+
for row in rows[0:20]:
83+
print " ", row
84+
print " Use the following SQL to debug:"
85+
print sql
86+
87+
APPS = ['announcements','idrfc','idtracker','iesg','ietfauth','ipr','liaisons','proceedings','redirects']
88+
all_models = []
89+
for app_label in APPS:
90+
all_models.extend(models.get_models(models.get_app(app_label)))
91+
92+
for model in all_models:
93+
print "\n\nChecking %s (table %s)" % (model._meta.object_name, model._meta.db_table)
94+
for f in model._meta.fields:
95+
if isinstance(f, ForeignKey):
96+
check_foreign_key(model,f)
97+
if not f.null:
98+
check_not_null(model, f)
99+
if f.unique:
100+
check_unique(model, f)
101+

0 commit comments

Comments
 (0)