Mercurial > p > roundup > code
comparison roundup/backends/back_mysql.py @ 2075:b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Fri, 12 Mar 2004 04:09:00 +0000 |
| parents | 261c2e6ceb1e |
| children | 3e0961d6d44d |
comparison
equal
deleted
inserted
replaced
| 2073:261c2e6ceb1e | 2075:b1704ba7be41 |
|---|---|
| 13 from roundup.backends import rdbms_common | 13 from roundup.backends import rdbms_common |
| 14 import MySQLdb | 14 import MySQLdb |
| 15 import os, shutil | 15 import os, shutil |
| 16 from MySQLdb.constants import ER | 16 from MySQLdb.constants import ER |
| 17 | 17 |
| 18 # Database maintenance functions | 18 |
| 19 def db_nuke(config): | 19 def db_nuke(config): |
| 20 """Clear all database contents and drop database itself""" | 20 """Clear all database contents and drop database itself""" |
| 21 db = Database(config, 'admin') | 21 if db_exists(config): |
| 22 try: | 22 conn = MySQLdb.connect(config.MYSQL_DBHOST, config.MYSQL_DBUSER, |
| 23 db.sql_commit() | 23 config.MYSQL_DBPASSWORD) |
| 24 db.sql("DROP DATABASE %s" % config.MYSQL_DBNAME) | 24 try: |
| 25 db.sql("CREATE DATABASE %s" % config.MYSQL_DBNAME) | 25 conn.select_db(config.MYSQL_DBNAME) |
| 26 finally: | 26 except: |
| 27 db.close() | 27 # no, it doesn't exist |
| 28 pass | |
| 29 else: | |
| 30 cursor = conn.cursor() | |
| 31 cursor.execute("SHOW TABLES") | |
| 32 tables = cursor.fetchall() | |
| 33 for table in tables: | |
| 34 if __debug__: | |
| 35 print >>hyperdb.DEBUG, 'DROP TABLE %s'%table[0] | |
| 36 cursor.execute("DROP TABLE %s"%table[0]) | |
| 37 if __debug__: | |
| 38 print >>hyperdb.DEBUG, "DROP DATABASE %s"%config.MYSQL_DBNAME | |
| 39 cursor.execute("DROP DATABASE %s"%config.MYSQL_DBNAME) | |
| 40 conn.commit() | |
| 41 conn.close() | |
| 42 | |
| 28 if os.path.exists(config.DATABASE): | 43 if os.path.exists(config.DATABASE): |
| 29 shutil.rmtree(config.DATABASE) | 44 shutil.rmtree(config.DATABASE) |
| 30 | 45 |
| 46 def db_create(config): | |
| 47 """Create the database.""" | |
| 48 conn = MySQLdb.connect(config.MYSQL_DBHOST, config.MYSQL_DBUSER, | |
| 49 config.MYSQL_DBPASSWORD) | |
| 50 cursor = conn.cursor() | |
| 51 if __debug__: | |
| 52 print >>hyperdb.DEBUG, "CREATE DATABASE %s"%config.MYSQL_DBNAME | |
| 53 cursor.execute("CREATE DATABASE %s"%config.MYSQL_DBNAME) | |
| 54 conn.commit() | |
| 55 conn.close() | |
| 56 | |
| 31 def db_exists(config): | 57 def db_exists(config): |
| 32 """Check if database already exists""" | 58 """Check if database already exists.""" |
| 33 # Yes, this is a hack, but we must must open connection without | 59 conn = MySQLdb.connect(config.MYSQL_DBHOST, config.MYSQL_DBUSER, |
| 34 # selecting a database to prevent creation of some tables | 60 config.MYSQL_DBPASSWORD) |
| 35 config.MYSQL_DATABASE = (config.MYSQL_DBHOST, config.MYSQL_DBUSER, | 61 # tables = None |
| 36 config.MYSQL_DBPASSWORD) | |
| 37 db = Database(config, 'admin') | |
| 38 try: | 62 try: |
| 39 db.conn.select_db(config.MYSQL_DBNAME) | 63 try: |
| 40 config.MYSQL_DATABASE = (config.MYSQL_DBHOST, config.MYSQL_DBUSER, | 64 conn.select_db(config.MYSQL_DBNAME) |
| 41 config.MYSQL_DBPASSWORD, config.MYSQL_DBNAME) | 65 # cursor = conn.cursor() |
| 42 db.sql("SHOW TABLES") | 66 # cursor.execute("SHOW TABLES") |
| 43 tables = db.sql_fetchall() | 67 # tables = cursor.fetchall() |
| 68 # if __debug__: | |
| 69 # print >>hyperdb.DEBUG, "tables %s"%(tables,) | |
| 70 except MySQLdb.OperationalError: | |
| 71 if __debug__: | |
| 72 print >>hyperdb.DEBUG, "no database '%s'"%config.MYSQL_DBNAME | |
| 73 return 0 | |
| 44 finally: | 74 finally: |
| 45 db.close() | 75 conn.close() |
| 46 if tables or os.path.exists(config.DATABASE): | 76 if __debug__: |
| 47 return 1 | 77 print >>hyperdb.DEBUG, "database '%s' exists"%config.MYSQL_DBNAME |
| 48 return 0 | 78 return 1 |
| 79 | |
| 49 | 80 |
| 50 class Database(Database): | 81 class Database(Database): |
| 51 arg = '%s' | 82 arg = '%s' |
| 52 | 83 |
| 53 # Backend for MySQL to use. | 84 # Backend for MySQL to use. |
| 55 # use BDB to pass all unit tests. | 86 # use BDB to pass all unit tests. |
| 56 mysql_backend = 'InnoDB' | 87 mysql_backend = 'InnoDB' |
| 57 #mysql_backend = 'BDB' | 88 #mysql_backend = 'BDB' |
| 58 | 89 |
| 59 def sql_open_connection(self): | 90 def sql_open_connection(self): |
| 91 # make sure the database actually exists | |
| 92 if not db_exists(self.config): | |
| 93 db_create(self.config) | |
| 94 | |
| 60 db = getattr(self.config, 'MYSQL_DATABASE') | 95 db = getattr(self.config, 'MYSQL_DATABASE') |
| 61 try: | 96 try: |
| 62 self.conn = MySQLdb.connect(*db) | 97 self.conn = MySQLdb.connect(*db) |
| 63 except MySQLdb.OperationalError, message: | 98 except MySQLdb.OperationalError, message: |
| 64 raise DatabaseError, message | 99 raise DatabaseError, message |
| 73 if message[0] != ER.NO_DB_ERROR: | 108 if message[0] != ER.NO_DB_ERROR: |
| 74 raise | 109 raise |
| 75 except MySQLdb.ProgrammingError, message: | 110 except MySQLdb.ProgrammingError, message: |
| 76 if message[0] != ER.NO_SUCH_TABLE: | 111 if message[0] != ER.NO_SUCH_TABLE: |
| 77 raise DatabaseError, message | 112 raise DatabaseError, message |
| 78 self.database_schema = {} | 113 self.init_dbschema() |
| 79 self.sql("CREATE TABLE schema (schema TEXT) TYPE=%s"% | 114 self.sql("CREATE TABLE schema (schema TEXT) TYPE=%s"% |
| 80 self.mysql_backend) | 115 self.mysql_backend) |
| 81 # TODO: use AUTO_INCREMENT for generating ids: | 116 # TODO: use AUTO_INCREMENT for generating ids: |
| 82 # http://www.mysql.com/doc/en/CREATE_TABLE.html | 117 # http://www.mysql.com/doc/en/CREATE_TABLE.html |
| 83 self.sql("CREATE TABLE ids (name varchar(255), num INT) TYPE=%s"% | 118 self.sql("CREATE TABLE ids (name varchar(255), num INT) TYPE=%s"% |
| 84 self.mysql_backend) | 119 self.mysql_backend) |
| 85 self.sql("CREATE INDEX ids_name_idx ON ids(name)") | 120 self.sql("CREATE INDEX ids_name_idx ON ids(name)") |
| 86 self.create_version_2_tables() | 121 self.create_version_2_tables() |
| 87 | 122 |
| 88 def create_version_2_tables(self): | 123 def create_version_2_tables(self): |
| 89 self.cursor.execute('CREATE TABLE otks (key VARCHAR(255), ' | 124 self.cursor.execute('CREATE TABLE otks (otk_key VARCHAR(255), ' |
| 90 'value VARCHAR(255), __time FLOAT(20))') | 125 'otk_value VARCHAR(255), otk_time FLOAT(20))') |
| 91 self.cursor.execute('CREATE INDEX otks_key_idx ON otks(key)') | 126 self.cursor.execute('CREATE INDEX otks_key_idx ON otks(otk_key)') |
| 92 self.cursor.execute('CREATE TABLE sessions (key VARCHAR(255), ' | 127 self.cursor.execute('CREATE TABLE sessions (s_key VARCHAR(255), ' |
| 93 'last_use FLOAT(20), user VARCHAR(255))') | 128 's_last_use FLOAT(20), s_user VARCHAR(255))') |
| 94 self.cursor.execute('CREATE INDEX sessions_key_idx ON sessions(key)') | 129 self.cursor.execute('CREATE INDEX sessions_key_idx ON sessions(s_key)') |
| 95 | 130 |
| 96 def __repr__(self): | 131 def __repr__(self): |
| 97 return '<myroundsql 0x%x>'%id(self) | 132 return '<myroundsql 0x%x>'%id(self) |
| 98 | 133 |
| 99 def sql_fetchone(self): | 134 def sql_fetchone(self): |
