Class: Google::Cloud::Spanner::Database
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::Database
- Defined in:
- lib/google/cloud/spanner/database.rb,
lib/google/cloud/spanner/database/job.rb,
lib/google/cloud/spanner/database/list.rb,
lib/google/cloud/spanner/database/job/list.rb,
lib/google/cloud/spanner/database/backup_info.rb,
lib/google/cloud/spanner/database/restore_info.rb
Overview
Database
Represents a Cloud Spanner database. To use Cloud Spanner's read and write operations, you must first create a database. A database belongs to a Instance and contains tables and indexes. You may create multiple databases in an Instance.
See Instance#databases, Instance#database, and Instance#create_database.
To read and/or modify data in a Cloud Spanner database, use an instance of Client. See Project#client.
Defined Under Namespace
Classes: BackupInfo, Job, List, RestoreInfo
Instance Method Summary collapse
-
#backups(page_size: nil) ⇒ Array<Google::Cloud::Spanner::Backup>
Retrieves backups belonging to the database.
-
#create_backup(backup_id, expire_time, version_time: nil, encryption_config: nil) ⇒ Google::Cloud::Spanner::Backup::Job
Creates a database backup.
-
#create_time ⇒ Time
Time at which the database creation started.
-
#creating? ⇒ Boolean
The database is still being created.
-
#database_id ⇒ String
The unique identifier for the database.
-
#database_operations(filter: nil, page_size: nil) ⇒ Array<Google::Cloud::Spanner::Database::Job>
Retrieves the list of database operations for the given database.
-
#ddl(force: nil) ⇒ Array<String>
Retrieve the Data Definition Language (DDL) statements that define database structures.
-
#drop ⇒ Boolean
Drops (deletes) the Cloud Spanner database.
-
#earliest_version_time ⇒ Time
The earliest available version time for a database.
-
#encryption_config ⇒ Google::Cloud::Spanner::Admin::Database::V1::EncryptionConfig?
An encryption configuration describing the encryption type and key resources in Cloud KMS.
-
#encryption_info ⇒ Array<Google::Cloud::Spanner::Admin::Database::V1::EncryptionInfo>
Encryption information for the database.
-
#instance_id ⇒ String
The unique identifier for the instance.
-
#path ⇒ String
The full path for the database resource.
-
#policy {|policy| ... } ⇒ Policy
Gets the Cloud IAM access control policy for this database.
-
#project_id ⇒ String
The unique identifier for the project.
-
#ready? ⇒ Boolean
The database is fully created and ready for use.
-
#ready_optimizing? ⇒ Boolean
The database is fully created from backup and optimizing.
-
#restore_info ⇒ Google::Cloud::Spanner::Database::RestoreInfo?
Information about the source used to restore the database.
-
#state ⇒ Symbol
The current database state.
-
#test_permissions(*permissions) ⇒ Array<Strings>
Tests the specified permissions against the Cloud IAM access control policy.
-
#update(statements: [], operation_id: nil) ⇒ Database::Job
Updates the database schema by adding Data Definition Language (DDL) statements to create, update, and delete tables and indexes.
-
#update_policy(new_policy) ⇒ Policy
(also: #policy=)
Updates the Cloud IAM access control policy for this database.
-
#version_retention_period ⇒ String
The version retention period for a database.
Instance Method Details
#backups(page_size: nil) ⇒ Array<Google::Cloud::Spanner::Backup>
Retrieves backups belonging to the database.
572 573 574 575 576 577 578 579 |
# File 'lib/google/cloud/spanner/database.rb', line 572 def backups page_size: nil ensure_service! grpc = service.list_backups \ instance_id, filter: "database:#{database_id}", page_size: page_size Backup::List.from_grpc grpc, service end |
#create_backup(backup_id, expire_time, version_time: nil, encryption_config: nil) ⇒ Google::Cloud::Spanner::Backup::Job
Creates a database backup.
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/google/cloud/spanner/database.rb', line 523 def create_backup backup_id, expire_time, version_time: nil, encryption_config: nil ensure_service! if encryption_config&.include?(:kms_key_name) && encryption_config[:encryption_type] != :CUSTOMER_MANAGED_ENCRYPTION raise Google::Cloud::InvalidArgumentError, "kms_key_name only used with CUSTOMER_MANAGED_ENCRYPTION" end grpc = service.create_backup \ instance_id, database_id, backup_id, expire_time, version_time, encryption_config: encryption_config Backup::Job.from_grpc grpc, service end |
#create_time ⇒ Time
Time at which the database creation started.
119 120 121 |
# File 'lib/google/cloud/spanner/database.rb', line 119 def create_time Convert. @grpc.create_time end |
#creating? ⇒ Boolean
The database is still being created. Operations on the database may
raise with FAILED_PRECONDITION
in this state.
152 153 154 |
# File 'lib/google/cloud/spanner/database.rb', line 152 def creating? state == :CREATING end |
#database_id ⇒ String
The unique identifier for the database.
84 85 86 |
# File 'lib/google/cloud/spanner/database.rb', line 84 def database_id @grpc.name.split("/")[5] end |
#database_operations(filter: nil, page_size: nil) ⇒ Array<Google::Cloud::Spanner::Database::Job>
Retrieves the list of database operations for the given database.
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/google/cloud/spanner/database.rb', line 417 def database_operations filter: nil, page_size: nil database_filter = format( DATBASE_OPERATION_METADAT_FILTER_TEMPLATE, database_id: database_id ) if filter database_filter = format( "(%<filter>s) AND (%<database_filter>s)", filter: filter, database_filter: database_filter ) end grpc = service.list_database_operations instance_id, filter: database_filter, page_size: page_size Database::Job::List.from_grpc grpc, service end |
#ddl(force: nil) ⇒ Array<String>
Retrieve the Data Definition Language (DDL) statements that define database structures. DDL statements are used to create, update, and delete tables and indexes.
203 204 205 206 207 208 |
# File 'lib/google/cloud/spanner/database.rb', line 203 def ddl force: nil return @ddl if @ddl && !force ensure_service! ddl_grpc = service.get_database_ddl instance_id, database_id @ddl = ddl_grpc.statements end |
#drop ⇒ Boolean
Drops (deletes) the Cloud Spanner database.
270 271 272 273 274 |
# File 'lib/google/cloud/spanner/database.rb', line 270 def drop ensure_service! service.drop_database instance_id, database_id true end |
#earliest_version_time ⇒ Time
The earliest available version time for a database.
96 97 98 |
# File 'lib/google/cloud/spanner/database.rb', line 96 def earliest_version_time Convert. @grpc.earliest_version_time end |
#encryption_config ⇒ Google::Cloud::Spanner::Admin::Database::V1::EncryptionConfig?
An encryption configuration describing the encryption type and key resources in Cloud KMS.
127 128 129 |
# File 'lib/google/cloud/spanner/database.rb', line 127 def encryption_config @grpc.encryption_config end |
#encryption_info ⇒ Array<Google::Cloud::Spanner::Admin::Database::V1::EncryptionInfo>
Encryption information for the database.
For databases that are using customer managed encryption, this field contains the encryption information for the database, such as encryption state and the Cloud KMS key versions that are in use.
For databases that are using Google default or other types of encryption, this field is empty.
This field is propagated lazily from the backend. There might be a delay from when a key version is being used and when it appears in this field.
144 145 146 |
# File 'lib/google/cloud/spanner/database.rb', line 144 def encryption_info @grpc.encryption_info.to_a end |
#instance_id ⇒ String
The unique identifier for the instance.
78 79 80 |
# File 'lib/google/cloud/spanner/database.rb', line 78 def instance_id @grpc.name.split("/")[3] end |
#path ⇒ String
The full path for the database resource. Values are of the form
projects/<project_id>/instances/<instance_id>/databases/<database_id>
.
104 105 106 |
# File 'lib/google/cloud/spanner/database.rb', line 104 def path @grpc.name end |
#policy {|policy| ... } ⇒ Policy
Gets the Cloud IAM access control policy for this database.
623 624 625 626 627 628 629 630 |
# File 'lib/google/cloud/spanner/database.rb', line 623 def policy ensure_service! grpc = service.get_database_policy instance_id, database_id policy = Policy.from_grpc grpc return policy unless block_given? yield policy update_policy policy end |
#project_id ⇒ String
The unique identifier for the project.
72 73 74 |
# File 'lib/google/cloud/spanner/database.rb', line 72 def project_id @grpc.name.split("/")[1] end |
#ready? ⇒ Boolean
The database is fully created and ready for use.
159 160 161 |
# File 'lib/google/cloud/spanner/database.rb', line 159 def ready? state == :READY end |
#ready_optimizing? ⇒ Boolean
The database is fully created from backup and optimizing.
166 167 168 |
# File 'lib/google/cloud/spanner/database.rb', line 166 def ready_optimizing? state == :READY_OPTIMIZING end |
#restore_info ⇒ Google::Cloud::Spanner::Database::RestoreInfo?
Information about the source used to restore the database.
584 585 586 587 |
# File 'lib/google/cloud/spanner/database.rb', line 584 def restore_info return nil unless @grpc.restore_info RestoreInfo.from_grpc @grpc.restore_info end |
#state ⇒ Symbol
The current database state. Possible values are :CREATING
and
:READY
.
112 113 114 |
# File 'lib/google/cloud/spanner/database.rb', line 112 def state @grpc.state end |
#test_permissions(*permissions) ⇒ Array<Strings>
Tests the specified permissions against the Cloud IAM access control policy.
- spanner.databases.beginPartitionedDmlTransaction
- spanner.databases.create
- spanner.databases.createBackup
- spanner.databases.list
- spanner.databases.update
- spanner.databases.updateDdl
- spanner.databases.get
- spanner.databases.getDdl
- spanner.databases.getIamPolicy
- spanner.databases.setIamPolicy
- spanner.databases.beginReadOnlyTransaction
- spanner.databases.beginOrRollbackReadWriteTransaction
- spanner.databases.read
- spanner.databases.select
- spanner.databases.write
- spanner.databases.drop
711 712 713 714 715 716 717 718 |
# File 'lib/google/cloud/spanner/database.rb', line 711 def * = Array().flatten = Array().flatten ensure_service! grpc = service. \ instance_id, database_id, grpc. end |
#update(statements: [], operation_id: nil) ⇒ Database::Job
Updates the database schema by adding Data Definition Language (DDL) statements to create, update, and delete tables and indexes.
249 250 251 252 253 254 255 |
# File 'lib/google/cloud/spanner/database.rb', line 249 def update statements: [], operation_id: nil ensure_service! grpc = service.update_database_ddl instance_id, database_id, statements: statements, operation_id: operation_id Database::Job.from_grpc grpc, service end |
#update_policy(new_policy) ⇒ Policy Also known as: policy=
Updates the Cloud IAM access control
policy for this database. The policy should be read from #policy.
See Policy for an explanation of the policy
etag
property and how to modify policies.
You can also update the policy by passing a block to #policy, which will call this method internally after the block completes.
661 662 663 664 665 666 |
# File 'lib/google/cloud/spanner/database.rb', line 661 def update_policy new_policy ensure_service! grpc = service.set_database_policy \ instance_id, database_id, new_policy.to_grpc Policy.from_grpc grpc end |
#version_retention_period ⇒ String
The version retention period for a database.
90 91 92 |
# File 'lib/google/cloud/spanner/database.rb', line 90 def version_retention_period @grpc.version_retention_period end |