source: tracdarcs/tracdarcs/components.py @ 44

Revision 44, 3.9 KB checked in by John Goerzen <jgoerzen@…>, 6 years ago (diff)

Fix so that it will work right on PostgreSQL
Without this patch, PostgreSQL users will get errors when trying
to install/upgrade tracdarcs. A transaction will be aborted
and without rolling it back, every subsequent operation will fail.

Line 
1# -*- coding: iso-8859-1 -*-
2#
3# Copyright (C) 2005 Edgewall Software
4# Copyright (C) 2005,2006 Lele Gaifax <lele@metapensiero.it>
5#
6# This software is licensed as described in the file COPYING, which
7# you should have received as part of this distribution. The terms
8# are also available at http://trac.edgewall.com/license.html.
9#
10# This software consists of voluntary contributions made by many
11# individuals. For the exact contribution history, see the revision
12# history and logs, available at http://projects.edgewall.com/trac/.
13#
14# Author: Lele Gaifax <lele@metapensiero.it>
15
16from trac.core import Component, implements
17from trac.versioncontrol import IRepositoryConnector
18from trac.env import IEnvironmentSetupParticipant
19from trac.db import Table, Column, Index, DatabaseManager
20
21from tracdarcs.repository import DarcsRepository
22
23class DarcsConnector(Component):
24
25    implements(IRepositoryConnector)
26
27    # IRepositoryConnector methods
28
29    def get_supported_types(self):
30        """Support the `darcs:` scheme"""
31        yield ("darcs", 8)
32
33    def get_repository(self, type, dir, authname):
34        """Return a `DarcsRepository`"""
35        db = self.env.get_db_cnx()
36        return DarcsRepository( db, dir, self.env.log, self.env.config )
37
38class DarcsSetup(Component):
39    """Darcs customizer.
40
41    The db tables required for the darcs backend are created here.
42    """
43
44    implements(IEnvironmentSetupParticipant)
45
46    def environment_created(self):
47        """After standard environment has been created, add the needed
48        tables."""
49
50        db = self.env.get_db_cnx()
51        self.upgrade_environment(db)
52        db.commit()
53
54    def environment_needs_upgrade(self, db):
55        """Check to see if the darcs tables are already there."""
56
57        c = db.cursor()
58        try:
59            c.execute( 'SELECT rev,author,time,hash,name,comment '
60                    'FROM darcs_revisions LIMIT 1' )
61            c.execute( 'SELECT node_id,node_type,add_rev,remove_rev '
62                    'FROM darcs_nodes LIMIT 1' )
63            c.execute( 'SELECT node_id,rev,path,parent_id,change '
64                    'FROM darcs_node_changes LIMIT 1' )
65            c.execute( 'SELECT node_id,rev,content '
66                    'FROM darcs_cache LIMIT 1' )
67            return False
68        except:
69            db.rollback()
70            return True
71
72    def upgrade_environment(self, db):
73        """Actually add the new db tables."""
74
75        def drop_table( table_name ) :
76            c = db.cursor()
77            try :
78                c.execute( 'drop table %s' % table_name )
79            except :
80                db.rollback()
81                pass
82
83        drop_table( 'darcs_revisions' )
84        drop_table( 'darcs_nodes' )
85        drop_table( 'darcs_node_changes' )
86        drop_table( 'darcs_cache' )
87
88        rev_table = Table( 'darcs_revisions', key='rev' )[
89                Column('rev',type='int'),
90                Column('author'),
91                Column('time',type='int'),
92                Column('hash'),
93                Column('name'),
94                Column('comment') ]
95        node_table = Table( 'darcs_nodes', key='node_id' )[
96                Column('node_id',type='int'),
97                Column('node_type'),
98                Column('add_rev',type='int'),
99                Column('remove_rev',type='int') ]
100        change_table = Table( 'darcs_node_changes', key=('node_id','rev') )[
101                Column('node_id',type='int'),
102                Column('rev',type='int'),
103                Column('path'),
104                Column('parent_id',type='int'),
105                Column('change') ]
106        cache_table = Table( 'darcs_cache', key=('node_id','rev') )[
107                Column('node_id',type='int'),
108                Column('rev',type='int'),
109                Column('content',type='blob') ]
110        connector = DatabaseManager(self.env)._get_connector()[0]
111        c = db.cursor()
112        for t in [rev_table,node_table,change_table,cache_table] :
113            for stmt in connector.to_sql(t) :
114                c.execute( stmt )
Note: See TracBrowser for help on using the repository browser.