Mercurial > p > roundup > code
comparison roundup/backends/back_mysql.py @ 1434:b953750bdc04
trackers on mysql can be initialised
added mechanism for backends to detect and clear database
| author | Andrey Lebedev <kedder@users.sourceforge.net> |
|---|---|
| date | Sat, 15 Feb 2003 23:19:01 +0000 |
| parents | 8429095241d7 |
| children | 94e430ad4fdb 84b6d5b07626 |
comparison
equal
deleted
inserted
replaced
| 1433:8429095241d7 | 1434:b953750bdc04 |
|---|---|
| 9 # | 9 # |
| 10 | 10 |
| 11 from roundup.backends.rdbms_common import * | 11 from roundup.backends.rdbms_common import * |
| 12 from roundup.backends import rdbms_common | 12 from roundup.backends import rdbms_common |
| 13 import MySQLdb | 13 import MySQLdb |
| 14 import os, shutil | |
| 14 from MySQLdb.constants import ER | 15 from MySQLdb.constants import ER |
| 16 | |
| 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') | |
| 22 db.sql("DROP DATABASE %s" % config.MYSQL_DBNAME) | |
| 23 db.sql("CREATE DATABASE %s" % config.MYSQL_DBNAME) | |
| 24 if os.path.exists(config.DATABASE): | |
| 25 shutil.rmtree(config.DATABASE) | |
| 26 | |
| 27 def db_exists(self, config): | |
| 28 """Check if database already exists""" | |
| 29 # Yes, this is a hack, but we must must open connection without | |
| 30 # selecting a database to prevent creation of some tables | |
| 31 config.MYSQL_DATABASE = (config.MYSQL_DBHOST, config.MYSQL_DBUSER, config.MYSQL_DBPASSWORD) | |
| 32 db = Database(config, 'admin') | |
| 33 db.conn.select_db(config.MYSQL_DBNAME) | |
| 34 config.MYSQL_DATABASE = (config.MYSQL_DBHOST, config.MYSQL_DBUSER, config.MYSQL_DBPASSWORD, config.MYSQL_DBNAME) | |
| 35 db.sql("SHOW TABLES") | |
| 36 tables = db.sql_fetchall() | |
| 37 if tables or os.path.exists(config.DATABASE): | |
| 38 return 1 | |
| 39 return 0 | |
| 15 | 40 |
| 16 class Database(Database): | 41 class Database(Database): |
| 17 arg = '%s' | 42 arg = '%s' |
| 18 | 43 |
| 19 def open_connection(self): | 44 def open_connection(self): |
| 112 sql = '''CREATE TABLE `%s_%s` (linkid VARCHAR(255), | 137 sql = '''CREATE TABLE `%s_%s` (linkid VARCHAR(255), |
| 113 nodeid VARCHAR(255)) TYPE=BDB'''%(spec.classname, ml) | 138 nodeid VARCHAR(255)) TYPE=BDB'''%(spec.classname, ml) |
| 114 if __debug__: | 139 if __debug__: |
| 115 print >>hyperdb.DEBUG, 'create_class', (self, sql) | 140 print >>hyperdb.DEBUG, 'create_class', (self, sql) |
| 116 self.cursor.execute(sql) | 141 self.cursor.execute(sql) |
| 142 | |
| 143 # Static methods | |
| 144 nuke = Maintenance().db_nuke | |
| 145 exists = Maintenance().db_exists | |
| 117 | 146 |
| 118 class MysqlClass: | 147 class MysqlClass: |
| 119 def find(self, **propspec): | 148 def find(self, **propspec): |
| 120 '''Get the ids of nodes in this class which link to the given nodes. | 149 '''Get the ids of nodes in this class which link to the given nodes. |
| 121 | 150 |
| 172 query = 'select nodeid from %s_%s where linkid in (%s)'%( | 201 query = 'select nodeid from %s_%s where linkid in (%s)'%( |
| 173 self.classname, prop, s) | 202 self.classname, prop, s) |
| 174 self.db.sql(query, vals) | 203 self.db.sql(query, vals) |
| 175 l += [x[0] for x in self.db.sql_fetchall()] | 204 l += [x[0] for x in self.db.sql_fetchall()] |
| 176 if __debug__: | 205 if __debug__: |
| 177 print >>hyperdb.DEBUG, 'find ... ', l #db.sql("DROP DATABASE %s" % config.MYSQL_DBNAME) | 206 print >>hyperdb.DEBUG, 'find ... ', l |
| 178 | 207 |
| 179 # Remove duplicated ids | 208 # Remove duplicated ids |
| 180 d = {} | 209 d = {} |
| 181 for k in l: | 210 for k in l: |
| 182 d[k] = 1 | 211 d[k] = 1 |
| 189 class IssueClass(MysqlClass, rdbms_common.IssueClass): | 218 class IssueClass(MysqlClass, rdbms_common.IssueClass): |
| 190 pass | 219 pass |
| 191 class FileClass(MysqlClass, rdbms_common.FileClass): | 220 class FileClass(MysqlClass, rdbms_common.FileClass): |
| 192 pass | 221 pass |
| 193 | 222 |
| 194 def nuke(config): | 223 #vim: set et |
| 195 """ Clear all database contents and drop database itself""" | |
| 196 # Connect to db | |
| 197 db = Database(config, 'admin') | |
| 198 db.sql("DROP DATABASE %s" % config.MYSQL_DBNAME) | |
| 199 db.sql("CREATE DATABASE %s" % config.MYSQL_DBNAME) |
