Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. #!/usr/bin/python
  2. #-*- coding: utf8 *-
  3. # WARNING: GPL+BEER VARIATION LICENCED IF YOU USE, MODIFY OR SHARE THIS YOU OWE ME A BEER!
  4. #
  5. # Mail: felipeariasf@gmail.com
  6. #
  7. # Ugly style, no comments and very fast coded: No support, guarantee, future versions... use this under your own risk.
  8. import sys
  9. import MySQLdb
  10. import getopt, sys
  11. def start(base,fix):
  12.     keys={}
  13.     queryhnd=base.query("show tables")
  14.     result=base.store_result()
  15.     for i in xrange( result.num_rows()):
  16.         array=result.fetch_row()[0][0]
  17.     queryhnd2=base.query("show full columns from "+array)
  18.     result2=base.store_result()
  19.         for i in xrange(result2.num_rows()):
  20.         array2=result2.fetch_row()[0]
  21.         if array2[1].find("text")>-1 or array2[1].find("varchar")>-1:
  22.             queryhnd3=base.query("show index from "+array)
  23.             result3=base.store_result()
  24.             arrayindex=[]
  25.             c=0
  26.             for i in xrange(result3.num_rows()):
  27.             array3=result3.fetch_row()[0]
  28.             if array3[4]==array2[0]:
  29.                 if array3[10]=="BTREE" and array3[2]!="PRIMARY":
  30.                     base.query("drop index "+array3[2]+" on "+array)
  31.                     arrayindex=array3
  32.                     break
  33.             else: c+=1
  34.                 if arrayindex!=[] or c==result3.num_rows():
  35.             try:   
  36.                     for query in fix.split(","):    
  37.                 if query.find("blob")>-1:
  38.                         base.query("alter table "+array+" modify "+array2[0]+" "+query+";")    
  39.                     else:
  40.                     base.query("alter table "+array+" modify "+array2[0]+" "+array2[1]+" charset "+query+";")
  41.                 print "Column repaired: "+array2[0]+" on "+array
  42.             except:
  43.                 print "Error with "+array+" column "+array2[0]
  44.             if arrayindex!=[]:
  45.                     base.query("CREATE INDEX "+arrayindex[2]+" USING BTREE ON "+array+" ("+arrayindex[4]+");")
  46. def usage():
  47.     print """
  48.    || Fix tables script ||
  49.    
  50.    Options:
  51.    
  52.    -u - Database User
  53.    -p - Password
  54.    -c - Host to connect
  55.    -d - Database to connect
  56.    -f - Change sequence separated by commas (ex: -f blob,utf8,latin1)
  57.    ----------------------------
  58.    -h - This help screen
  59.    
  60.    Warning:
  61.     1. This won't touch any PRIMARY index
  62.     2. Some index values can be affected.
  63.        You may preffer to use this with a backup and export fixed data instead use it directly to the original database
  64.    
  65.    """
  66. def main():
  67.     global base
  68.     try:
  69.         opts, args = getopt.getopt(sys.argv[1:], "hu:p:c:d:f:v", ["help"])
  70.     except getopt.GetoptError, err:
  71.         print str(err)
  72.         usage()
  73.         sys.exit(2)
  74.     user = None
  75.     password = None
  76.     host = None
  77.     database = None
  78.     fix = None
  79.     verbose = False
  80.     for o, a in opts:
  81.         if o == "-v":
  82.             verbose = True
  83.         elif o in ("-h", "--help"):
  84.             usage()
  85.             sys.exit()
  86.         elif o in ("-u", "--user"):
  87.             user = a
  88.         print user
  89.         elif o in ("-p", "--password"):
  90.             password = a
  91.         elif o in ("-c", "--conhost"):
  92.             host = a
  93.         elif o in ("-d", "--database"):
  94.             database = a
  95.         elif o in ("-f", "--fix"):
  96.             fix = a
  97.         else:
  98.             assert False, "unhandled option"
  99.     if user!=None and host!=None and database !=None and fix!=None and password!=None:
  100.         base=MySQLdb.connect(host=host,user=user,passwd=password, db=database)
  101.         base.set_character_set("utf8") ## codificacion de caracteres
  102.         start(base,fix)
  103.     else:   usage()
  104. if __name__ == "__main__":
  105.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement