Projet

Général

Profil

Paste
Télécharger (803 octets) Statistiques
| Branche: | Révision:

root / other-scripts / one-use / rm-prefix.py @ 3eef768d

1
#!/usr/bin/env python3
2

    
3
# This was created in order to delete the prefix of drupal database
4
# These prefix were useful when all sites were in the same database
5

    
6
import pymysql as mysql
7
import sys
8
import re
9

    
10
db = sys.argv[1]
11
prefix = sys.argv[2]
12
host = 'localhost'
13
passwd = 'tata'
14
user = 'root'
15
port = ''
16

    
17
db = mysql.connect(host=host, user=user, passwd=passwd, db=db, charset='utf8')
18
with db:
19
    cur = db.cursor()
20
    cur.execute('SHOW TABLES')
21
    tables = cur.fetchall()
22
    for table in tables:
23
        table = table[0]
24
        if re.match('^{}'.format(prefix), table):
25
            new_table = re.sub('^{}(.*)'.format(prefix), r'\1', table)
26
            cur.execute('DROP TABLE IF EXISTS {}'.format(new_table))
27
            cur.execute('RENAME TABLE {} TO {}'.format(table, new_table))
28
    db.commit()