Skip to content

Commit a0adbfe

Browse files
committed
migrate script
1 parent 2cdc8b9 commit a0adbfe

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

db_migrate.py

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# -*- coding:utf-8 -*-
2+
import MySQLdb
3+
import config
4+
import uuid
5+
import cStringIO
6+
7+
'''
8+
Q: 这个脚本都做了什么呀?
9+
A: 它把老的数据库转换成了新的数据库?
10+
11+
Q: 它都做了哪些转换呀?
12+
A: 它转换了用户,版面,帖子,楼,楼中楼和站内信,
13+
然后链接的转换也是很大的。
14+
15+
Q: 用户怎么转的呀?
16+
A: 老的用户表叫 userinfo,新的叫 users。
17+
没有什么特别要说的,除了我规定 gender 男生是 1 女生是 0。
18+
19+
Q: 版面怎么转的呀?
20+
A: 自己看吧~
21+
22+
Q: 帖子怎么转的呀?
23+
A: 新 tid 是按老 (bid, tid) 排序后重新编的。
24+
这里有个坑,就是老的 timestamp(最后更新) 是 unix 时间戳,
25+
我直接用了 from_unixtime 把它转成 MySQL 中的 datetime,
26+
可能会损失精度或者需要考虑时区问题。
27+
28+
Q: 楼怎么转的呀?
29+
A: 新 pid 是按老 (bid, tid, pid) 排序后重新编的。
30+
这里也有坑,坑 1 是两个时间的处理有上一条里说的问题。
31+
坑 2 是帖子签名档原来存的是“引用”现在直接存内容了,
32+
坑 3 是 parse_type 现在取值 'html' 或 'plain'。
33+
34+
Q: 楼中楼怎么转的呀?
35+
A: 无可奉告!
36+
37+
Q: 站内信怎么转的呀?
38+
A: 发送者不是 system 的认为是站内信。时间方面有前述的坑。
39+
40+
Q: 嗯好像大部分问题都问完了,然后就剩你说的很大那个,
41+
链接作了哪些替换?
42+
A: 哦这个呀,我们现在做的就是把以下四种格式的链接换成新链接:
43+
http://www.chexie.net/bbs/content/?bid=<bid>&tid=<tid>[&p=<p>]
44+
http://chexie.net/bbs/content/?bid=<bid>&tid=<tid>[&p=<p>]
45+
http://www.chexie.net/cgi-bin/bbs.pl?see=<see>&b=<b>[&p=<p>]
46+
http://chexie.net/cgi-bin/bbs.pl?see=<see>&b=<b>[&p=<p>]
47+
新链接格式是 /thread/<new_tid>/[page/<p>/]
48+
49+
Q: 嗯好吧好像没什么问题了,中秋快乐 ^_^
50+
A: 很惭愧,做了一些微小的工作,谢谢大家 Θ..Θ
51+
'''
52+
53+
db = MySQLdb.connect(host=config.DB_SERVER, passwd=config.DB_PASSWORD, db=config.DB_NAME, user=config.DB_USERNAME)
54+
55+
c = db.cursor()
56+
57+
c.execute("""insert into users(username, password, gender, avatar, intro, sig1, sig2, sig3, hobby, qq, mail, registration_date, last_login_time, num_post, num_reply, num_water, num_sign, current_board, user_agent) select username, password, sex = "男", icon, intro, sig1, sig2, sig3, hobby, qq, mail, regdate, lastdate, post, reply, water, sign, nowboard, logininfo from capubbs.userinfo""")
58+
59+
c.execute("""insert into boards(bid, name, invisible) select bid, bbstitle, hide from capubbs.boardinfo""")
60+
61+
c.execute("""alter table capubbs.threads add column new_tid integer""")
62+
c.execute("""update capubbs.threads, (select bid, tid, @cur_tid := if(@cur_tid is null, 1, @cur_tid + 1) as new_tid from capubbs.threads order by bid, tid) as ord set threads.new_tid = ord.new_tid where threads.bid = ord.bid and threads.tid = ord.tid""")
63+
c.execute("""insert into Threads(tid, author_uid, bid, title, replyer_uid, num_click, num_reply, good, sticky, created_at, replied_at) select new_tid, author.uid, bid, title, replier.uid, click, threads.reply, threads.extr, top, postdate, from_unixtime(timestamp) from (capubbs.threads as threads join users as author on threads.author = author.username) left join users as replier on replyer = replier.username""")
64+
65+
c.execute("""alter table capubbs.posts add column new_pid integer""")
66+
c.execute("""update capubbs.posts, (select bid, tid, pid, @cur_pid := if(@cur_pid is null, 1, @cur_pid + 1) as new_pid from capubbs.posts order by bid, tid, pid) as ord set posts.new_pid = ord.new_pid where posts.bid = ord.bid and posts.tid = ord.tid and posts.pid = ord.pid""")
67+
c.execute("""insert into posts(pid, uid, bid, tid, title, content, created_at, updated_at, signature, ip, parse_type) select new_pid, author.uid, posts.bid, threads.new_tid, posts.title, text, from_unixtime(replytime), from_unixtime(updatetime), case posts.sig when 1 then author.sig1 when 2 then author.sig2 when 3 then author.sig3 end, ip, case ishtml when 'YES' then 'html' else 'plain' end from capubbs.posts join users as author on posts.author = author.username join capubbs.threads on threads.bid = posts.bid and threads.tid = posts.tid""")
68+
69+
c.execute("""insert into comments(cid, pid, uid, content, time, deleted) select lzl.id, posts.new_pid, users.uid, lzl.text, from_unixtime(time), !lzl.visible from capubbs.lzl join capubbs.posts on lzl.fid = posts.fid join users on lzl.author = users.username""")
70+
71+
c.execute("""insert into messages(mid, sender_uid, receiver_uid, content, time, is_read, sender_deleted, receiver_deleted) select id, sender.uid, receiver.uid, text, from_unixtime(time), hasread, 0, 0 from capubbs.messages join users as sender on messages.sender = sender.username join users as receiver on messages.receiver = receiver.username where sender != 'system'""")
72+
73+
#c.execute("""insert into notifications(nid, uid, time, type, pid, is_read) select id, receiver.uid, from_unixtime(time), case messages.text when 'reply' then 1 when 'at' then 2 when 'replylzl' then 3 when 'replylzlreply' then 4 when 'quote' then 5 end, new_pid, hasread from capubbs.messages join users as receiver on messages.receiver = receiver.username join capubbs.posts on messages.rbid = posts.bid and messages.rtid = posts.tid where sender = 'system'""")
74+
75+
c.close()
76+
77+
78+
c2 = db.cursor()
79+
c3 = db.cursor()
80+
81+
def parse_args(s, pos, begin):
82+
stat = 'k'
83+
d = {}
84+
k = ''
85+
v = ''
86+
while True:
87+
if pos >= len(s):
88+
d[k] = v
89+
break
90+
nxt = s[pos]
91+
pos += 1
92+
if stat == 'k':
93+
if nxt.isalnum() or nxt == '_':
94+
k += nxt
95+
elif nxt == '=':
96+
stat = 'v'
97+
else:
98+
break
99+
raise KeyError
100+
elif stat == 'v':
101+
if nxt.isalnum() or nxt == '_':
102+
v += nxt
103+
elif nxt == '&':
104+
if s[pos:pos + 4] == 'amp;':
105+
pos += 4
106+
d[k] = v
107+
stat = 'k'
108+
k = ''
109+
v = ''
110+
else:
111+
d[k] = v
112+
break
113+
return (begin, pos, d)
114+
115+
def parse_url(s, pattern):
116+
res = []
117+
pos = s.find(pattern)
118+
if pos == -1:
119+
return None
120+
while pos != -1:
121+
res.append(parse_args(s, pos + len(pattern), pos))
122+
pos = s.find(pattern, pos + 1)
123+
return res
124+
125+
tbl_name = "tbl_" + uuid.uuid4().hex
126+
c3.execute("""create table %s (bid integer, tid integer, n integer primary key auto_increment)""" % tbl_name)
127+
128+
def tihuan(pattern, proc):
129+
c2.execute("""select pid, content from posts where locate('%s', content) > 0 order by pid desc""" % pattern)
130+
res = c2.fetchall()
131+
actions = []
132+
lookups = []
133+
for x in res:
134+
ca = (x, parse_url(x[1], pattern))
135+
if ca[1] == None:
136+
actions.append(None)
137+
else:
138+
ca1 = []
139+
for y in ca[1]:
140+
p = proc(y[2])
141+
if p:
142+
lookups.append(p)
143+
ca1.append(y)
144+
actions.append((x, ca1))
145+
146+
c3.executemany("""insert into """ + tbl_name + """(bid, tid) values (%s, %s)""", lookups)
147+
c3.execute("""select * from """ + tbl_name)
148+
c3.execute("""select capubbs.threads.new_tid, capubbs.threads.tid, capubbs.threads.bid, n from %s left join capubbs.threads on %s.bid = capubbs.threads.bid and %s.tid = capubbs.threads.tid order by n""" % (tbl_name, tbl_name, tbl_name))
149+
res = c3.fetchall()
150+
updates = []
151+
rc = 0
152+
for x in actions:
153+
last = 0
154+
o = cStringIO.StringIO()
155+
s = x[0][1]
156+
for a in x[1]:
157+
o.write(s[last:a[0]])
158+
o.write('/thread/')
159+
o.write(str(res[rc][0]))
160+
o.write('/')
161+
if 'p' in x[1][0][2]:
162+
o.write('page/')
163+
o.write(x[1][0][2]['p'])
164+
o.write('/')
165+
last = a[1] - 1
166+
o.write(s[last:])
167+
updates.append((o.getvalue(), x[0][0]))
168+
o.close()
169+
170+
c3.executemany("""update posts set content = %s where pid = %s""", updates)
171+
172+
def decode26(s):
173+
return reduce(lambda tot, x: tot * 26 + ord(x) - ord('a'), s, 0) + 1
174+
175+
tihuan("""http://www.chexie.net/bbs/content/?""", lambda x: (x['bid'], x['tid']) if ('bid' in x) and ('tid' in x) else None)
176+
tihuan("""http://chexie.net/bbs/content/?""", lambda x: (x['bid'], x['tid']) if ('bid' in x) and ('tid' in x) else None)
177+
tihuan("""http://www.chexie.net/cgi-bin/bbs.pl?""", lambda x: (x['b'], decode26(x['see'])) if ('b' in x) and (x['b'] != '') and ('see' in x) else None)
178+
tihuan("""http://chexie.net/cgi-bin/bbs.pl?""", lambda x: (x['b'], decode26(x['see'])) if ('b' in x) and (x['b'] != '') and ('see' in x) else None)
179+
c3.execute("""drop table %s""" % tbl_name)

0 commit comments

Comments
 (0)