Permalink
Browse files

Allow setting of a user-specified external database. Fixes #1.

  • Loading branch information...
1 parent 2e97d91 commit 096947b728dfd23d28eaa4a9537b7bdc9adbb929 @mrjbq7 mrjbq7 committed Dec 1, 2011
Showing with 26 additions and 3 deletions.
  1. +6 −0 README
  2. +20 −3 tracsql/web_ui.py
View
6 README
@@ -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.
+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
View
@@ -6,6 +6,7 @@
# trac imports
import trac
from trac.core import *
+from trac.db.api import DatabaseManager
from trac.util.html import html
from trac.web import IRequestHandler
from trac.web.chrome import INavigationContributor, ITemplateProvider
@@ -54,17 +55,33 @@ def match_request(self, req):
req.args['path'] = path or '/'
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):
req.perm.require('TRAC_ADMIN')
path = req.args.get('path', '')
data = {}
- db = self.env.get_db_cnx()
+ db, db_str = self.get_db_cnx()
cursor = db.cursor()
-
- db_str = self.env.config.get('trac', 'database')
db_type, db_path = db_str.split(':', 1)
assert db_type in ('sqlite', 'mysql', 'postgres'), \
'Unsupported database "%s"' % db_type

0 comments on commit 096947b

Please sign in to comment.