# Cloud Spanner API - Class Google::Cloud::Spanner::Project (v2.36.0)

2.36.0 (latest) 2.35.0 2.34.0 2.33.0 2.32.0 2.31.0 2.30.0 2.29.0 2.28.0 2.27.0 2.26.0 2.25.0 2.24.0 2.23.0 2.22.0 2.21.0 2.20.0 2.19.1 2.18.1 2.17.0 2.16.1 2.15.0 2.14.0 2.13.0 2.12.1 2.11.0 Reference documentation and code samples for the Cloud Spanner API class Google::Cloud::Spanner::Project.

## Project

Projects are top-level containers in Google Cloud Platform. They store
information about billing and authorized users, and they contain
Cloud Spanner data. Each project has a friendly name and a unique ID.

Google::Cloud::Spanner::Project is the main object for interacting with
Cloud Spanner.

[Instance](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance "Google::Cloud::Spanner::Instance (class)") and
[Database](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Database "Google::Cloud::Spanner::Database (class)") objects are created,
accessed, and managed by Google::Cloud::Spanner::Project.

A [Client](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client "Google::Cloud::Spanner::Client (class)") obtained from a project can be used
to read and/or modify data in a Cloud Spanner database.

See [new](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner#Google__Cloud__Spanner_new_class_ "Google::Cloud::Spanner.new (method)") and [Google::Cloud#spanner](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud#Google__Cloud_spanner_instance_ "Google::Cloud#spanner (method)").

## Inherits

- Object

## Examples

Obtaining an instance and a database from a project.

```ruby
require "google/cloud"

spanner = Google::Cloud::Spanner.new
instance = spanner.instance "my-instance"
database = instance.database "my-database"
```

Obtaining a client for use with a database.

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

db.transaction do |tx|
  results = tx.execute_query "SELECT * FROM users"

  results.rows.each do |row|
    puts "User #{row[:id]} is #{row[:name]}"
  end
end
```

## Methods

### #batch_client

    def batch_client(instance_id, database_id, labels: nil, query_options: nil, directed_read_options: nil) -> ::Google::Cloud::Spanner::BatchClient

Creates a Cloud Spanner batch client. A batch client is used to read
data across multiple machines or processes.

The following settings can be provided:

- `:exclude_replicas` (Hash) Exclude_replicas indicates what replicas should be excluded from serving requests. Spanner will not route requests to the replicas in this list.
- `:include_replicas` (Hash) Include_replicas indicates the order of replicas to process the request. If auto_failover_disabled is set to true and all replicas are exhausted without finding a healthy replica, Spanner will wait for a replica in the list to become available, requests may fail due to DEADLINE_EXCEEDED errors.
**Parameters**

- **instance_id** (String) --- The unique identifier for the instance. Required.
- **database_id** (String) --- The unique identifier for the database. Required.
- **labels** (Hash) *(defaults to: nil)* ---

  The labels to be applied to all sessions
  created by the batch client. Labels are a flexible and lightweight
  mechanism for organizing cloud resources into groups that reflect a
  customer's organizational needs and deployment strategies. Cloud
  Labels can be used to filter collections of resources. They can be
  used to control how resource metrics are aggregated. And they can be
  used as arguments to policy management rules (e.g. route, firewall,
  load balancing, etc.). Optional. The default is `nil`.
  - Label keys must be between 1 and 63 characters long and must conform to the following regular expression: `[a-z]([-a-z0-9]*[a-z0-9])?`.
  - Label values must be between 0 and 63 characters long and must conform to the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
  - No more than 64 labels can be associated with a given resource.
- **query_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  query options for executing SQL query. Query options are optional.
  The following settings can be provided:
  - `:optimizer_version` (String) The version of optimizer to use. Empty to use database default. "latest" to use the latest available optimizer version.
  - `:optimizer_statistics_package` (String) Statistics package to use. Empty to use the database default.
- **directed_read_options** (Hash) *(defaults to: nil)* --- Client options used to set the directed_read_options for all ReadRequests and ExecuteSqlRequests that indicates which replicas or regions should be used for non-transactional reads or queries. Will represent [`Google::Cloud::Spanner::V1::DirectedReadOptions`](https://cloud.google.com/ruby/docs/reference/google-cloud-spanner-v1/latest/Google-Cloud-Spanner-V1-DirectedReadOptions)
**Returns**

- ([::Google::Cloud::Spanner::BatchClient](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchClient)) --- The newly created client.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

batch_snapshot = batch_client.batch_snapshot
serialized_snapshot = batch_snapshot.dump

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first
serialized_partition = partition.dump

# In a separate process
new_batch_snapshot = batch_client.load_batch_snapshot \
  serialized_snapshot

new_partition = batch_client.load_partition \
  serialized_partition

results = new_batch_snapshot.execute_partition \
  new_partition
```

### #client

    def client(instance_id, database_id, pool: {}, labels: nil, query_options: nil, database_role: nil, directed_read_options: nil, isolation_level: nil, read_lock_mode: nil) -> ::Google::Cloud::Spanner::Client

Creates a Cloud Spanner client. A client is used to read and/or modify
data in a Cloud Spanner database.

The following settings can be provided:

- `:exclude_replicas` (Hash) Exclude_replicas indicates what replicas should be excluded from serving requests. Spanner will not route requests to the replicas in this list.
- `:include_replicas` (Hash) Include_replicas indicates the order of replicas to process the request. If auto_failover_disabled is set to true and all replicas are exhausted without finding a healthy replica, Spanner will wait for a replica in the list to become available, requests may fail due to DEADLINE_EXCEEDED errors.
**Parameters**

- **instance_id** (String) --- The unique identifier for the instance. Required.
- **database_id** (String) --- The unique identifier for the database. Required.
- **pool** (Hash) *(defaults to: {})* --- Optional. Defaults to `{}`. Deprecated. @deprecated This parameter is non-functional since the multiplexed SessionCache does not require pool options.
- **labels** (Hash) *(defaults to: nil)* ---

  The labels to be applied to all sessions
  created by the client. Cloud Labels are a flexible and lightweight
  mechanism for organizing cloud resources into groups that reflect a
  customer's organizational needs and deployment strategies. Cloud
  Labels can be used to filter collections of resources. They can be
  used to control how resource metrics are aggregated. And they can be
  used as arguments to policy management rules (e.g. route, firewall,
  load balancing, etc.). Optional. The default is `nil`.
  - Label keys must be between 1 and 63 characters long and must conform to the following regular expression: `[a-z]([-a-z0-9]*[a-z0-9])?`.
  - Label values must be between 0 and 63 characters long and must conform to the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
  - No more than 64 labels can be associated with a given resource.
- **query_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  query options for executing SQL query. Query options are optional.
  The following settings can be provided:
  - `:optimizer_version` (String) The version of optimizer to use. Empty to use database default. "latest" to use the latest available optimizer version.
  - `:optimizer_statistics_package` (String) Statistics package to use. Empty to use the database default.
- **directed_read_options** (Hash) *(defaults to: nil)* --- Client options used to set the directed_read_options for all ReadRequests and ExecuteSqlRequests that indicates which replicas or regions should be used for non-transactional reads or queries. Will represent [`Google::Cloud::Spanner::V1::DirectedReadOptions`](https://cloud.google.com/ruby/docs/reference/google-cloud-spanner-v1/latest/Google-Cloud-Spanner-V1-DirectedReadOptions)
- **isolation_level** (::Symbol, nil) *(defaults to: nil)* ---

  Optional. The isolation level for the transactions. Can be
  overridden by the isolation level set on the transaction.
  Can be one of the following:
  - `:ISOLATION_LEVEL_UNSPECIFIED` : The default unspecified isolation level.
  - `:SERIALIZABLE` : The serializable isolation level.
  - `:REPEATABLE_READ` : The repeatable read isolation level.
- **read_lock_mode** (::Symbol, nil) *(defaults to: nil)* ---

  Optional. The read lock mode for the transactions. Can be
  overridden by the read lock mode set on the transaction.
  Can be one of the following:
  - `:READ_LOCK_MODE_UNSPECIFIED` : The default unspecified read lock mode.
  - `:PESSIMISTIC` : The pessimistic lock mode, where depending on the isolation level and/or lock requested, locks are acquired on read.
  - `:OPTIMISTIC` : The optimistic lock mode, where locks are not acquired on read. Depending on the isolation level and/or lock requested on a read, data may be validated at commit time to be not changed since the transaction started.
**Returns**

- ([::Google::Cloud::Spanner::Client](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client)) --- The newly created client.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

db.transaction do |tx|
  results = tx.execute_query "SELECT * FROM users"

  results.rows.each do |row|
    puts "User #{row[:id]} is #{row[:name]}"
  end
end
```

### #create_database

    def create_database(instance_id, database_id, statements: [], encryption_config: nil) -> Database::Job

Creates a database and starts preparing it to begin serving.


<br />

See [Database::Job](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Database-Job "Google::Cloud::Spanner::Database::Job (class)").
**Parameters**

- **instance_id** (String) --- The unique identifier for the instance. Required.
- **database_id** (String) --- The unique identifier for the database, which cannot be changed after the database is created. Values are of the form `[a-z][a-z0-9_\-]*[a-z0-9]` and must be between 2 and 30 characters in length. Required.
- **statements** (Array\<String\>) *(defaults to: \[\])* --- DDL statements to run inside the newly created database. Statements can create tables, indexes, etc. These statements execute atomically with the creation of the database: if there is an error in any statement, the database is not created. Optional.
- **encryption_config** (Hash) *(defaults to: nil)* ---

  An encryption configuration describing
  the encryption type and key resources in Cloud KMS. Optional. The
  following settings can be provided:
  - `:kms_key_name` (String) The name of KMS key to use which should be the full path, e.g., `projects/<project>/locations/<location>\
    /keyRings/<key_ring>/cryptoKeys/<kms_key_name>`
**Returns**

- ([Database::Job](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Database-Job)) --- The job representing the long-running, asynchronous processing of a database create operation.
**Examples**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

job = spanner.create_database "my-instance",
                              "my-new-database"

job.done? #=> false
job.reload! # API call
job.done? #=> true

if job.error?
  status = job.error
else
  database = job.database
end
```

Create with encryption config

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

kms_key_name = "projects/<project>/locations/<location>/keyRings/<key_ring>/cryptoKeys/<kms_key_name>"
encryption_config = { kms_key_name: kms_key_name }
job = spanner.create_database "my-instance",
                              "my-new-database",
                              encryption_config: encryption_config

job.done? #=> false
job.reload! # API call
job.done? #=> true

if job.error?
  status = job.error
else
  database = job.database
end
```

### #create_instance

    def create_instance(instance_id, name: nil, config: nil, nodes: nil, processing_units: nil, labels: nil) -> Instance::Job

Creates a Cloud Spanner instance and starts preparing it to begin
serving.


<br />

See [Instance::Job](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance-Job "Google::Cloud::Spanner::Instance::Job (class)").
**Parameters**

- **instance_id** (String) --- The unique identifier for the instance, which cannot be changed after the instance is created. Values are of the form `[a-z][-a-z0-9]*[a-z0-9]` and must be between 6 and 30 characters in length. Required.
- **name** (String) *(defaults to: nil)* --- The descriptive name for this instance as it appears in UIs. Must be unique per project and between 4 and 30 characters in length. Required.
- **config** (String, [Instance::Config](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance-Config)) *(defaults to: nil)* --- The name of the instance's configuration. Values can be the `instance_config_id`, the full path, or an [Instance::Config](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance-Config "Google::Cloud::Spanner::Instance::Config (class)") object. Required.
- **nodes** (Integer) *(defaults to: nil)* --- The number of nodes allocated to this instance. Optional. Specify either `nodes` or `processing_units`
- **processing_units** (Integer) *(defaults to: nil)* --- The number of processing units allocated to this instance. Optional. Specify either `nodes` or `processing_units`
- **labels** (Hash) *(defaults to: nil)* ---

  Cloud Labels are a flexible and lightweight
  mechanism for organizing cloud resources into groups that reflect a
  customer's organizational needs and deployment strategies. Cloud
  Labels can be used to filter collections of resources. They can be
  used to control how resource metrics are aggregated. And they can be
  used as arguments to policy management rules (e.g. route, firewall,
  load balancing, etc.).
  - Label keys must be between 1 and 63 characters long and must conform to the following regular expression: `[a-z]([-a-z0-9]*[a-z0-9])?`.
  - Label values must be between 0 and 63 characters long and must conform to the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
  - No more than 64 labels can be associated with a given resource.
**Returns**

- ([Instance::Job](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance-Job)) --- The job representing the long-running, asynchronous processing of an instance create operation.
**Raises**

- (ArgumentError) --- if both processing_units or nodes are specified.
**Examples**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

job = spanner.create_instance "my-new-instance",
                              name: "My New Instance",
                              config: "regional-us-central1",
                              nodes: 5,
                              labels: { production: :env }

job.done? #=> false
job.reload! # API call
job.done? #=> true

if job.error?
  status = job.error
else
  instance = job.instance
end
```

Create instance using processsing units

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

job = spanner.create_instance "my-new-instance",
                              name: "My New Instance",
                              config: "regional-us-central1",
                              processing_units: 500,
                              labels: { production: :env }

job.done? #=> false
job.reload! # API call
job.done? #=> true

if job.error?
  status = job.error
else
  instance = job.instance
end
```

### #database

    def database(instance_id, database_id) -> Google::Cloud::Spanner::Database, nil

Retrieves a database by unique identifier.
**Parameters**

- **instance_id** (String) --- The unique identifier for the instance.
- **database_id** (String) --- The unique identifier for the database.
**Returns**

- ([Google::Cloud::Spanner::Database](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Database), nil) --- The database, or `nil` if the database does not exist.
**Examples**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
database = spanner.database "my-instance", "my-database"
```

Will return `nil` if instance does not exist.

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
database = spanner.database "my-instance", "my-database" # nil
```

### #databases

    def databases(instance_id, token: nil, max: nil) -> Array<Google::Cloud::Spanner::Database>

Retrieves the list of databases for the project.
**Parameters**

- **instance_id** (String) --- The unique identifier for the instance.
- **token** (String) *(defaults to: nil)* --- The `token` value returned by the last call to `databases`; indicates that this is a continuation of a call, and that the system should return the next page of data.
- **max** (Integer) *(defaults to: nil)* --- Maximum number of databases to return.
**Returns**

- (Array\<[Google::Cloud::Spanner::Database](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Database)\>) --- The list of databases. (See [Database::List](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Database-List "Google::Cloud::Spanner::Database::List (class)"))
**Examples**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

databases = spanner.databases "my-instance"
databases.each do |database|
  puts database.database_id
end
```

Retrieve all: (See [Instance::Config::List#all](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance-Config-List#Google__Cloud__Spanner__Instance__Config__List_all_instance_ "Google::Cloud::Spanner::Instance::Config::List#all (method)"))

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

databases = spanner.databases "my-instance"
databases.all do |database|
  puts database.database_id
end
```

### #instance

    def instance(instance_id) -> Google::Cloud::Spanner::Instance, nil

Retrieves a Cloud Spanner instance by unique identifier.
**Parameter**

- **instance_id** (String) --- The unique identifier for the instance.
**Returns**

- ([Google::Cloud::Spanner::Instance](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance), nil) --- The instance, or `nil` if the instance does not exist.
**Examples**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
instance = spanner.instance "my-instance"
```

Will return `nil` if instance does not exist.

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
instance = spanner.instance "non-existing" # nil
```

### #instance_config

    def instance_config(instance_config_id) -> Google::Cloud::Spanner::Instance::Config, nil

Retrieves an instance configuration by unique identifier.
**Parameter**

- **instance_config_id** (String) --- The instance configuration identifier. Values can be the `instance_config_id`, or the full path.
**Returns**

- ([Google::Cloud::Spanner::Instance::Config](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance-Config), nil) --- The instance configuration, or `nil` if the instance configuration does not exist.
**Examples**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
config = spanner.instance_config "regional-us-central1"
```

Will return `nil` if instance config does not exist.

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
config = spanner.instance_config "non-existing" # nil
```

### #instance_configs

    def instance_configs(token: nil, max: nil) -> Array<Google::Cloud::Spanner::Instance::Config>

Retrieves the list of instance configurations for the project.
**Parameters**

- **token** (String) *(defaults to: nil)* --- The `token` value returned by the last call to `instance_configs`; indicates that this is a continuation of a call, and that the system should return the next page of data.
- **max** (Integer) *(defaults to: nil)* --- Maximum number of instance configs to return.
**Returns**

- (Array\<[Google::Cloud::Spanner::Instance::Config](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance-Config)\>) --- The list of instance configurations. (See [Instance::Config::List](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance-Config-List "Google::Cloud::Spanner::Instance::Config::List (class)"))
**Examples**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

instance_configs = spanner.instance_configs
instance_configs.each do |config|
  puts config.instance_config_id
end
```

Retrieve all: (See [Instance::Config::List#all](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance-Config-List#Google__Cloud__Spanner__Instance__Config__List_all_instance_ "Google::Cloud::Spanner::Instance::Config::List#all (method)"))

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

instance_configs = spanner.instance_configs
instance_configs.all do |config|
  puts config.instance_config_id
end
```

### #instances

    def instances(token: nil, max: nil) -> Array<Google::Cloud::Spanner::Instance>

Retrieves the list of Cloud Spanner instances for the project.
**Parameters**

- **token** (String) *(defaults to: nil)* --- The `token` value returned by the last call to `instances`; indicates that this is a continuation of a call, and that the system should return the next page of data.
- **max** (Integer) *(defaults to: nil)* --- Maximum number of instances to return.
**Returns**

- (Array\<[Google::Cloud::Spanner::Instance](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance)\>) --- The list of instances. (See [Instance::List](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance-List "Google::Cloud::Spanner::Instance::List (class)"))
**Examples**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

instances = spanner.instances
instances.each do |instance|
  puts instance.instance_id
end
```

Retrieve all: (See [Instance::Config::List#all](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance-Config-List#Google__Cloud__Spanner__Instance__Config__List_all_instance_ "Google::Cloud::Spanner::Instance::Config::List#all (method)"))

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

instances = spanner.instances
instances.all do |instance|
  puts instance.instance_id
end
```

### #project

    def project() -> ::String

**Alias Of** : [#project_id](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Project#Google__Cloud__Spanner__Project_project_id_instance_) The identifier for the Cloud Spanner project.
**Returns**

- (::String)
**Example**

```ruby
require "google/cloud"

spanner = Google::Cloud::Spanner.new(
  project_id: "my-project",
  credentials: "/path/to/keyfile.json"
)

spanner.project_id #=> "my-project"
```

### #project_id

    def project_id() -> ::String

**Aliases**

- [#project](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Project#Google__Cloud__Spanner__Project_project_instance_)
The identifier for the Cloud Spanner project.
**Returns**

- (::String)
**Example**

```ruby
require "google/cloud"

spanner = Google::Cloud::Spanner.new(
  project_id: "my-project",
  credentials: "/path/to/keyfile.json"
)

spanner.project_id #=> "my-project"
```

### #universe_domain

    def universe_domain() -> String

The universe domain the client is connected to
**Returns**

- (String)