Example #1
0
 def GetAzCommandForEngine(self):
     engine = self.spec.engine
     if engine == sql_engine_utils.POSTGRES:
         return 'postgres'
     elif engine == sql_engine_utils.MYSQL:
         return 'mysql'
     elif engine == sql_engine_utils.SQLSERVER:
         return 'sql'
     raise relational_db.RelationalDbEngineNotFoundError(
         'Unsupported engine {0}'.format(engine))
Example #2
0
 def RenameDatabase(self, new_name):
     """Renames an the database instace."""
     engine = self.spec.engine
     if engine == sql_engine_utils.SQLSERVER:
         cmd = [
             azure.AZURE_PATH,
             self.GetAzCommandForEngine(), 'db', 'rename',
             '--resource-group', self.resource_group.name, '--server',
             self.instance_id, '--name', self.database_name, '--new-name',
             new_name
         ]
         vm_util.IssueCommand(cmd)
         self.database_name = new_name
     else:
         raise relational_db.RelationalDbEngineNotFoundError(
             'Unsupported engine {0}'.format(engine))
Example #3
0
    def GetDefaultEngineVersion(engine):
        """Returns the default version of a given database engine.

    Args:
      engine (string): type of database (my_sql or postgres).
    Returns:
      (string): Default engine version.
    Raises:
      RelationalDbEngineNotFoundError: if an unknown engine is
                                                  requested.
    """
        if engine == sql_engine_utils.POSTGRES:
            return DEFAULT_POSTGRES_VERSION
        elif engine == sql_engine_utils.MYSQL:
            return DEFAULT_MYSQL_VERSION
        elif engine == sql_engine_utils.SQLSERVER:
            return DEFALUT_SQLSERVER_VERSION
        else:
            raise relational_db.RelationalDbEngineNotFoundError(
                'Unsupported engine {0}'.format(engine))
Example #4
0
    def _IsInstanceReady(self, timeout=IS_READY_TIMEOUT):
        """Return true if the instance is ready.

    This method will query the instance every 5 seconds until
    its instance state is 'Ready', or until a timeout occurs.

    Args:
      timeout: timeout in seconds

    Returns:
      True if the resource was ready in time, False if the wait timed out
        or an Exception occurred.
    """
        if not self.is_managed_db:
            return self._IsReadyUnmanaged()

        start_time = datetime.datetime.now()

        while True:
            if (datetime.datetime.now() - start_time).seconds >= timeout:
                logging.warning('Timeout waiting for sql instance to be ready')
                return False

            server_show_json = self._AzServerShow()
            if server_show_json is not None:
                engine = self.spec.engine
                if engine == sql_engine_utils.POSTGRES:
                    state = server_show_json['userVisibleState']
                elif engine == sql_engine_utils.MYSQL:
                    state = server_show_json['userVisibleState']
                elif engine == sql_engine_utils.SQLSERVER:
                    state = server_show_json['state']
                else:
                    raise relational_db.RelationalDbEngineNotFoundError(
                        'The db engine does not contain a valid state')

                if state == 'Ready':
                    break
            time.sleep(5)

        return True