add sharable subscriptions (bug 854)
[mygpo.git] / bin / fix-duplicate-device-uids.py
blob6ad2af472e3d0ed55fdd3b505a85dca4e0e8eadc
2 # Fix a bug where older versions of mygpo allowed users to set the
3 # UID to the same value for different devices. This is not allowed
4 # anymore by update-12.sql from January 2010, but it is needed for
5 # migrating the existing user database. -- Thomas Perl, 2010-01-19
7 import MySQLdb
8 import sys
10 if len(sys.argv) != 3:
11 print >>sys.stderr, """
12 Usage: %s [username] [database]
13 """ % (sys.argv[0],)
14 sys.exit(1)
16 username, database = sys.argv[-2:]
17 connection = MySQLdb.connect(user=username, db=database)
19 cur = connection.cursor()
20 cur.execute('select uid, user_id, count(*) from device'
21 +' group by uid, user_id having count(*) > 1')
22 todo = list(cur.fetchall())
23 cur.close()
25 cur = connection.cursor()
26 for uid, user_id, count in todo:
27 counter = 1
28 cur.execute('select id from device where uid=%s and user_id=%s',
29 (uid, user_id))
30 for row in list(cur.fetchall()):
31 id = row[0]
32 if counter > 1:
33 new_uid = '%s%d' % (uid, counter)
34 print 'updating uid of %s to %s' % (id, new_uid)
35 cur.execute('update device set uid=%s where id=%s',
36 (new_uid, id))
37 counter += 1
38 cur.close()
40 connection.close()