1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#!/usr/bin/env python
import os
import sys
import chardet
import json
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'scannerjammer.settings'
from db import db as DB
from pprint import pprint
import django
from datetime import datetime
from django.conf import settings
from django.contrib.auth.models import User
from backend.models import SJLike
from backend.models import SJRoom
from backend.models import SJContent
from backend.models import SJSearch
from backend.models import SJUserProfile
from backend.views import thraw
from backend.views import freeze
class MigrateDB(DB):
def __init__(self, *args, **kwargs):
super(MigrateDB, self).__init__(*args, **kwargs)
def get_table(self, name):
self.execute('SELECT * FROM %s' % name)
fields = [d[0] for d in self.cursor.description]
rows = self.cursor.fetchall()
for row in rows:
row = list(row)
for i, r in enumerate(row[:]):
if isinstance(r, str):
detect = chardet.detect(r)
try:
row[i] = unicode(r, detect['encoding'] or 'utf8', 'replace')
except LookupError:
row[i] = unicode(r, 'utf8', 'replace')
yield dict(zip(fields, row))
def get_radio_chat_table(self):
return self.get_table('radio_chat')
def get_search_log_table(self):
return self.get_table('search_log')
def get_sj_bg_table(self):
return self.get_table('sj_bg')
def get_sj_chat_table(self):
return self.get_table('sj_chat')
def get_sj_likes_table(self):
return self.get_table('sj_likes')
def get_sj_likes_tmp_table(self):
return self.get_table('sj_likes_tmp')
def get_sj_room_table(self):
return self.get_table('sj_room')
def get_sj_search_log_table(self):
return self.get_table('sj_search_log')
def get_sj_session_table(self):
return self.get_table('sj_sesson')
def get_sj_url_table(self):
return self.get_table('sj_url')
def get_sj_url_tmp_table(self):
return self.get_table('sj_url_tmp')
def get_sj_user_table(self):
return self.get_table('sj_user')
def get_sj_video_table(self):
return self.get_table('sj_video')
if __name__ == '__main__':
db = MigrateDB()
db.connect()
print "loading users..."
sj_user = list(db.get_sj_user_table())
map_user = {}
for row in sj_user:
try:
user = User.objects.get(username=row['username'])
except User.DoesNotExist:
print "ERROR in users '%s' does not exist" % row['username']
exit(0)
map_user[row['id']] = user
print "loading rooms..."
sj_room = list(db.get_sj_room_table())
map_room = {}
for row in sj_room:
try:
room = SJRoom.objects.get(name=row['name'])
except SJRoom.DoesNotExist:
print "ERROR in room '%s' does not exist" % row['name']
exit(0)
map_room[row['id']] = room
# Migrate sj_url table
sj_url = list(db.get_sj_url_table())
for row in sj_url:
if row['userid'] in map_user:
user = map_user[row['userid']]
else:
print "unknown user in migrate: '%d'" % row['userid']
continue
if row['roomid'] in map_room:
room = map_room[row['roomid']]
else:
print "unknown room in migrate: '%d'" % row['roomid']
continue
try:
url = SJContent.objects.get(
user=user,
datetime=datetime.fromtimestamp(row['date']),
room=room,
old_id=row['id'],
content_type='url')
print "url '%d' exist" % row['date']
except SJContent.DoesNotExist:
url = SJContent(
user=user,
datetime=datetime.fromtimestamp(row['date']),
room=room,
old_id=row['id'],
content_type='url')
print "url '%d' created" % row['date']
url.settings = dict(url=row['url'])
url.save()
|