Beispiel #1
0
    def database(self, database_id, ddl_statements=(), pool=None, logger=None):
        """Factory to create a database within this instance.

        :type database_id: str
        :param database_id: The ID of the instance.

        :type ddl_statements: list of string
        :param ddl_statements: (Optional) DDL statements, excluding the
                               'CREATE DATABSE' statement.

        :type pool: concrete subclass of
                    :class:`~google.cloud.spanner_v1.pool.AbstractSessionPool`.
        :param pool: (Optional) session pool to be used by database.

        :type logger: `logging.Logger`
        :param logger: (Optional) a custom logger that is used if `log_commit_stats`
                       is `True` to log commit statistics. If not passed, a logger
                       will be created when needed that will log the commit statistics
                       to stdout.

        :rtype: :class:`~google.cloud.spanner_v1.database.Database`
        :returns: a database owned by this instance.
        """
        return Database(database_id,
                        self,
                        ddl_statements=ddl_statements,
                        pool=pool,
                        logger=logger)
Beispiel #2
0
    def execute_dml(self, project_id, instance_id, database_id, queries):
        # type: (str, str, str, str) -> None
        """
        Executes an arbitrary DML query (INSERT, UPDATE, DELETE).

        :param project_id: The ID of the project which owns the instances, tables and data.
        :type project_id: str
        :param instance_id: The ID of the instance.
        :type instance_id: str
        :param database_id: The ID of the database.
        :type database_id: str
        :param queries: The queries to be executed.
        :type queries: str
        """
        client = self.get_client(project_id)
        instance = client.instance(instance_id)
        database = Database(database_id, instance)
        database.run_in_transaction(lambda transaction:
                                    self._execute_sql_in_transaction(transaction, queries))
Beispiel #3
0
    def execute_dml(self, project_id, instance_id, database_id, queries):
        # type: (str, str, str, str) -> None
        """
        Executes an arbitrary DML query (INSERT, UPDATE, DELETE).

        :param project_id: The ID of the project which owns the instances, tables and data.
        :type project_id: str
        :param instance_id: The ID of the instance.
        :type instance_id: str
        :param database_id: The ID of the database.
        :type database_id: str
        :param queries: The queries to be executed.
        :type queries: str
        """
        client = self.get_client(project_id)
        instance = client.instance(instance_id)
        database = Database(database_id, instance)
        database.run_in_transaction(
            lambda transaction: self._execute_sql_in_transaction(
                transaction, queries))
    def _item_to_database(self, iterator, database_pb):
        """Convert a database protobuf to the native object.

        :type iterator: :class:`~google.api_core.page_iterator.Iterator`
        :param iterator: The iterator that is currently in use.

        :type database_pb: :class:`~google.spanner.admin.database.v1.Database`
        :param database_pb: A database returned from the API.

        :rtype: :class:`~google.cloud.spanner_v1.database.Database`
        :returns: The next database in the page.
        """
        return Database.from_pb(database_pb, self, pool=BurstyPool())
Beispiel #5
0
    def _item_to_database(self, iterator, database_pb):
        """Convert a database protobuf to the native object.

        :type iterator: :class:`~google.api_core.page_iterator.Iterator`
        :param iterator: The iterator that is currently in use.

        :type database_pb: :class:`~google.spanner.admin.database.v1.Database`
        :param database_pb: A database returned from the API.

        :rtype: :class:`~google.cloud.spanner_v1.database.Database`
        :returns: The next database in the page.
        """
        return Database.from_pb(database_pb, self, pool=BurstyPool())
Beispiel #6
0
    def database(
        self,
        database_id,
        ddl_statements=(),
        pool=None,
        logger=None,
        encryption_config=None,
    ):
        """Factory to create a database within this instance.

        :type database_id: str
        :param database_id: The ID of the database.

        :type ddl_statements: list of string
        :param ddl_statements: (Optional) DDL statements, excluding the
                               'CREATE DATABASE' statement.

        :type pool: concrete subclass of
                    :class:`~google.cloud.spanner_v1.pool.AbstractSessionPool`.
        :param pool: (Optional) session pool to be used by database.

        :type logger: :class:`logging.Logger`
        :param logger: (Optional) a custom logger that is used if `log_commit_stats`
                       is `True` to log commit statistics. If not passed, a logger
                       will be created when needed that will log the commit statistics
                       to stdout.

        :type encryption_config:
            :class:`~google.cloud.spanner_admin_database_v1.types.EncryptionConfig`
            or :class:`~google.cloud.spanner_admin_database_v1.types.RestoreDatabaseEncryptionConfig`
            or :class:`dict`
        :param encryption_config:
            (Optional) Encryption configuration for the database.
            If a dict is provided, it must be of the same form as either of the protobuf
            messages :class:`~google.cloud.spanner_admin_database_v1.types.EncryptionConfig`
            or :class:`~google.cloud.spanner_admin_database_v1.types.RestoreDatabaseEncryptionConfig`

        :rtype: :class:`~google.cloud.spanner_v1.database.Database`
        :returns: a database owned by this instance.
        """
        return Database(
            database_id,
            self,
            ddl_statements=ddl_statements,
            pool=pool,
            logger=logger,
            encryption_config=encryption_config,
        )
Beispiel #7
0
def deleter(database: Database,
        name: str,
        query: str,
        prefix: Optional[str]=None,
        params: Optional[dict]=None,
        param_types: Optional[dict]=None,
        dryrun: Optional[bool]=False):
    with statsd.timer("syncstorage.purge_ttl.{}_duration".format(name)):
        logging.info("Running: {} :: {}".format(query, params))
        start = datetime.now()
        result = 0
        if not dryrun:
            result = database.execute_partitioned_dml(query, params=params, param_types=param_types)
        end = datetime.now()
        logging.info(
            "{name}: removed {result} rows, {name}_duration: {time}, prefix: {prefix}".format(
                name=name, result=result, time=end - start, prefix=prefix))
Beispiel #8
0
    def execute_dml(self, project_id, instance_id, database_id, queries):
        # type: (str, str, str, str) -> None
        """
        Executes an arbitrary DML query (INSERT, UPDATE, DELETE).

        :param project_id: The ID of the GCP project that owns the Cloud Spanner
            database.
        :type project_id: str
        :param instance_id: The ID of the Cloud Spanner instance.
        :type instance_id: str
        :param database_id: The ID of the database in Cloud Spanner.
        :type database_id: str
        :param queries: The queries to execute.
        :type queries: str
        """
        instance = self.get_client(project_id).instance(instance_id)
        Database(database_id, instance).run_in_transaction(
            lambda transaction: self._execute_sql_in_transaction(transaction, queries))
    def database(self, database_id, ddl_statements=(), pool=None):
        """Factory to create a database within this instance.

        :type database_id: str
        :param database_id: The ID of the instance.

        :type ddl_statements: list of string
        :param ddl_statements: (Optional) DDL statements, excluding the
                               'CREATE DATABSE' statement.

        :type pool: concrete subclass of
                    :class:`~google.cloud.spanner_v1.pool.AbstractSessionPool`.
        :param pool: (Optional) session pool to be used by database.

        :rtype: :class:`~google.cloud.spanner_v1.database.Database`
        :returns: a database owned by this instance.
        """
        return Database(database_id, self, ddl_statements=ddl_statements, pool=pool)