~ubuntu-branches/ubuntu/precise/trac/precise

« back to all changes in this revision

Viewing changes to trac/db/api.py

  • Committer: Bazaar Package Importer
  • Author(s): W. Martin Borgert
  • Date: 2009-09-15 21:43:38 UTC
  • mfrom: (1.1.15 upstream)
  • Revision ID: james.westby@ubuntu.com-20090915214338-q3ecy6qxwxfzf9y8
Tags: 0.11.5-2
* Set exec bit for *_frontends (Closes: #510441), thanks to Torsten
  Landschoff for the patch.
* Move python-psycopg2 and python-mysql from Suggests to Depends as
  alternative to python-psqlite2 (Closes: #513117).
* Use debhelper 7 (Closes: #497862).
* Don't compress *-hook files and don't install MS-Windows *.cmd
  files (Closes: #526142), thanks to Jan Dittberner for the patch.
* Add README.source to point to dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- coding: utf-8 -*-
2
2
#
3
 
# Copyright (C) 2005 Edgewall Software
 
3
# Copyright (C)2005-2009 Edgewall Software
4
4
# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
5
5
# All rights reserved.
6
6
#
16
16
 
17
17
import os
18
18
import urllib
 
19
import time
19
20
 
20
 
from trac.config import Option, IntOption
 
21
from trac.config import BoolOption, IntOption, Option
21
22
from trac.core import *
22
23
from trac.db.pool import ConnectionPool
23
24
from trac.util.text import unicode_passwd
38
39
        their relative priorities as an iterable of `(scheme, priority)` tuples.
39
40
        """
40
41
 
41
 
    def get_connection(**kwargs):
 
42
    def get_connection(path, log=None, **kwargs):
42
43
        """Create a new connection to the database."""
43
44
        
44
 
    def init_db(**kwargs):
 
45
    def init_db(path, log=None, **kwargs):
45
46
        """Initialize the database."""
46
47
 
47
48
    def to_sql(table):
48
49
        """Return the DDL statements necessary to create the specified table,
49
50
        including indices."""
 
51
        
 
52
    def backup(dest):
 
53
        """Backup the database to a location defined by trac.backup_dir"""
50
54
 
51
55
 
52
56
class DatabaseManager(Component):
58
62
        [wiki:TracEnvironment#DatabaseConnectionStrings string] for this
59
63
        project""")
60
64
 
 
65
    backup_dir = Option('trac', 'backup_dir', 'db',
 
66
        """Database backup location""")
 
67
 
61
68
    timeout = IntOption('trac', 'timeout', '20',
62
69
        """Timeout value for database connection, in seconds.
63
70
        Use '0' to specify ''no timeout''. ''(Since 0.11)''""")
64
71
 
 
72
    debug_sql = BoolOption('trac', 'debug_sql', False,
 
73
        """Show the SQL queries in the Trac log, at DEBUG level.
 
74
        ''(Since 0.11.5)''""")
 
75
 
65
76
    def __init__(self):
66
77
        self._cnx_pool = None
67
78
 
80
91
            self._cnx_pool.shutdown(tid)
81
92
            if not tid:
82
93
                self._cnx_pool = None
 
94
                
 
95
    def backup(self, dest=None):
 
96
        """Save a backup of the database.
 
97
 
 
98
        @param dest: base filename to write to.
 
99
        Returns the file actually written.
 
100
        """
 
101
        connector, args = self._get_connector()
 
102
        if not dest:
 
103
            backup_dir = self.backup_dir
 
104
            if backup_dir[0] != "/":
 
105
                backup_dir = os.path.join(self.env.path, backup_dir)
 
106
            db_str = self.config.get('trac', 'database')
 
107
            db_name, db_path = db_str.split(":",1)
 
108
            dest_name = '%s.%i.%d.bak' % (db_name, self.env.get_version(),
 
109
                                          int(time.time()))
 
110
            dest = os.path.join(backup_dir, dest_name)
 
111
        else:
 
112
            backup_dir = os.path.dirname(dest)
 
113
        if not os.path.exists(backup_dir):
 
114
            os.makedirs(backup_dir)
 
115
        return connector.backup(dest)
83
116
 
84
117
    def _get_connector(self): ### FIXME: Make it public?
85
118
        scheme, args = _parse_db_str(self.connection_uri)
104
137
                args['path'] = os.path.join(self.env.path,
105
138
                                            args['path'].lstrip('/'))
106
139
 
 
140
        if self.debug_sql:
 
141
            args['log'] = self.log
107
142
        return connector, args
108
143
 
109
144
 
116
151
            host = None
117
152
            path = rest
118
153
        else:
119
 
            raise TracError('Database connection string must start with '
120
 
                            'scheme:/')
 
154
            raise TracError('Unknown scheme "%s"; database connection string '
 
155
                            'must start with {scheme}:/' % scheme)
121
156
    else:
122
157
        if not rest.startswith('//'):
123
158
            host = None