def _do_convert_db_in_place(self, dst_dburi): dbm = DatabaseManager(self.env) src_dburi = dbm.connection_uri if src_dburi == dst_dburi: printferr("Source database and destination database are same: %s", dst_dburi) return 1 env_path = mkdtemp(prefix='convert_db-', dir=os.path.dirname(self.env.path)) try: dst_env = self._create_env(env_path, dst_dburi) src_db = dbm.get_connection() dst_db = DatabaseManager(dst_env).get_connection() self._copy_tables(dst_env, src_db, dst_db, src_dburi, dst_dburi) del src_db del dst_db dst_env.shutdown() dst_env = None schema, params = parse_connection_uri(dst_dburi) if schema == 'sqlite': dbpath = os.path.join(self.env.path, params['path']) dbdir = os.path.dirname(dbpath) if not os.path.isdir(dbdir): os.makedirs(dbdir) shutil.copy(os.path.join(env_path, params['path']), dbpath) finally: shutil.rmtree(env_path) backup_config_file(self.env, '.convert_db-%d' % int(time.time())) self.config.set('trac', 'database', dst_dburi) self.config.save()
def _do_convert_db_in_new_env(self, dst_dburi, env_path): try: os.rmdir(env_path) # remove directory if it's empty except OSError: pass if os.path.exists(env_path) or os.path.lexists(env_path): printferr("Cannot create Trac environment: %s: File exists", env_path) return 1 dst_env = self._create_env(env_path, dst_dburi) dbm = DatabaseManager(self.env) src_dburi = dbm.connection_uri src_db = dbm.get_connection() dst_db = DatabaseManager(dst_env).get_connection() self._copy_tables(dst_env, src_db, dst_db, src_dburi, dst_dburi) self._copy_directories(dst_env)