Database Admin

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

List Databases

To iterate over all existing databases for an instance, use its list_databases() method:

for database in instance.list_databases():
    # `database` is a `Database` object.

This method yields Database objects.

Database Factory

To create a Database object:

database = instance.database(database_id, ddl_statements)
  • ddl_statements is a list of strings containing DDL statements for the new database.

You can also use the database() method on an Instance object 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 a database triggers a “long-running operation” and returns a Future-like object. Use the result() method to wait for and inspect the result.

Update an existing Database

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

operation = database.update_ddl(ddl_statements, operation_id)
  • ddl_statements is a list of strings containing DDL statements to be applied to the database.

  • operation_id is a string ID for the long-running operation.

NOTE: Updating a database triggers a “long-running operation” and returns an Operation object. See Check on Current Database Operation for polling to find out if the operation is completed.

Drop a Database

Drop a database using its drop() method:

database.drop()

Check on Current Database Operation

The create() and update_ddl() methods of the Database object trigger long-running operations on the server, and return operations conforming to the Future class.

>>> operation = database.create()
>>> operation.result()

Non-Admin Database Usage

Use a Snapshot to Read / Query the Database

A snapshot represents a read-only point-in-time view of the database.

Calling snapshot() with no arguments creates a snapshot with strong concurrency:

with database.snapshot() as snapshot:
    do_something_with(snapshot)

See Snapshot for the other options which can be passed.

NOTE: