1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-04-28 03:10:45 +00:00
seahub/batch-delete.py

55 lines
1.5 KiB
Python
Raw Normal View History

#!/usr/bin/python
2012-03-23 03:01:56 +00:00
# -*- coding: utf-8 -*-
import os
import sys
2012-03-23 03:01:56 +00:00
import MySQLdb
import settings
try:
dbname = os.environ['DBNAME']
dbuser = os.environ['DBUSER']
dbpasswd = os.environ['DBPASSWD']
except:
print 'Environment not set! Exit'
sys.exit(1)
2012-03-23 03:01:56 +00:00
def do_create():
2012-03-23 06:09:06 +00:00
root_passwd = raw_input("Please enter root password to create database %s: " % dbname)
conn = MySQLdb.Connect(host='localhost', user='root', passwd=root_passwd)
2012-03-23 03:01:56 +00:00
cursor = conn.cursor()
2012-03-23 06:09:06 +00:00
create_cmd = ( "CREATE DATABASE IF NOT EXISTS `%s` default charset utf8 COLLATE utf8_general_ci;") % (dbname)
grant_cmd = ("grant all privileges on %s.* to '%s'@localhost identified by '%s';") % (dbname, dbuser, dbpasswd)
2012-03-23 03:01:56 +00:00
try:
2012-03-23 06:09:06 +00:00
cursor.execute(create_cmd)
cursor.execute(grant_cmd)
2012-03-23 03:01:56 +00:00
except:
pass
cursor.close()
conn.close()
def do_delete(prefix):
cmd = ('echo "select concat(\'drop table \', table_name ,\';\') from TABLES where TABLE_SCHEMA =\'%s\' and table_name like \'%s_%%\' ;" | mysql -u %s -p%s information_schema | sed -n \'2,$p\' | mysql -u %s -p%s %s') % (dbname, prefix, dbuser, dbpasswd, dbuser, dbpasswd, dbname)
try:
output = os.popen(cmd).read()
except:
pass
if __name__=="__main__":
2012-03-23 03:01:56 +00:00
# create database if not exists
do_create()
# detele all seahub tables
for app in settings.INSTALLED_APPS:
app_name = app.split('.')[-1]
do_delete(app_name)
do_delete('django')
print '[Delete seahub tables...Done]'