blob: b2a632b6ff1836d72dd5fbaba4479be8d5e1f6fe [file] [log] [blame]
Jan Luebbe6f656fc2009-08-13 00:58:34 +02001#!/usr/bin/python2.5
2
3from __future__ import with_statement
4
5from pysqlite2 import dbapi2 as sqlite3
6import sys
7
8hlr = sqlite3.connect(sys.argv[1])
9web = sqlite3.connect(sys.argv[2])
10
11# switch to autocommit
12hlr.isolation_level = None
13web.isolation_level = None
14
15hlr.row_factory = sqlite3.Row
16web.row_factory = sqlite3.Row
17
18with hlr:
19 hlr_subscrs = hlr.execute("""
20 SELECT * FROM Subscriber
21 """).fetchall()
22 hlr_tokens = hlr.execute("""
23 SELECT * FROM AuthToken
24 """).fetchall()
25
26with web:
27 web_tokens = web.execute("""
28 SELECT * FROM reg_tokens
29 """).fetchall()
30
31# index by subscr id
32hlr_subscrs_by_id = {}
33hlr_tokens_by_subscr_id = {}
34for x in hlr_subscrs:
35 hlr_subscrs_by_id[x['id']] = x
36del hlr_subscrs
37for x in hlr_tokens:
38 hlr_tokens_by_subscr_id[x['subscriber_id']] = x
39del hlr_tokens
40
41web_tokens_by_subscr_id = {}
42for x in web_tokens:
43 web_tokens_by_subscr_id[x['subscriber_id']] = x
44del web_tokens
45
46# remove leftover web_tokens and correct inconsistent fields
47with web:
48 for x in web_tokens_by_subscr_id.values():
49 subscr = hlr_subscrs_by_id.get(x['subscriber_id'], None)
50 if subscr is None:
51 web.execute("""
52 DELETE FROM reg_tokens WHERE subscriber_id = ?
53 """, (x['subscriber_id'],))
54 del web_tokens_by_subscr_id[x['subscriber_id']]
55 continue
56 if str(x['imsi']) != str(subscr['imsi']) or \
57 x['extension'] != subscr['extension'] or \
58 x['tmsi'] != subscr['tmsi'] or \
59 x['lac'] != subscr['lac']:
60 web.execute("""
61 UPDATE reg_tokens
62 SET imsi = ?, extension = ?, tmsi = ?, lac = ?
63 WHERE subscriber_id = ?
64 """, (str(subscr['imsi']), subscr['extension'], x['subscriber_id']))
65 x['imsi'] = str(subscr['imsi'])
66 x['extension'] = subscr['extension']
67 x['tmsi'] = subscr['tmsi']
68 x['lac'] = subscr['lac']
69
70# add missing web_tokens
71with web:
72 for x in hlr_tokens_by_subscr_id.values():
73 subscr = hlr_subscrs_by_id.get(x['subscriber_id'], None)
74 if subscr is None:
75 hlr.execute("""
76 DELETE FROM AuthToken WHERE subscriber_id = ?
77 """, (x['subscriber_id'],))
78 del hlr_tokens_by_subscr_id[x['subscriber_id']]
79 continue
80 webtoken = web_tokens_by_subscr_id.get(x['subscriber_id'], None)
81 if webtoken is None:
82 web.execute("""
83 INSERT INTO reg_tokens
84 (subscriber_id, extension, reg_completed, name, email, lac, imsi, token, tmsi)
85 VALUES
86 (?, ?, 0, ?, '', ?, ?, ?, ?)
87 """, (x['subscriber_id'], subscr['extension'], subscr['name'],
88 subscr['lac'], str(subscr['imsi']), x['token'], subscr['tmsi']))
89
90# authorize subscribers
91with hlr:
92 for x in web_tokens_by_subscr_id.values():
93 subscr = hlr_subscrs_by_id.get(x['subscriber_id'], None)
94 if x['reg_completed'] and not subscr['authorized']:
95 hlr.execute("""
96 UPDATE Subscriber
97 SET authorized = 1
98 WHERE id = ?
99 """, (x['subscriber_id'],))
100
101hlr.close()
102web.close()
103