comparison roundup/backends/back_postgresql.py @ 4357:13b3155869e0

Beginnings of a big code cleanup / modernisation to make 2to3 happy
author Richard Jones <richard@users.sourceforge.net>
date Mon, 22 Feb 2010 05:26:57 +0000
parents 5e3991670011
children 9655a1b65974
comparison
equal deleted inserted replaced
4356:05a65559d873 4357:13b3155869e0
25 from roundup.backends import sessions_rdbms 25 from roundup.backends import sessions_rdbms
26 26
27 def connection_dict(config, dbnamestr=None): 27 def connection_dict(config, dbnamestr=None):
28 ''' read_default_group is MySQL-specific, ignore it ''' 28 ''' read_default_group is MySQL-specific, ignore it '''
29 d = rdbms_common.connection_dict(config, dbnamestr) 29 d = rdbms_common.connection_dict(config, dbnamestr)
30 if d.has_key('read_default_group'): 30 if 'read_default_group' in d:
31 del d['read_default_group'] 31 del d['read_default_group']
32 if d.has_key('read_default_file'): 32 if 'read_default_file' in d:
33 del d['read_default_file'] 33 del d['read_default_file']
34 return d 34 return d
35 35
36 def db_create(config): 36 def db_create(config):
37 """Clear all database contents and drop database itself""" 37 """Clear all database contents and drop database itself"""
56 template1['database'] = 'template1' 56 template1['database'] = 'template1'
57 57
58 try: 58 try:
59 conn = psycopg.connect(**template1) 59 conn = psycopg.connect(**template1)
60 except psycopg.OperationalError, message: 60 except psycopg.OperationalError, message:
61 raise hyperdb.DatabaseError, message 61 raise hyperdb.DatabaseError(message)
62 62
63 conn.set_isolation_level(0) 63 conn.set_isolation_level(0)
64 cursor = conn.cursor() 64 cursor = conn.cursor()
65 try: 65 try:
66 for n in range(10): 66 for n in range(10):
67 if pg_command(cursor, command): 67 if pg_command(cursor, command):
68 return 68 return
69 finally: 69 finally:
70 conn.close() 70 conn.close()
71 raise RuntimeError, '10 attempts to create database failed' 71 raise RuntimeError('10 attempts to create database failed')
72 72
73 def pg_command(cursor, command): 73 def pg_command(cursor, command):
74 '''Execute the postgresql command, which may be blocked by some other 74 '''Execute the postgresql command, which may be blocked by some other
75 user connecting to the database, and return a true value if it succeeds. 75 user connecting to the database, and return a true value if it succeeds.
76 76
79 try: 79 try:
80 cursor.execute(command) 80 cursor.execute(command)
81 except psycopg.ProgrammingError, err: 81 except psycopg.ProgrammingError, err:
82 response = str(err).split('\n')[0] 82 response = str(err).split('\n')[0]
83 if response.find('FATAL') != -1: 83 if response.find('FATAL') != -1:
84 raise RuntimeError, response 84 raise RuntimeError(response)
85 else: 85 else:
86 msgs = [ 86 msgs = [
87 'is being accessed by other users', 87 'is being accessed by other users',
88 'could not serialize access due to concurrent update', 88 'could not serialize access due to concurrent update',
89 ] 89 ]
92 if response.find(msg) == -1: 92 if response.find(msg) == -1:
93 can_retry = 1 93 can_retry = 1
94 if can_retry: 94 if can_retry:
95 time.sleep(1) 95 time.sleep(1)
96 return 0 96 return 0
97 raise RuntimeError, response 97 raise RuntimeError(response)
98 return 1 98 return 1
99 99
100 def db_exists(config): 100 def db_exists(config):
101 """Check if database already exists""" 101 """Check if database already exists"""
102 db = connection_dict(config, 'database') 102 db = connection_dict(config, 'database')
133 db = connection_dict(self.config, 'database') 133 db = connection_dict(self.config, 'database')
134 logging.getLogger('hyperdb').info('open database %r'%db['database']) 134 logging.getLogger('hyperdb').info('open database %r'%db['database'])
135 try: 135 try:
136 conn = psycopg.connect(**db) 136 conn = psycopg.connect(**db)
137 except psycopg.OperationalError, message: 137 except psycopg.OperationalError, message:
138 raise hyperdb.DatabaseError, message 138 raise hyperdb.DatabaseError(message)
139 139
140 cursor = conn.cursor() 140 cursor = conn.cursor()
141 141
142 return (conn, cursor) 142 return (conn, cursor)
143 143
207 USING btree (_word, _textid)''') 207 USING btree (_word, _textid)''')
208 208
209 def add_actor_column(self): 209 def add_actor_column(self):
210 # update existing tables to have the new actor column 210 # update existing tables to have the new actor column
211 tables = self.database_schema['tables'] 211 tables = self.database_schema['tables']
212 for name in tables.keys(): 212 for name in tables:
213 self.sql('ALTER TABLE _%s add __actor VARCHAR(255)'%name) 213 self.sql('ALTER TABLE _%s add __actor VARCHAR(255)'%name)
214 214
215 def __repr__(self): 215 def __repr__(self):
216 return '<roundpsycopgsql 0x%x>' % id(self) 216 return '<roundpsycopgsql 0x%x>' % id(self)
217 217
269 269
270 def clear(self): 270 def clear(self):
271 rdbms_common.Database.clear(self) 271 rdbms_common.Database.clear(self)
272 272
273 # reset the sequences 273 # reset the sequences
274 for cn in self.classes.keys(): 274 for cn in self.classes:
275 self.cursor.execute('DROP SEQUENCE _%s_ids'%cn) 275 self.cursor.execute('DROP SEQUENCE _%s_ids'%cn)
276 self.cursor.execute('CREATE SEQUENCE _%s_ids'%cn) 276 self.cursor.execute('CREATE SEQUENCE _%s_ids'%cn)
277 277
278 class PostgresqlClass: 278 class PostgresqlClass:
279 order_by_null_values = '(%s is not NULL)' 279 order_by_null_values = '(%s is not NULL)'

Roundup Issue Tracker: http://roundup-tracker.org/