b0b8ff03ec3176205cb4892ea32fa21ded4bbbae
[joel/kofoto.git] / src / cmdline / kofoto-check-integrity
1 #! /usr/bin/env python
2
3 import sqlite
4 import sys
5 from sets import Set
6
7 if len(sys.argv) != 2:
8     print "Usage: kofoto-check-integrity <path-to-database>"
9     sys.exit(1)
10
11 def getIds(connection, idMapping, tableColumn):
12     cursor = connection.cursor()
13     s = Set()
14     table, column = tableColumn.split(".")
15     cursor.execute("select %s from %s" % (column, table))
16     for row in cursor.fetchall():
17         s.add(row[0])
18     idMapping[tableColumn] = s
19
20 def checkSubset(idMapping, subsetName, supersetName):
21     subset = idMapping[subsetName]
22     superset = idMapping[supersetName]
23     if not subset.issubset(superset):
24         print "%s not in %s: %s" % (
25             subsetName,
26             supersetName,
27             ", ".join([str(x) for x in subset.difference(superset)]))
28
29 connection = sqlite.connect(sys.argv[1])
30 idMapping = {}
31
32 for tableColumn in [
33     "object.id",
34     "album.id",
35     "image.id",
36     "image.primary_version",
37     "image_version.id",
38     "image_version.image",
39     "member.album",
40     "member.object",
41     "attribute.object",
42     "category.id",
43     "category_child.parent",
44     "category_child.child",
45     "object_category.object",
46     "object_category.category",
47     ]:
48     getIds(connection, idMapping, tableColumn)
49
50 for subset, superset in [
51     ("album.id", "object.id"),
52     ("image.id", "object.id"),
53     ("image.primary_version", "image_version.id"),
54     ("image_version.image", "image.id"),
55     ("member.album", "album.id"),
56     ("member.object", "object.id"),
57     ("attribute.object", "object.id"),
58     ("category_child.parent", "category.id"),
59     ("category_child.child", "category.id"),
60     ("object_category.object", "object.id"),
61     ("object_category.category", "category.id"),
62     ]:
63     checkSubset(idMapping, subset, superset)
64
65 cursor = connection.cursor()
66 cursor.execute(
67     " select i1.id, iv.id, i2.id"
68     " from   image as i1,"
69     "        image_version as iv,"
70     "        image as i2"
71     " where  i1.primary_version = iv.id and"
72     "        iv.image = i2.id and"
73     "        i1.id != i2.id")
74 for image1_id, imageversion_id, image2_id in cursor.fetchall():
75     print "Image %d has primary version %d, but version %d belongs to " \
76           "image %d" % (image1_id, imageversion_id, imageversion_id, image2_id)