Created
October 29, 2015 17:41
-
-
Save theraphim/a75a1ef16f63137a4a3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from db4 import * | |
import re, sys | |
fd_re = re.compile("^(\S+)\((\d+)\)$", re.I) | |
cvt = { | |
"varchar" : "varbinary", | |
"char" : "binary", | |
"text" : "blob", | |
"tinytext" : "tinyblob", | |
"mediumtext": "mediumblob", | |
"longtext" : "longblob" | |
} | |
dbw = DBWrapper(user = 'root', passwd = sys.argv[2], db = sys.argv[1]) | |
db = dbw.New() | |
cs = sys.argv[3] | |
def t1(n): | |
rows, cursor = db.ExecuteRaw("explain %s" % n) | |
if rows: | |
_a = [] | |
_b = [] | |
for x in cursor: | |
m = fd_re.match(x["Type"]) | |
if m: | |
_t = m.group(1) | |
_l = "(%s)" % m.group(2) | |
else: | |
_t = x["Type"] | |
_l = "" | |
if _t in cvt.keys(): | |
_null = "NOT NULL" | |
if x["Null"]: | |
_null = "" | |
_default = "" | |
if x["Default"]: | |
_default = "DEFAULT '%s'" % x["Default"] | |
_a.append("modify `%s` %s%s %s %s" % (x["Field"], cvt[_t], _l, _null, _default)) | |
_b.append("modify `%s` %s%s CHARACTER SET %s %s %s" % (x["Field"], _t, _l, cs, _null, _default)) | |
if len(_a) > 0: | |
print "alter table `%s`\n%s;" % (n, ",\n".join(_a)) | |
print "alter table `%s`\n%s;" % (n, ",\n".join(_b)) | |
print "alter table `%s` default character set %s;" % (n, cs) | |
rows, cursor = db.ExecuteRaw("show tables") | |
if rows: | |
for x in cursor: | |
t1(x.values()[0]) | |
print "alter database `%s` default character set %s;" % (sys.argv[1], cs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment