Database Admin API#

After creating a Instance, you can interact with individual databases for that instance.

List Databases#

To list of all existing databases for an instance, use its list_databases() method:

databases, token = instance.list_databases()

Database Factory#

To create a Database object:

database = instance.database(database_id, ddl_statements)
  • ddl_statements is a string containing DDL for the new database.

You can also use Instance.database() to create a local wrapper for a database that has already been created:

database = instance.database(existing_database_id)

Create a new Database#

After creating the database object, use its create() method to trigger its creation on the server:

operation = database.create()

Note

Creating an instance triggers a “long-running operation” and returns an google.cloud.spanner.database.Operation object. See Check on Current Database Operation for polling to find out if the operation is completed.

Update an existing Database#

After creating the database object, you can apply additional DDL statements via its update_ddl() method:

operation = instance.update_ddl(ddl_statements, operation_id)
  • ddl_statements is a string containing DDL to be applied to the database.
  • operation_id is a string ID for the long-running operation.

Note

Update an instance triggers a “long-running operation” and returns a google.cloud.spanner.database.Operation object. See Check on Current Database Operation for polling to find out if the operation is completed.

Drop a Database#

Drop a databse using its drop() method:

database.drop()

Check on Current Database Operation#

The create() and update() methods of instance object trigger long-running operations on the server, and return instances of the Operation class.

You can check if a long-running operation has finished by using its finished() method:

>>> operation = instance.create()
>>> operation.finished()
True

Note

Once an Operation object has returned True from its finished() method, the object should not be re-used. Subsequent calls to finished() will result in an :exc`ValueError` being raised.

Next Step#

Next, learn about Session Creation / Deletion.