]> andersk Git - sql.git/blob - lib/python/sql/util.py
7f05b20b04830c59e8d3cc27d810eb65f934aca2
[sql.git] / lib / python / sql / util.py
1 #!/usr/bin/python
2
3 def get_backup_exceptions(path='/mit/sql/etc/db_no-daily-backup'):
4     return filter(len, map(str.strip, file(path).read().split('\n')))
5
6 import MySQLdb, MySQLdb.cursors
7
8 def use_db(cursor, db):
9     cursor.execute('USE `%s`' % db)
10
11 def get_dbs(cursor):
12     cursor.execute('SHOW DATABASES')
13     return [x.values()[0] for x in cursor.fetchall() if x.values()[0] != 'information_schema']
14
15 def get_db_tables(cursor, db):
16     cursor.execute('SHOW TABLES FROM `%s`' % db)
17     return [(db, x.values()[0]) for x in cursor.fetchall()]
18
19 def get_db_tables_ft(cursor, db):
20     r = []
21     for table in get_db_tables(cursor, db):
22         cursor.execute('SHOW INDEXES FROM `%s`.`%s`' % table)
23         r.extend(set([(db, x['Table']) for x in cursor.fetchall() if x['Index_type'] == 'FULLTEXT']))
24     return r
25
26 def get_db_tables_status(cursor, db):
27     cursor.execute('SHOW TABLE STATUS FROM `%s`' % db)
28     return dict([((db, x['Name']), x) for x in cursor.fetchall()])
29
30 def get_db_tables_engines(cursor, db):
31     return [(table, status['Engine']) for table, status in get_db_tables_status(cursor, db).items()]
32
33 def repair_table_quick(cursor, table):
34     # REPAIR TABLE tbl QUICK
35     # works when changing ft_min_word_len
36     cursor.execute('REPAIR TABLE `%s`.`%s` QUICK' % table)
37
38 if __name__ == '__main__':
39     import sys
40
41     cursor = MySQLdb.connect('localhost', read_default_group='client', cursorclass=MySQLdb.cursors.DictCursor).cursor()
42     #for db in get_dbs(cursor):
43     #    for table in get_db_tables_ft(cursor, db):
44     #        print table
45
46     #db = 'freeculture+youtomb'
47     #print get_db_tables_status(cursor, db)
48     #sys.exit(0)
49
50     while True:
51         data = sys.stdin.readline()
52         if not data:
53             break
54         data = data.strip()
55         table = eval(data)
56         print table
57         repair_table_quick(cursor, table)
This page took 0.027348 seconds and 3 git commands to generate.