comparison roundup/backends/back_postgresql.py @ 5319:62de601bdf6f

Fix commits although a Reject exception is raised Fix the problem that changes are committed to the database (due to commits to otk handling) even when a Reject exception occurs. The fix implements separate database connections for otk/session handling and normal database operation.
author Ralf Schlatterbeck <rsc@runtux.com>
date Fri, 20 Apr 2018 18:46:28 +0200
parents 198b6e810c67
children 5cb6e6b594b0
comparison
equal deleted inserted replaced
5318:506c7ee9a385 5319:62de601bdf6f
50 if config.RDBMS_TEMPLATE : 50 if config.RDBMS_TEMPLATE :
51 command = command + " TEMPLATE=%s" % config.RDBMS_TEMPLATE 51 command = command + " TEMPLATE=%s" % config.RDBMS_TEMPLATE
52 logging.getLogger('roundup.hyperdb').info(command) 52 logging.getLogger('roundup.hyperdb').info(command)
53 db_command(config, command) 53 db_command(config, command)
54 54
55 def db_nuke(config, fail_ok=0): 55 def db_nuke(config):
56 """Clear all database contents and drop database itself""" 56 """Clear all database contents and drop database itself"""
57 command = 'DROP DATABASE "%s"'% config.RDBMS_NAME 57 command = 'DROP DATABASE "%s"'% config.RDBMS_NAME
58 logging.getLogger('roundup.hyperdb').info(command) 58 logging.getLogger('roundup.hyperdb').info(command)
59 db_command(config, command) 59 db_command(config, command)
60 60
149 dbtype = "postgres" 149 dbtype = "postgres"
150 150
151 # used by some code to switch styles of query 151 # used by some code to switch styles of query
152 implements_intersect = 1 152 implements_intersect = 1
153 153
154 def getSessionManager(self):
155 return Sessions(self)
156
157 def sql_open_connection(self): 154 def sql_open_connection(self):
158 db = connection_dict(self.config, 'database') 155 db = connection_dict(self.config, 'database')
159 logging.getLogger('roundup.hyperdb').info( 156 logging.getLogger('roundup.hyperdb').info(
160 'open database %r'%db['database']) 157 'open database %r'%db['database'])
161 try: 158 try:
185 self.init_dbschema() 182 self.init_dbschema()
186 self.sql("CREATE TABLE schema (schema TEXT)") 183 self.sql("CREATE TABLE schema (schema TEXT)")
187 self.sql("CREATE TABLE dual (dummy integer)") 184 self.sql("CREATE TABLE dual (dummy integer)")
188 self.sql("insert into dual values (1)") 185 self.sql("insert into dual values (1)")
189 self.create_version_2_tables() 186 self.create_version_2_tables()
187 # Need to commit here, otherwise otk/session will not find
188 # the necessary tables (in a parallel connection!)
189 self.commit()
190 190
191 def create_version_2_tables(self): 191 def create_version_2_tables(self):
192 # OTK store 192 # OTK store
193 self.sql('''CREATE TABLE otks (otk_key VARCHAR(255), 193 self.sql('''CREATE TABLE otks (otk_key VARCHAR(255),
194 otk_value TEXT, otk_time REAL)''') 194 otk_value TEXT, otk_time REAL)''')
242 self.sql('ALTER TABLE _%s add __actor VARCHAR(255)'%name) 242 self.sql('ALTER TABLE _%s add __actor VARCHAR(255)'%name)
243 243
244 def __repr__(self): 244 def __repr__(self):
245 return '<roundpsycopgsql 0x%x>' % id(self) 245 return '<roundpsycopgsql 0x%x>' % id(self)
246 246
247 def sql_commit(self, fail_ok=False):
248 ''' Actually commit to the database.
249 '''
250 logging.getLogger('roundup.hyperdb').info('commit')
251
252 try:
253 self.conn.commit()
254 except ProgrammingError as message:
255 # we've been instructed that this commit is allowed to fail
256 if fail_ok and str(message).endswith('could not serialize '
257 'access due to concurrent update'):
258 logging.getLogger('roundup.hyperdb').info(
259 'commit FAILED, but fail_ok')
260 else:
261 raise
262
263 # open a new cursor for subsequent work
264 self.cursor = self.conn.cursor()
265
266 def sql_stringquote(self, value): 247 def sql_stringquote(self, value):
267 ''' psycopg.QuotedString returns a "buffer" object with the 248 ''' psycopg.QuotedString returns a "buffer" object with the
268 single-quotes around it... ''' 249 single-quotes around it... '''
269 return str(QuotedString(str(value)))[1:-1] 250 return str(QuotedString(str(value)))[1:-1]
270 251

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