Created
February 21, 2011 11:02
-
-
Save oinume/836927 to your computer and use it in GitHub Desktop.
Convert all tables to InnoDB
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# INSTALL ==================== | |
# $ sudo easy_install argparse | |
# $ sudo easy_install mysql-python | |
import argparse | |
import MySQLdb | |
import os | |
def main(): | |
# setup command line options | |
parser = argparse.ArgumentParser( | |
prog = os.path.basename(__file__), | |
description = 'Convert all tables to InnoDB', | |
conflict_handler = 'resolve' | |
) | |
parser.add_argument( | |
'database', metavar='DATABASE', nargs=1, | |
help='database name.', | |
) | |
parser.add_argument( | |
'-h', '--host', metavar='HOST', default='localhost', | |
help='host name', | |
) | |
parser.add_argument( | |
'-u', '--user', metavar='USER', default='root', | |
help='user' | |
) | |
parser.add_argument( | |
'-p', '--password', metavar='PASSWORD', default='', | |
help='password' | |
) | |
parser.add_argument( | |
'-P', '--port', metavar='PORT', type=int, default=3306, | |
help='port' | |
) | |
options = parser.parse_args() | |
db = MySQLdb.connect( | |
host = options.host, | |
port = options.port, | |
user = options.user, | |
passwd = options.password, | |
db = options.database[0]) | |
cursor = db.cursor() | |
# get all table names | |
cursor.execute("SHOW TABLES") | |
for row in cursor.fetchall(): | |
table = row[0] | |
print("--- %s ---" % (table)) | |
cursor.execute("ALTER TABLE %s ENGINE=InnoDB" % (table)) | |
cursor.close() | |
db.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment