Edgewall Software

Ticket #8575: update_sequence.patch

File update_sequence.patch, 2.4 KB (added by bobbysmith007@…, 2 months ago)

A patch to add update_sequence to the database backends

  • trac/db/sqlite_backend.py

     
    315315 
    316316    def get_last_id(self, cursor, table, column='id'): 
    317317        return cursor.lastrowid 
     318     
     319    def update_sequence(self, cursor, table, column='id'): 
     320        # sqlite handles sequence updates automagically 
     321        # http://www.sqlite.org/autoinc.html 
     322        pass 
  • trac/db/tests/api.py

     
    320320        id2 = self.db.get_last_id(c, 'report') 
    321321        self.assertEqual(id1 + 1, id2) 
    322322 
     323    def test_update_sequence(self): 
     324        c = self.db.cursor() 
     325        q = "INSERT INTO report (id, author) VALUES (42, 'anonymous')" 
     326        c.execute(q) 
     327        self.db.update_sequence(c, "report") 
     328        id1 = self.db.get_last_id(c, 'report') 
     329        self.assertEqual(id1, 42) 
    323330 
     331 
     332 
    324333def suite(): 
    325334    suite = unittest.TestSuite() 
    326335    suite.addTest(unittest.makeSuite(ParseConnectionStringTestCase, 'test')) 
  • trac/db/mysql_backend.py

     
    253253    def get_last_id(self, cursor, table, column='id'): 
    254254        return cursor.lastrowid 
    255255 
     256    def update_sequence(self, cursor, table, column='id'): 
     257        # mysql handles this automagically, verified in  
     258        # mysql  Ver 14.14 Distrib 5.1.41, for debian-linux-gnu (x86_64)  
     259        # using readline 6.1 
     260        pass 
     261 
    256262    def rollback(self): 
    257263        self.cnx.ping() 
    258264        try: 
  • trac/db/postgres_backend.py

     
    235235        cursor.execute("SELECT CURRVAL('%s_%s_seq')" % (table, column)) 
    236236        return cursor.fetchone()[0] 
    237237 
     238    def update_sequence(self, cursor, table, column='id'): 
     239        sql = "SELECT setval('%s_%s_seq', (SELECT MAX(id) FROM %s))" % \ 
     240            (table, column, table) 
     241        cursor.execute(sql) 
     242 
    238243    def cursor(self): 
    239244        return IterableCursor(self.cnx.cursor(), self.log) 
    240245