-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigration.py
More file actions
executable file
·95 lines (80 loc) · 2.16 KB
/
migration.py
File metadata and controls
executable file
·95 lines (80 loc) · 2.16 KB
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
#!/usr/local/bin/python
import sys
import sqlite3
import subprocess
DEBUG = False
IN_DB = sys.argv[1]
OUT_DB = sys.argv[2]
in_con = sqlite3.connect(IN_DB)
out_con = sqlite3.connect(OUT_DB)
in_cur = in_con.cursor()
out_cur = out_con.cursor()
idx = {
"id": 0,
"url": 1,
"title": 2,
"visit_count": 3,
"typed_count": 4,
"last_visit_time": 5,
"hidden": 6,
}
cnt = 0
def _log_debug(msg):
if DEBUG:
print(msg)
def _log_info(msg):
print(msg)
def exec_cmd(cmd):
result = subprocess.check_output(cmd, shell=True)
result = str(result, "utf-8")
return result.strip("\n")
def update_item(fields):
in_cur.execute(
"update urls set title=?, visit_count=?, typed_count=?, last_visit_time=?, hidden=? where url=?",
(
fields[idx["title"]],
1720,
fields[idx["typed_count"]],
fields[idx["last_visit_time"]],
fields[idx["hidden"]],
fields[idx["url"]],
),
)
def insert_item(fields):
in_cur.execute(
"insert into urls (url, title, visit_count, typed_count, last_visit_time, hidden) values (?, ?, ?, ?, ?, ?)",
(
fields[idx["url"]],
fields[idx["title"]],
fields[idx["visit_count"]],
fields[idx["typed_count"]],
fields[idx["last_visit_time"]],
fields[idx["hidden"]],
),
)
def dispose_rows(rows):
global cnt
for row in rows:
cnt = cnt + 1
in_cur.execute("select * from urls where url=:url", {"url": row[idx["url"]]})
fields = in_cur.fetchone()
if fields != None and len(fields) > 0:
update_item(row)
if cnt % 100 == 0:
print("u", end="", flush=True)
else:
insert_item(row)
if cnt % 100 == 0:
print("i", end="")
if __name__ == "__main__":
batch = 100
offset = 0
while True:
rows = out_cur.execute(
"select * from urls limit ?, ?", (offset, batch)
).fetchall()
dispose_rows(rows)
offset = offset + batch
if len(rows) < batch:
break
in_con.commit()