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