]> andersk Git - sql.git/blobdiff - lib/python/sql/util.py
more old code
[sql.git] / lib / python / sql / util.py
diff --git a/lib/python/sql/util.py b/lib/python/sql/util.py
new file mode 100755 (executable)
index 0000000..7f05b20
--- /dev/null
@@ -0,0 +1,57 @@
+#!/usr/bin/python
+
+def get_backup_exceptions(path='/mit/sql/etc/db_no-daily-backup'):
+    return filter(len, map(str.strip, file(path).read().split('\n')))
+
+import MySQLdb, MySQLdb.cursors
+
+def use_db(cursor, db):
+    cursor.execute('USE `%s`' % db)
+
+def get_dbs(cursor):
+    cursor.execute('SHOW DATABASES')
+    return [x.values()[0] for x in cursor.fetchall() if x.values()[0] != 'information_schema']
+
+def get_db_tables(cursor, db):
+    cursor.execute('SHOW TABLES FROM `%s`' % db)
+    return [(db, x.values()[0]) for x in cursor.fetchall()]
+
+def get_db_tables_ft(cursor, db):
+    r = []
+    for table in get_db_tables(cursor, db):
+        cursor.execute('SHOW INDEXES FROM `%s`.`%s`' % table)
+        r.extend(set([(db, x['Table']) for x in cursor.fetchall() if x['Index_type'] == 'FULLTEXT']))
+    return r
+
+def get_db_tables_status(cursor, db):
+    cursor.execute('SHOW TABLE STATUS FROM `%s`' % db)
+    return dict([((db, x['Name']), x) for x in cursor.fetchall()])
+
+def get_db_tables_engines(cursor, db):
+    return [(table, status['Engine']) for table, status in get_db_tables_status(cursor, db).items()]
+
+def repair_table_quick(cursor, table):
+    # REPAIR TABLE tbl QUICK
+    # works when changing ft_min_word_len
+    cursor.execute('REPAIR TABLE `%s`.`%s` QUICK' % table)
+
+if __name__ == '__main__':
+    import sys
+
+    cursor = MySQLdb.connect('localhost', read_default_group='client', cursorclass=MySQLdb.cursors.DictCursor).cursor()
+    #for db in get_dbs(cursor):
+    #    for table in get_db_tables_ft(cursor, db):
+    #        print table
+
+    #db = 'freeculture+youtomb'
+    #print get_db_tables_status(cursor, db)
+    #sys.exit(0)
+
+    while True:
+        data = sys.stdin.readline()
+        if not data:
+            break
+        data = data.strip()
+        table = eval(data)
+        print table
+        repair_table_quick(cursor, table)
This page took 0.326218 seconds and 4 git commands to generate.