|
8 | 8 | # Mysql backend for roundup |
9 | 9 | # |
10 | 10 |
|
| 11 | +from roundup import hyperdb |
11 | 12 | from roundup.backends.rdbms_common import * |
12 | 13 | from roundup.backends import rdbms_common |
13 | 14 | import MySQLdb |
14 | 15 | import os, shutil |
15 | 16 | from MySQLdb.constants import ER |
16 | 17 |
|
17 | | -class Maintenance: |
18 | | - """ Database maintenance functions """ |
19 | | - def db_nuke(self, config): |
20 | | - """Clear all database contents and drop database itself""" |
21 | | - db = Database(config, 'admin') |
| 18 | +def db_nuke(config): |
| 19 | + """Clear all database contents and drop database itself""" |
| 20 | + if db_exists(config): |
| 21 | + conn = MySQLdb.connect(config.MYSQL_DBHOST, config.MYSQL_DBUSER, |
| 22 | + config.MYSQL_DBPASSWORD) |
22 | 23 | try: |
23 | | - db.sql_commit() |
24 | | - db.sql("DROP DATABASE %s" % config.MYSQL_DBNAME) |
25 | | - db.sql("CREATE DATABASE %s" % config.MYSQL_DBNAME) |
26 | | - finally: |
27 | | - db.close() |
28 | | - if os.path.exists(config.DATABASE): |
29 | | - shutil.rmtree(config.DATABASE) |
30 | | - |
31 | | - def db_exists(self, config): |
32 | | - """Check if database already exists""" |
33 | | - # Yes, this is a hack, but we must must open connection without |
34 | | - # selecting a database to prevent creation of some tables |
35 | | - config.MYSQL_DATABASE = (config.MYSQL_DBHOST, config.MYSQL_DBUSER, config.MYSQL_DBPASSWORD) |
36 | | - db = Database(config, 'admin') |
| 24 | + conn.select_db(config.MYSQL_DBNAME) |
| 25 | + except: |
| 26 | + # no, it doesn't exist |
| 27 | + pass |
| 28 | + else: |
| 29 | + cursor = conn.cursor() |
| 30 | + cursor.execute("SHOW TABLES") |
| 31 | + tables = cursor.fetchall() |
| 32 | + for table in tables: |
| 33 | + if __debug__: |
| 34 | + print >>hyperdb.DEBUG, 'DROP TABLE %s'%table[0] |
| 35 | + cursor.execute("DROP TABLE %s"%table[0]) |
| 36 | + if __debug__: |
| 37 | + print >>hyperdb.DEBUG, "DROP DATABASE %s"%config.MYSQL_DBNAME |
| 38 | + cursor.execute("DROP DATABASE %s"%config.MYSQL_DBNAME) |
| 39 | + conn.commit() |
| 40 | + conn.close() |
| 41 | + |
| 42 | + if os.path.exists(config.DATABASE): |
| 43 | + shutil.rmtree(config.DATABASE) |
| 44 | + |
| 45 | +def db_create(config): |
| 46 | + """Create the database.""" |
| 47 | + conn = MySQLdb.connect(config.MYSQL_DBHOST, config.MYSQL_DBUSER, |
| 48 | + config.MYSQL_DBPASSWORD) |
| 49 | + cursor = conn.cursor() |
| 50 | + if __debug__: |
| 51 | + print >>hyperdb.DEBUG, "CREATE DATABASE %s"%config.MYSQL_DBNAME |
| 52 | + cursor.execute("CREATE DATABASE %s"%config.MYSQL_DBNAME) |
| 53 | + conn.commit() |
| 54 | + conn.close() |
| 55 | + |
| 56 | +def db_exists(config): |
| 57 | + """Check if database already exists.""" |
| 58 | + conn = MySQLdb.connect(config.MYSQL_DBHOST, config.MYSQL_DBUSER, |
| 59 | + config.MYSQL_DBPASSWORD) |
| 60 | +# tables = None |
| 61 | + try: |
37 | 62 | try: |
38 | | - db.conn.select_db(config.MYSQL_DBNAME) |
39 | | - config.MYSQL_DATABASE = (config.MYSQL_DBHOST, config.MYSQL_DBUSER, |
40 | | - config.MYSQL_DBPASSWORD, config.MYSQL_DBNAME) |
41 | | - db.sql("SHOW TABLES") |
42 | | - tables = db.sql_fetchall() |
43 | | - finally: |
44 | | - db.close() |
45 | | - if tables or os.path.exists(config.DATABASE): |
46 | | - return 1 |
47 | | - return 0 |
| 63 | + conn.select_db(config.MYSQL_DBNAME) |
| 64 | +# cursor = conn.cursor() |
| 65 | +# cursor.execute("SHOW TABLES") |
| 66 | +# tables = cursor.fetchall() |
| 67 | +# if __debug__: |
| 68 | +# print >>hyperdb.DEBUG, "tables %s"%(tables,) |
| 69 | + except MySQLdb.OperationalError: |
| 70 | + if __debug__: |
| 71 | + print >>hyperdb.DEBUG, "no database '%s'"%config.MYSQL_DBNAME |
| 72 | + return 0 |
| 73 | + finally: |
| 74 | + conn.close() |
| 75 | + if __debug__: |
| 76 | + print >>hyperdb.DEBUG, "database '%s' exists"%config.MYSQL_DBNAME |
| 77 | + return 1 |
48 | 78 |
|
49 | 79 | class Database(Database): |
50 | 80 | arg = '%s' |
51 | 81 |
|
52 | 82 | def open_connection(self): |
| 83 | + # make sure the database actually exists |
| 84 | + if not db_exists(self.config): |
| 85 | + db_create(self.config) |
| 86 | + |
53 | 87 | db = getattr(self.config, 'MYSQL_DATABASE') |
54 | 88 | try: |
55 | 89 | self.conn = MySQLdb.connect(*db) |
@@ -142,9 +176,6 @@ def create_multilink_table(self, spec, ml): |
142 | 176 | print >>hyperdb.DEBUG, 'create_class', (self, sql) |
143 | 177 | self.cursor.execute(sql) |
144 | 178 |
|
145 | | - # Static methods |
146 | | - nuke = Maintenance().db_nuke |
147 | | - exists = Maintenance().db_exists |
148 | 179 |
|
149 | 180 | class MysqlClass: |
150 | 181 | def find(self, **propspec): |
|
0 commit comments