rebuild_sqlitedb.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """
4 usage: %(progname)s [db filenames...]
5 """
6 
7 
8 import os, sys, string, time, getopt
9 from log import *
10 
11 def dumpTable2(path, fn, tableName, fields):
12  tfn = os.path.join(path, tableName + '.sql')
13  warn("dumpTables", tfn)
14  cmd = "sqlite3 %s '.schema %s' > %s" % (fn, tableName, tfn)
15  warn(cmd)
16  r = os.system(cmd)
17  if r != 0:
18  warn("return=", r)
19  return False
20 
21  import sqlite3
22  conn = sqlite3.connect(fn)
23  c = conn.cursor()
24 
25 
26  fieldList = []
27  for field in fields:
28  fieldList.append("%s > ''" % field)
29 
30  c.execute("SELECT %s FROM %s WHERE %s" % (string.join(fields, ","), tableName, string.join(fieldList, " AND ")))
31  rows = c.fetchall()
32  print len(rows)
33 
34  rows.sort()
35 
36  fp = open(tfn, "a")
37 
38  for row in rows:
39  mnum = row[0]
40  fieldList = []
41  i = 0
42  for field in fields:
43  fieldList.append("%s='%s'" % (field, row[i]))
44  i = i + 1
45 
46  open(".currentRow", "w").write("%s\n" % string.join(fieldList))
47  try:
48  c.execute("select * from %s where %s" % (tableName, string.join(fieldList, " AND ")))
49  except:
50  warn("Error with", row)
51  continue
52 
53  row = c.fetchone()
54 
55  parts = []
56  i = 0
57  for col in row:
58  if type(col) == type(None):
59  parts.append("NULL")
60  elif type(col) == type(1):
61  parts.append(str(col))
62  else:
63  if c.rs.col_defs[i][0] == "odb_value": col = ""
64  col = string.replace(col,"'","''")
65  parts.append("'" + col + "'")
66  i = i + 1
67  sql = "INSERT INTO %s VALUES (%s);" % (tableName, string.join(parts, ", "))
68  fp.write(sql + "\n")
69  fp.flush()
70 
71  # print mnum, len(rows)
72  fp.close()
73  return True
74 
75 
76 def dumpTable(path, fn, tableName):
77  tfn = os.path.join(path, tableName + '.sql')
78  warn("dumpTables", tfn)
79  cmd = "sqlite3 %s '.dump %s' > %s" % (fn, tableName, tfn)
80  warn(cmd)
81  r = os.system(cmd)
82  if r != 0:
83  warn("return=", r)
84  return False
85  return True
86 
87 def loadTable(path,fn, tableName):
88  warn("loadTables", tableName)
89 
90  tfn = os.path.join(path, tableName + '.sql')
91 
92  cmd = "sqlite3 %s.new < %s" % (fn, tfn)
93  warn(cmd)
94  r = os.system(cmd)
95  if r != 0:
96  warn("return=", r)
97  return False
98 
99  return True
100 
102  if not os.path.exists(fn): return
103 
104  path, f = os.path.split(fn)
105 
106  dumpPath = os.path.join(path, "dump")
107  try: os.mkdir(dumpPath, 0700)
108  except os.error, reason: pass
109 
110  cmd = "sqlite3 %s .tables" % fn
111  fp = os.popen(cmd, "r")
112  body = fp.read()
113  fp.close()
114 
115  tables = body.split()
116 
117  #tables = ["mb_msgdata", "mb_msgthread"]
118  #tables = ["mb_msgthread"]
119 
120  for tableName in tables:
121  if tableName == "mb_msgdata":
122  ret = dumpTable2(dumpPath, fn, tableName, fields=('mnum', ))
123  elif tableName == "mb_thread":
124  ret = dumpTable2(dumpPath, fn, tableName, fields=('thr_id', 'mnum'))
125  else:
126  ret = dumpTable(dumpPath, fn, tableName)
127  if ret is False: return
128 
129  try: os.unlink(fn + ".new")
130  except os.error, reason: pass
131 
132  for tableName in tables:
133  ret = loadTable(dumpPath, fn, tableName)
134  if ret is False: return
135 
136  now = int(time.time())
137  dateStr = time.strftime("%Y%m%d_%H%M%S", time.localtime(now))
138  warn(dateStr)
139 
140  os.rename(fn, fn + "_" + dateStr)
141  os.rename(fn + ".new", fn)
142 
143 
144 def test():
145  pass
146 
147 def usage(progname):
148  print __doc__ % vars()
149 
150 def main(argv, stdout, environ):
151  progname = argv[0]
152  optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])
153 
154  testflag = 0
155  if len(args) == 0:
156  usage(progname)
157  return
158  for (field, val) in optlist:
159  if field == "--help":
160  usage(progname)
161  return
162  elif field == "--debug":
163  debugfull()
164  elif field == "--test":
165  testflag = 1
166 
167  if testflag:
168  test()
169  return
170 
171  for fn in args:
172  rebuildDatabase(fn)
173 
174 
175 
176 if __name__ == "__main__":
177  main(sys.argv, sys.stdout, os.environ)
def loadTable(path, fn, tableName)
def dumpTable(path, fn, tableName)
def debugfull()
Definition: log.py:120
def warn(args)
Definition: log.py:100
def dumpTable2(path, fn, tableName, fields)
def main(argv, stdout, environ)


pyclearsilver
Author(s): Scott Noob Hassan
autogenerated on Mon Jun 10 2019 15:51:13