Skip to content

Commit

Permalink
Allow setting of a user-specified external database. Fixes #1.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Dec 1, 2011
1 parent 2e97d91 commit 096947b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README
Expand Up @@ -41,3 +41,9 @@ It is configured in the ``trac.ini`` file by enabling and configuring::


The ``TRAC_ADMIN`` permission is used to control access to the query pages. The ``TRAC_ADMIN`` permission is used to control access to the query pages.


By default, the TracSQL plugin connects to the project database. To use an
external database, set the ``database`` parameter in the ``tracsql`` section
of the ``trac.ini`` file to a valid database connection string::

[tracsql]
database = sqlite:db/external.db
23 changes: 20 additions & 3 deletions tracsql/web_ui.py
Expand Up @@ -6,6 +6,7 @@
# trac imports # trac imports
import trac import trac
from trac.core import * from trac.core import *
from trac.db.api import DatabaseManager
from trac.util.html import html from trac.util.html import html
from trac.web import IRequestHandler from trac.web import IRequestHandler
from trac.web.chrome import INavigationContributor, ITemplateProvider from trac.web.chrome import INavigationContributor, ITemplateProvider
Expand Down Expand Up @@ -54,17 +55,33 @@ def match_request(self, req):
req.args['path'] = path or '/' req.args['path'] = path or '/'
return True return True


def get_db_cnx(self):
"""
Load the database, either user-specified or the project database.
"""
db_str = self.env.config.get('tracsql', 'database', '')

if db_str:
class ExternalDatabaseManager(DatabaseManager):
connection_uri = db_str
db_mgr = ExternalDatabaseManager(self.env)
db = db_mgr.get_connection()

else:
db_str = self.env.config.get('trac', 'database')
db = self.env.get_db_cnx()

return db, db_str

def process_request(self, req): def process_request(self, req):
req.perm.require('TRAC_ADMIN') req.perm.require('TRAC_ADMIN')


path = req.args.get('path', '') path = req.args.get('path', '')


data = {} data = {}


db = self.env.get_db_cnx() db, db_str = self.get_db_cnx()
cursor = db.cursor() cursor = db.cursor()

db_str = self.env.config.get('trac', 'database')
db_type, db_path = db_str.split(':', 1) db_type, db_path = db_str.split(':', 1)
assert db_type in ('sqlite', 'mysql', 'postgres'), \ assert db_type in ('sqlite', 'mysql', 'postgres'), \
'Unsupported database "%s"' % db_type 'Unsupported database "%s"' % db_type
Expand Down

0 comments on commit 096947b

Please sign in to comment.