Edgewall Software

Ticket #2304: trac_pg_backup.3.patch

File trac_pg_backup.3.patch, 3.6 KB (added by Shane Caraveo <shanec@…>, 3 years ago)

Implementation of IBackupProvier

  • vendor/trac/trac/env.py

     
    3333from trac.versioncontrol import RepositoryManager 
    3434from trac.web.href import Href 
    3535 
    36 __all__ = ['Environment', 'IEnvironmentSetupParticipant', 'open_environment'] 
     36__all__ = ['Environment', 'IEnvironmentSetupParticipant', 'open_environment', 
     37           'IBackupProvider'] 
    3738 
    3839 
    3940class IEnvironmentSetupParticipant(Interface): 
     
    5960        performed the upgrades they need without an error being raised. 
    6061        """ 
    6162 
     63class IBackupProvider(Interface): 
     64    """Extension point interface for components that implment support for 
     65       backup of database tables during environment upgrade.""" 
    6266 
     67    def supports_database(env): 
     68        """Returns a boolean if this provider supports the database 
     69        used by the environment.  The provider should check the 
     70        trac.database configuration.""" 
     71 
     72    def backup(env, dest): 
     73        """Called when a new Trac environment is created.""" 
     74 
    6375class Environment(Component, ComponentManager): 
    6476    """Trac stores project information in a Trac environment. 
    6577 
     
    7183     * wiki and ticket attachments. 
    7284    """    
    7385    setup_participants = ExtensionPoint(IEnvironmentSetupParticipant) 
     86    backup_providers = ExtensionPoint(IBackupProvider) 
    7487 
    7588    shared_plugins_dir = PathOption('inherit', 'plugins_dir', '', 
    7689        """Path of the directory containing additional plugins. 
     
    399412            yield username, name, email 
    400413 
    401414    def backup(self, dest=None): 
    402         """Simple SQLite-specific backup of the database. 
     415        """Backup the databas using database specific backup providers. 
    403416 
    404417        @param dest: Destination file; if not specified, the backup is stored in 
    405418                     a file called db_name.trac_version.bak 
    406419        """ 
    407         import shutil 
     420        for provider in self.backup_providers: 
     421            if provider.supports_database(self): 
     422                provider.backup(self, dest) 
     423                return 
     424        raise TracError(_('No IBackupProvider supports this database type.')) 
    408425 
    409         db_str = self.config.get('trac', 'database') 
    410         if not db_str.startswith('sqlite:'): 
    411             raise TracError(_('Can only backup sqlite databases')) 
    412         db_name = os.path.join(self.path, db_str[7:]) 
    413         if not dest: 
    414             dest = '%s.%i.bak' % (db_name, self.get_version()) 
    415         shutil.copy (db_name, dest) 
    416  
    417426    def needs_upgrade(self): 
    418427        """Return whether the environment needs to be upgraded.""" 
    419428        db = self.get_db_cnx() 
     
    471480        return self._abs_href 
    472481    abs_href = property(_get_abs_href, 'The application URL') 
    473482 
     483class DefaultBackupProvider(Component): 
     484    implements(IBackupProvider) 
    474485 
     486    def supports_database(self, env): 
     487        db_str = env.config.get('trac', 'database') 
     488        return db_str.startswith('sqlite:') 
     489 
     490    def backup(self, env, dest=None): 
     491        """Simple SQLite-specific backup of the database. 
     492 
     493        @param dest: Destination file; if not specified, the backup is stored in 
     494                     a file called db_name.trac_version.bak 
     495        """ 
     496        import shutil 
     497        db_str = env.config.get('trac', 'database') 
     498        db_name = os.path.join(env.path, db_str[7:]) 
     499        if not dest: 
     500            dest = '%s.%i.bak' % (db_name, env.get_version()) 
     501        shutil.copy (db_name, dest) 
     502 
    475503class EnvironmentSetup(Component): 
    476504    implements(IEnvironmentSetupParticipant) 
    477505