Changeset 11041


Ignore:
Timestamp:
Dec 14, 2011, 9:19:06 PM (12 years ago)
Author:
Steffen Hoffmann
Message:

TagsPlugin: Prevent AttributeError on environment startup for Trac 0.13dev, refs #9521.

Additionally this broke all environment upgrades for plugins and for
Trac core itself, at least as long as the plugin was installed and enabled.

While this might be just a temporary Trac behavior (see Remy Blank's
comment in ticket #10451), it prevails in trunk
for more than a month now - a major headache for early adopters.
So I feel like acting now, facing the risk of not fixing it properly.

The logical next step is switching to the recommended procedure of storing
explicite db schema versions inside the Trac db like other plugins already do.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tagsplugin/trunk/tractags/model.py

    r10628 r11041  
    1 from trac.core import *
     1from trac.core import Component, TracError, implements
    22from trac.env import IEnvironmentSetupParticipant
    33from trac.db import Table, Column, Index
     4from trac.db.api import DatabaseManager
    45
    56
    67class TagModelProvider(Component):
     8
    79    implements(IEnvironmentSetupParticipant)
    810
     
    1517              Index(['tagspace', 'tag']),]
    1618        ]
     19    def __init__(self):
     20        # Preemptive check for rollback tolerance of read-only db connections.
     21        # This is required to avoid breaking `environment_needs_upgrade`,
     22        #   if the plugin uses intentional db transaction errors for the test.
     23        self.rollback_is_safe = True
     24        try:
     25            db = DatabaseManager(self.env).get_connection()
     26            if hasattr(db, 'readonly'):
     27                db = DatabaseManager(self.env).get_connection(readonly=True)
     28                cursor = db.cursor()
     29                # Test needed for rollback on read-only connections.
     30                cursor.execute("SELECT COUNT(*) FROM system")
     31                cursor.fetchone()
     32                try:
     33                    db.rollback()
     34                except AttributeError:
     35                    # Avoid rollback on read-only connections.
     36                    self.rollback_is_safe = False
     37                    return
     38                # Test passed.
     39        except TracError, e:
     40            # Trac too old - expect no constraints.
     41            return
    1742
    1843    # IEnvironmentSetupParticipant methods
     
    3055        except Exception, e:
    3156            self.log.error("DatabaseError: %s", e)
    32             db.rollback()
     57            if self.rollback_is_safe:
     58                db.rollback()
    3359            return True
    3460
     
    4470            return True
    4571        except Exception, e:
    46             # The expected outcome for any new/updated installation.
    47             db.rollback()
     72            # The expected outcome for any up-to-date installation.
     73            if self.rollback_is_safe:
     74                db.rollback()
    4875            return False
    4976
Note: See TracChangeset for help on using the changeset viewer.