Commits

rblank  committed ba11cda

0.13dev: Allow parameters in the database connection string for the MySQL backend.

Patch by Thijs Triemstra. Closes #5120 and #9949.

  • Participants
  • Parent commits 6ecdebc
  • Branches trunk

Comments (0)

Files changed (1)

File trac/db/mysql_backend.py

 from trac.config import Option
 from trac.db.api import IDatabaseConnector, _parse_db_str
 from trac.db.util import ConnectionWrapper, IterableCursor
-from trac.util import get_pkginfo
+from trac.util import as_int, get_pkginfo
 from trac.util.compat import close_fds
 from trac.util.text import exception_to_unicode, to_unicode
 from trac.util.translation import _
     
     Database URLs should be of the form:
     {{{
-    mysql://user[:password]@host[:port]/database
+    mysql://user[:password]@host[:port]/database[?param1=value&param2=value]
     }}}
+    The following parameters are supported:
+     * `compress`: Enable compression (0 or 1)
+     * `init_command`: Command to run once the connection is created
+     * `named_pipe`: Use a named pipe to connect on Windows (0 or 1)
+     * `read_default_file`: Read default client values from the given file
+     * `read_default_group`: Configuration group to use from the default file
+     * `unix_socket`: Use a Unix socket at the given path to connect
     """
     implements(IDatabaseConnector)
 
         from subprocess import Popen, PIPE
         db_url = self.env.config.get('trac', 'database')
         scheme, db_prop = _parse_db_str(db_url)
+        db_params = db_prop.setdefault('params', {})
         db_name = os.path.basename(db_prop['path'])
 
         args = [self.mysqldump_path]
             args.extend(['-P', str(db_prop['port'])])
         if 'user' in db_prop:
             args.extend(['-u', db_prop['user']])
+        for name, value in db_params.iteritems():
+            if name == 'compress' and as_int(value, 0):
+                args.extend(['--compress'])
+            elif name == 'named_pipe' and as_int(value, 0):
+                args.extend(['--protocol', 'pipe'])
+            elif name == 'read_default_file':
+                args.extend(['--defaults-file=', value])
+            elif name == 'unix_socket':
+                args.extend(['--protocol', 'socket'])
+                args.extend(['--socket', value])
+            elif name not in ('init_command', 'read_default_group'):
+                self.log.warning("Invalid connection string parameter '%s'",
+                                 name)
         args.extend(['-r', dest_file, db_name])
         
         environ = os.environ.copy()
             password = ''
         if port == None:
             port = 3306
-        cnx = MySQLdb.connect(db=path, user=user, passwd=password,
-                              host=host, port=port, charset='utf8')
+        opts = {}
+        for name, value in params.iteritems():
+            if name in ('init_command', 'read_default_file',
+                        'read_default_group', 'unix_socket'):
+                opts[name] = value
+            elif name in ('compress', 'named_pipe'):
+                opts[name] = as_int(value, 0)
+            else:
+                self.log.warning("Invalid connection string parameter '%s'",
+                                 name)
+        cnx = MySQLdb.connect(db=path, user=user, passwd=password, host=host,
+                              port=port, charset='utf8', **opts)
         if hasattr(cnx, 'encoders'):
             # 'encoders' undocumented but present since 1.2.1 (r422)
             cnx.encoders[Markup] = cnx.encoders[types.UnicodeType]