def _check_db_name(name): """Check if a database name is valid. This functionality is copied from pymongo Database class constructor. """ if not isinstance(name, str): raise TypeError(f"name must be an instance of {str}") elif name != "$external": _check_name(name)
def _check_db_name(name): """Check if a database name is valid. This functionality is copied from pymongo Database class constructor. """ if not isinstance(name, six.string_types): raise TypeError('name must be an instance of %s' % six.string_types) elif name != '$external': _check_name(name)
def check_db_name(name): """Check if a database name is valid. This functionality is copied from pymongo Database class constructor. """ if not isinstance(name, six.string_types): raise TypeError('name must be an instance of %s' % six.string_types) elif name != '$external': _check_name(name)
def copy_database(self, from_name, to_name, from_host=None, username=None, password=None): """Copy a database, potentially from another host. Raises :class:`TypeError` if `from_name` or `to_name` is not an instance of :class:`basestring` (:class:`str` in python 3). Raises :class:`~pymongo.errors.InvalidName` if `to_name` is not a valid database name. If `from_host` is ``None`` the current host is used as the source. Otherwise the database is copied from `from_host`. If the source database requires authentication, `username` and `password` must be specified. :Parameters: - `from_name`: the name of the source database - `to_name`: the name of the target database - `from_host` (optional): host name to copy from - `username` (optional): username for source database - `password` (optional): password for source database .. note:: Specifying `username` and `password` requires server version **>= 1.3.3+**. """ if not isinstance(from_name, basestring): raise TypeError("from_name must be an instance " "of %s" % (basestring.__name__, )) if not isinstance(to_name, basestring): raise TypeError("to_name must be an instance " "of %s" % (basestring.__name__, )) database._check_name(to_name) command = {"fromdb": from_name, "todb": to_name} if from_host is not None: command["fromhost"] = from_host in_request = self.in_request() try: if not in_request: self.start_request() if username is not None: nonce = self.admin.command("copydbgetnonce", fromhost=from_host)["nonce"] command["username"] = username command["nonce"] = nonce command["key"] = helpers._auth_key(nonce, username, password) return self.admin.command("copydb", **command) finally: if not in_request: self.end_request()
def copy_database(self, from_name, to_name, from_host=None, username=None, password=None): """Copy a database, potentially from another host. Raises :class:`TypeError` if `from_name` or `to_name` is not an instance of :class:`basestring` (:class:`str` in python 3). Raises :class:`~pymongo.errors.InvalidName` if `to_name` is not a valid database name. If `from_host` is ``None`` the current host is used as the source. Otherwise the database is copied from `from_host`. If the source database requires authentication, `username` and `password` must be specified. :Parameters: - `from_name`: the name of the source database - `to_name`: the name of the target database - `from_host` (optional): host name to copy from - `username` (optional): username for source database - `password` (optional): password for source database .. note:: Specifying `username` and `password` requires server version **>= 1.3.3+**. .. versionadded:: 1.5 """ if not isinstance(from_name, basestring): raise TypeError("from_name must be an instance " "of %s" % (basestring.__name__,)) if not isinstance(to_name, basestring): raise TypeError("to_name must be an instance " "of %s" % (basestring.__name__,)) database._check_name(to_name) command = {"fromdb": from_name, "todb": to_name} if from_host is not None: command["fromhost"] = from_host in_request = self.in_request() try: if not in_request: self.start_request() if username is not None: nonce = self.admin.command("copydbgetnonce", fromhost=from_host)["nonce"] command["username"] = username command["nonce"] = nonce command["key"] = helpers._auth_key(nonce, username, password) return self.admin.command("copydb", **command) finally: if not in_request: self.end_request()