Trac SQLAlchemy Bridge


This is a bridge for a plugin developer to use SQLAlchemy with Trac. SQLAlchemy is a Python SQL toolkit and Object Relational Mapper. SQLAlchemy itself is released under a MIT license.

See also: SqlAlchemyQueryMacro

Bugs/Feature Requests

Existing bugs and feature requests for TracSqlAlchemyBridgeIntegration are here.

If you have any issues, create a new ticket.


0 / 1


Download the zipped source from here.

Download at PyPI from here.


You can check out TracSqlAlchemyBridgeIntegration from here using Subversion, or browse the source with Trac.


General instructions on installing Trac plugins can be found on the TracPlugins page.


You define your tables as SQLAlchemy tells you to on its docs.

Making Trac upgrade its tables to include yours (from the plugin I was developing from which this package was born):

from trac.core import *
from trac.env import IEnvironmentSetupParticipant
from tsab import engine

from tl10nm import model

class TracL10nManagerSetup(Component):

    # IEnvironmentSetupParticipant Methods
    def environment_created(self):
        self.found_db_version = 0

    def environment_needs_upgrade(self, db):
        cursor = db.cursor()
        cursor.execute("SELECT value FROM system WHERE name=%s";,
        value = cursor.fetchone()
        if not value:
            self.found_db_version = 0
            return True
            self.found_db_version = int(value[0])
            self.log.debug("%s: Found db version %s, current is %s",
                           __package__, self.found_db_version, model.version)
            return self.found_db_version < model.version

    def upgrade_environment(self, db):
        # Currently we only create the tables, so far there's no migration done
        cursor = db.cursor()
        if not self.found_db_version:
            cursor.execute("INSERT INTO system (name, value) VALUES (%s, %s)",
                           (, model.version))
            cursor.execute("UPDATE system SET value=%s WHERE name=%s",

Then to use it in your code (from the plugin I was developing from which this package was born):

#--------8<------- code cut for readability --------8<-------
from tsab import session
#--------8<------- code cut for readability --------8<-------
class L10nModule(Component):
#--------8<------- code cut for readability --------8<-------
    def _list_messages(self, req, catalog_id, locale_name, page):
        Session = session(self.env)
        locale = Session.query(Locale).filter_by(locale=locale_name,

        data = {'locale': locale, 'catalog_id': catalog_id}

        paginator = Paginator(list(locale.catalog.messages), page-1, 5)
        data['messages'] = paginator
        shown_pages = paginator.get_shown_pages(25)
        pagedata = []
        for show_page in shown_pages:
            page_href = req.href.translations(catalog_id, locale_name,
            pagedata.append([page_href, None, str(show_page),
                             'page %s' % show_page])
        fields = ['href', 'class', 'string', 'title']
        paginator.shown_pages = [dict(zip(fields, p)) for p in pagedata]
        paginator.current_page = {'href': None, 'class': 'current',
                                  'string': str( + 1),
                                  'title': None}
        if paginator.has_next_page:
            add_link(req, 'next', req.href.translations(locale_id, page+1),
                     _('Next Page'))
        if paginator.has_previous_page:
            add_link(req, 'prev', req.href.translations(locale_id, page-1),
                     _('Previous Page'))
        return 'l10n_messages.html', data, None
#--------8<------- code cut for readability --------8<-------

Recent Changes

5089 by s0undt3ch on 2009-01-05 09:06:52
Bump version number and more old<->new SQLAlchemy compatibility changes.
5088 by s0undt3ch on 2009-01-05 07:55:18
Support both old and new versions of SQLAlchemy.
4086 by s0undt3ch on 2008-07-30 14:20:32
Fix bug where the _engines dict would only get entries if trac was in debug mode.
Include correct logging setup for future feature of trac. See this ticket on t.e.o.


Author: mitsuhiko
Maintainer: Pedro Algarvio, aka, s0undt3ch

Last modified 13 months ago Last modified on Dec 15, 2015, 4:07:12 PM