Class Database (1.18.0)

Database(database_id, instance, ddl_statements=(), pool=None)

Representation of a Cloud Spanner Database.

We can use a Database to:

  • create the database
  • reload the database
  • update the database
  • drop the database

Parameters

NameDescription
database_id str

The ID of the database.

instance Instance

The instance that owns the database.

ddl_statements list of string

(Optional) DDL statements, excluding the CREATE DATABASE statement.

pool concrete subclass of AbstractSessionPool.

(Optional) session pool to be used by database. If not passed, the database will construct an instance of BurstyPool.

Properties

create_time

Create time of this database.

Returns
TypeDescription
datetime.datetimea datetime object representing the create time of this database

ddl_statements

DDL Statements used to define database schema.

See cloud.google.com/spanner/docs/data-definition-language

Returns
TypeDescription
sequence of stringthe statements

name

Database name used in requests.

"projects/../instances/../databases/{database_id}"

Returns
TypeDescription
strThe database name.

restore_info

Restore info for this database.

Returns
TypeDescription
RestoreInfoan object representing the restore info for this database

spanner_api

Helper for session-related API calls.

state

State of this database.

Returns
TypeDescription
Statean enum describing the state of the database

Methods

batch

batch()

Return an object which wraps a batch.

The wrapper must be used as a context manager, with the batch as the value returned by the wrapper.

Returns
TypeDescription
BatchCheckoutnew wrapper

batch_snapshot

batch_snapshot(read_timestamp=None, exact_staleness=None)

Return an object which wraps a batch read / query.

Parameters
NameDescription
read_timestamp datetime.datetime

Execute all reads at the given timestamp.

exact_staleness datetime.timedelta

Execute all reads at a timestamp that is exact_staleness old.

Returns
TypeDescription
BatchSnapshotnew wrapper

create

create()

Create this database within its instance

Inclues any configured schema assigned to ddl_statements.

See https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.CreateDatabase

Exceptions
TypeDescription
Conflictif the database already exists
NotFoundif the instance owning the database does not exist
Returns
TypeDescription
google.api_core.operation.Operationa future used to poll the status of the create request

drop

drop()

execute_partitioned_dml

execute_partitioned_dml(dml, params=None, param_types=None, query_options=None)

Execute a partitionable DML statement.

Parameters
NameDescription
dml str

DML statement

params dict, {str -> column value}

values for parameter replacement. Keys must match the names used in dml.

param_types dict[str -> Union[dict, .types.Type]]

(Optional) maps explicit types for one or more param values; required if parameters are passed.

query_options QueryOptions or dict

(Optional) Query optimizer configuration to use for the given query. If a dict is provided, it must be of the same form as the protobuf message QueryOptions

Returns
TypeDescription
intCount of rows affected by the DML statement.

exists

exists()
Returns
TypeDescription
boolTrue if the database exists, else false.

from_pb

from_pb(database_pb, instance, pool=None)

Creates an instance of this class from a protobuf.

Parameters
NameDescription
database_pb google.spanner.v2.spanner_instance_admin_pb2.Instance

A instance protobuf object.

instance Instance

The instance that owns the database.

pool concrete subclass of AbstractSessionPool.

(Optional) session pool to be used by database.

Exceptions
TypeDescription
ValueErrorif the instance name does not match the expected format or if the parsed project ID does not match the project ID on the instance's client, or if the parsed instance ID does not match the instance's ID.
Returns
TypeDescription
DatabaseThe database parsed from the protobuf response.

is_optimized

is_optimized()

Test whether this database has finished optimizing.

Returns
TypeDescription
boolTrue if the database state is READY, else False.

is_ready

is_ready()

Test whether this database is ready for use.

Returns
TypeDescription
boolTrue if the database state is READY_OPTIMIZING or READY, else False.

list_database_operations

list_database_operations(filter_="", page_size=None)

List database operations for the database.

Parameters
NameDescription
filter_ str

Optional. A string specifying a filter for which database operations to list.

page_size int

Optional. The maximum number of operations in each page of results from this request. Non-positive values are ignored. Defaults to a sensible value set by the API. :type: google.api_core.page_iterator.Iterator

reload

reload()
Exceptions
TypeDescription
NotFoundif the database does not exist

restore

restore(source)

Restore from a backup to this database.

Parameter
NameDescription
backup Backup

the path of the backup being restored from.

Exceptions
TypeDescription
Conflictif the database already exists
NotFoundif the instance owning the database does not exist, or if the backup being restored from does not exist
ValueErrorif backup is not set
Returns
TypeDescription
google.api_core.operation.Operationa future used to poll the status of the create request

run_in_transaction

run_in_transaction(func, *args, **kw)

Perform a unit of work in a transaction, retrying on abort.

Parameters
NameDescription
args tuple

additional positional arguments to be passed to func.

kw dict

(Optional) keyword arguments to be passed to func. If passed, "timeout_secs" will be removed and used to override the default retry timeout which defines maximum timestamp to continue retrying the transaction.

func callable

takes a required positional argument, the transaction, and additional positional / keyword arguments as supplied by the caller.

Exceptions
TypeDescription
Exceptionreraises any non-ABORT execptions raised by func.
Returns
TypeDescription
AnyThe return value of func.

session

session(labels=None)

Factory to create a session for this database.

Parameter
NameDescription
labels dict (str -> str) or None

(Optional) user-assigned labels for the session.

Returns
TypeDescription
Sessiona session bound to this database.

snapshot

snapshot(**kw)

Return an object which wraps a snapshot.

The wrapper must be used as a context manager, with the snapshot as the value returned by the wrapper.

See https://cloud.google.com/spanner/reference/rpc/google.spanner.v1#google.spanner.v1.TransactionOptions.ReadOnly

Parameter
NameDescription
kw dict

Passed through to Snapshot constructor.

Returns
TypeDescription
SnapshotCheckoutnew wrapper

update_ddl

update_ddl(ddl_statements, operation_id="")
Parameters
NameDescription
ddl_statements Sequence[str]

a list of DDL statements to use on this database

operation_id str

(optional) a string ID for the long-running operation

Exceptions
TypeDescription
NotFoundif the database does not exist
Returns
TypeDescription
google.api_core.operation.Operationan operation instance