source: tagsplugin/tags/0.6/tractags/model.py

Last change on this file was 10384, checked in by Steffen Hoffmann, 12 years ago

TagsPlugin: Don't catch all exceptions silently, refs #5345.

Errors from db operations throw not selectively catchable OperationalError,
but don't swallow each and every exception without notice - at least log them.

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1from trac.core import *
2from trac.env import IEnvironmentSetupParticipant
3from trac.db import Table, Column, Index
4
5
6class TagModelProvider(Component):
7    implements(IEnvironmentSetupParticipant)
8
9    SCHEMA = [
10        Table('tags', key = ('tagspace', 'name', 'tag'))[
11              Column('tagspace'),
12              Column('name'),
13              Column('tag'),
14              Index(['tagspace', 'name']),
15              Index(['tagspace', 'tag']),]
16        ]
17
18    # IEnvironmentSetupParticipant methods
19    def environment_created(self):
20        self._upgrade_db(self.env.get_db_cnx())
21
22    def environment_needs_upgrade(self, db):
23        if self._need_migration(db):
24            return True
25        try:
26            cursor = db.cursor()
27            cursor.execute("select count(*) from tags")
28            cursor.fetchone()
29            return False
30        except Exception, e:
31            self.log.error("DatabaseError: %s", e)
32            db.rollback()
33            return True
34
35    def upgrade_environment(self, db):
36        self._upgrade_db(db)
37
38    def _need_migration(self, db):
39        try:
40            cursor = db.cursor()
41            cursor.execute("select count(*) from wiki_namespace")
42            cursor.fetchone()
43            self.env.log.debug("tractags needs to migrate old data")
44            return True
45        except Exception, e:
46            self.log.error("DatabaseError: %s", e)
47            db.rollback()
48            return False
49
50    def _upgrade_db(self, db):
51        try:
52            try:
53                from trac.db import DatabaseManager
54                db_backend, _ = DatabaseManager(self.env)._get_connector()
55            except ImportError:
56                db_backend = self.env.get_db_cnx()
57
58            cursor = db.cursor()
59            for table in self.SCHEMA:
60                for stmt in db_backend.to_sql(table):
61                    self.env.log.debug(stmt)
62                    cursor.execute(stmt)
63            db.commit()
64
65            # Migrate old data
66            if self._need_migration(db):
67                cursor = db.cursor()
68                cursor.execute("INSERT INTO tags (tagspace, name, tag) SELECT "
69                               "'wiki', name, namespace FROM wiki_namespace")
70                cursor.execute("DROP TABLE wiki_namespace")
71                db.commit()
72        except Exception, e:
73            self.log.error("DatabaseError: %s", e)
74            db.rollback()
75            raise
76
Note: See TracBrowser for help on using the repository browser.