google-cloud-spanner 1.14.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +1 -1
  3. data/CHANGELOG.md +51 -0
  4. data/CONTRIBUTING.md +5 -5
  5. data/TROUBLESHOOTING.md +0 -6
  6. data/lib/google-cloud-spanner.rb +23 -13
  7. data/lib/google/cloud/spanner.rb +8 -9
  8. data/lib/google/cloud/spanner/admin/database/credentials.rb +2 -2
  9. data/lib/google/cloud/spanner/admin/instance/credentials.rb +2 -2
  10. data/lib/google/cloud/spanner/backup.rb +315 -0
  11. data/lib/google/cloud/spanner/backup/job.rb +274 -0
  12. data/lib/google/cloud/spanner/backup/job/list.rb +177 -0
  13. data/lib/google/cloud/spanner/backup/list.rb +169 -0
  14. data/lib/google/cloud/spanner/backup/restore/job.rb +246 -0
  15. data/lib/google/cloud/spanner/batch_client.rb +7 -5
  16. data/lib/google/cloud/spanner/batch_snapshot.rb +49 -16
  17. data/lib/google/cloud/spanner/batch_update.rb +1 -1
  18. data/lib/google/cloud/spanner/client.rb +65 -16
  19. data/lib/google/cloud/spanner/commit.rb +14 -14
  20. data/lib/google/cloud/spanner/convert.rb +7 -7
  21. data/lib/google/cloud/spanner/credentials.rb +2 -2
  22. data/lib/google/cloud/spanner/data.rb +2 -2
  23. data/lib/google/cloud/spanner/database.rb +275 -15
  24. data/lib/google/cloud/spanner/database/backup_info.rb +105 -0
  25. data/lib/google/cloud/spanner/database/job.rb +5 -2
  26. data/lib/google/cloud/spanner/database/job/list.rb +177 -0
  27. data/lib/google/cloud/spanner/database/list.rb +1 -1
  28. data/lib/google/cloud/spanner/database/restore_info.rb +63 -0
  29. data/lib/google/cloud/spanner/fields.rb +8 -8
  30. data/lib/google/cloud/spanner/instance.rb +401 -8
  31. data/lib/google/cloud/spanner/instance/config.rb +1 -1
  32. data/lib/google/cloud/spanner/instance/config/list.rb +1 -1
  33. data/lib/google/cloud/spanner/instance/job.rb +2 -2
  34. data/lib/google/cloud/spanner/instance/list.rb +1 -1
  35. data/lib/google/cloud/spanner/partition.rb +4 -4
  36. data/lib/google/cloud/spanner/policy.rb +2 -2
  37. data/lib/google/cloud/spanner/project.rb +31 -6
  38. data/lib/google/cloud/spanner/results.rb +4 -2
  39. data/lib/google/cloud/spanner/service.rb +231 -240
  40. data/lib/google/cloud/spanner/session.rb +46 -15
  41. data/lib/google/cloud/spanner/snapshot.rb +28 -5
  42. data/lib/google/cloud/spanner/transaction.rb +51 -7
  43. data/lib/google/cloud/spanner/version.rb +1 -1
  44. metadata +28 -104
  45. data/lib/google/cloud/spanner/admin/database.rb +0 -148
  46. data/lib/google/cloud/spanner/admin/database/v1.rb +0 -146
  47. data/lib/google/cloud/spanner/admin/database/v1/credentials.rb +0 -46
  48. data/lib/google/cloud/spanner/admin/database/v1/database_admin_client.rb +0 -791
  49. data/lib/google/cloud/spanner/admin/database/v1/database_admin_client_config.json +0 -71
  50. data/lib/google/cloud/spanner/admin/database/v1/doc/google/iam/v1/iam_policy.rb +0 -64
  51. data/lib/google/cloud/spanner/admin/database/v1/doc/google/iam/v1/options.rb +0 -33
  52. data/lib/google/cloud/spanner/admin/database/v1/doc/google/iam/v1/policy.rb +0 -151
  53. data/lib/google/cloud/spanner/admin/database/v1/doc/google/longrunning/operations.rb +0 -51
  54. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/any.rb +0 -131
  55. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/empty.rb +0 -29
  56. data/lib/google/cloud/spanner/admin/database/v1/doc/google/rpc/status.rb +0 -39
  57. data/lib/google/cloud/spanner/admin/database/v1/doc/google/spanner/admin/database/v1/spanner_database_admin.rb +0 -201
  58. data/lib/google/cloud/spanner/admin/database/v1/doc/google/type/expr.rb +0 -45
  59. data/lib/google/cloud/spanner/admin/instance.rb +0 -164
  60. data/lib/google/cloud/spanner/admin/instance/v1.rb +0 -162
  61. data/lib/google/cloud/spanner/admin/instance/v1/credentials.rb +0 -46
  62. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/iam/v1/iam_policy.rb +0 -64
  63. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/iam/v1/options.rb +0 -33
  64. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/iam/v1/policy.rb +0 -151
  65. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/longrunning/operations.rb +0 -51
  66. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/any.rb +0 -131
  67. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/empty.rb +0 -29
  68. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/field_mask.rb +0 -222
  69. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/rpc/status.rb +0 -39
  70. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/spanner/admin/instance/v1/spanner_instance_admin.rb +0 -341
  71. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/type/expr.rb +0 -45
  72. data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client.rb +0 -972
  73. data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client_config.json +0 -76
  74. data/lib/google/cloud/spanner/v1.rb +0 -16
  75. data/lib/google/cloud/spanner/v1/credentials.rb +0 -42
  76. data/lib/google/cloud/spanner/v1/doc/google/protobuf/any.rb +0 -131
  77. data/lib/google/cloud/spanner/v1/doc/google/protobuf/duration.rb +0 -91
  78. data/lib/google/cloud/spanner/v1/doc/google/protobuf/empty.rb +0 -29
  79. data/lib/google/cloud/spanner/v1/doc/google/protobuf/struct.rb +0 -74
  80. data/lib/google/cloud/spanner/v1/doc/google/protobuf/timestamp.rb +0 -113
  81. data/lib/google/cloud/spanner/v1/doc/google/rpc/status.rb +0 -39
  82. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/keys.rb +0 -150
  83. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/mutation.rb +0 -88
  84. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/query_plan.rb +0 -121
  85. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/result_set.rb +0 -190
  86. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/spanner.rb +0 -581
  87. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/transaction.rb +0 -432
  88. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/type.rb +0 -112
  89. data/lib/google/cloud/spanner/v1/spanner_client.rb +0 -1509
  90. data/lib/google/cloud/spanner/v1/spanner_client_config.json +0 -121
  91. data/lib/google/spanner/admin/database/v1/spanner_database_admin_pb.rb +0 -88
  92. data/lib/google/spanner/admin/database/v1/spanner_database_admin_services_pb.rb +0 -99
  93. data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_pb.rb +0 -125
  94. data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_services_pb.rb +0 -182
  95. data/lib/google/spanner/v1/keys_pb.rb +0 -34
  96. data/lib/google/spanner/v1/mutation_pb.rb +0 -39
  97. data/lib/google/spanner/v1/query_plan_pb.rb +0 -48
  98. data/lib/google/spanner/v1/result_set_pb.rb +0 -48
  99. data/lib/google/spanner/v1/spanner_pb.rb +0 -174
  100. data/lib/google/spanner/v1/spanner_services_pb.rb +0 -188
  101. data/lib/google/spanner/v1/transaction_pb.rb +0 -56
  102. data/lib/google/spanner/v1/type_pb.rb +0 -44
@@ -77,7 +77,7 @@ module Google
77
77
 
78
78
  ##
79
79
  # @private Creates a new Instance::Config instance from a
80
- # Google::Spanner::Admin::Instance::V1::InstanceConfig.
80
+ # `Google::Cloud::Spanner::Admin::Instance::V1::InstanceConfig`.
81
81
  def self.from_grpc grpc
82
82
  new grpc
83
83
  end
@@ -142,7 +142,7 @@ module Google
142
142
 
143
143
  ##
144
144
  # @private New Instance::Config::List from a
145
- # Google::Spanner::Admin::Instance::V1::ListInstanceConfigsResponse
145
+ # `Google::Cloud::Spanner::Admin::Instance::V1::ListInstanceConfigsResponse`
146
146
  # object.
147
147
  def self.from_grpc grpc, service, max = nil
148
148
  configs = List.new(Array(grpc.instance_configs).map do |config|
@@ -54,7 +54,7 @@ module Google
54
54
  #
55
55
  class Job
56
56
  ##
57
- # @private The Google::Gax::Operation gRPC object.
57
+ # @private The `Gapic::Operation` gRPC object.
58
58
  attr_accessor :grpc
59
59
 
60
60
  ##
@@ -219,7 +219,7 @@ module Google
219
219
  end
220
220
 
221
221
  ##
222
- # @private New Instance::Job from a Google::Gax::Operation object.
222
+ # @private New Instance::Job from a `Gapic::Operation` object.
223
223
  def self.from_grpc grpc, service
224
224
  new.tap do |job|
225
225
  job.instance_variable_set :@grpc, grpc
@@ -139,7 +139,7 @@ module Google
139
139
 
140
140
  ##
141
141
  # @private New Instance::List from a
142
- # Google::Spanner::Admin::Instance::V1::ListInstancesResponse
142
+ # `Google::Cloud::Spanner::Admin::Instance::V1::ListInstancesResponse`
143
143
  # object.
144
144
  def self.from_grpc grpc, service, max = nil
145
145
  instances = List.new(Array(grpc.instances).map do |instance|
@@ -172,13 +172,13 @@ module Google
172
172
  new.tap do |p|
173
173
  if data[:execute]
174
174
  execute_sql_grpc = \
175
- Google::Spanner::V1::ExecuteSqlRequest.decode(
175
+ V1::ExecuteSqlRequest.decode(
176
176
  Base64.decode64(data[:execute])
177
177
  )
178
178
  p.instance_variable_set :@execute, execute_sql_grpc
179
179
  end
180
180
  if data[:read]
181
- read_grpc = Google::Spanner::V1::ReadRequest.decode \
181
+ read_grpc = V1::ReadRequest.decode \
182
182
  Base64.decode64(data[:read])
183
183
  p.instance_variable_set :@read, read_grpc
184
184
  end
@@ -194,7 +194,7 @@ module Google
194
194
  end
195
195
 
196
196
  ##
197
- # @private New Partition from a Google::Spanner::V1::ExecuteSqlRequest
197
+ # @private New Partition from a `Google::Cloud::Spanner::V1::ExecuteSqlRequest`
198
198
  # object.
199
199
  def self.from_execute_sql_grpc grpc
200
200
  new.tap do |p|
@@ -203,7 +203,7 @@ module Google
203
203
  end
204
204
 
205
205
  ##
206
- # @private New Partition from a Google::Spanner::V1::ReadRequest object.
206
+ # @private New Partition from a `Google::Cloud::Spanner::V1::ReadRequest` object.
207
207
  def self.from_read_grpc grpc
208
208
  new.tap do |p|
209
209
  p.instance_variable_set :@read, grpc
@@ -159,7 +159,7 @@ module Google
159
159
  end
160
160
 
161
161
  ##
162
- # @private Convert the Policy to a Google::Iam::V1::Policy object.
162
+ # @private Convert the Policy to a `Google::Iam::V1::Policy` object.
163
163
  def to_grpc
164
164
  Google::Iam::V1::Policy.new(
165
165
  etag: etag,
@@ -174,7 +174,7 @@ module Google
174
174
  end
175
175
 
176
176
  ##
177
- # @private New Policy from a Google::Iam::V1::Policy object.
177
+ # @private New Policy from a `Google::Iam::V1::Policy` object.
178
178
  def self.from_grpc grpc
179
179
  roles = grpc.bindings.each_with_object({}) do |binding, memo|
180
180
  memo[binding.role] = binding.members.to_a
@@ -68,12 +68,13 @@ module Google
68
68
  class Project
69
69
  ##
70
70
  # @private The Service object.
71
- attr_accessor :service
71
+ attr_accessor :service, :query_options
72
72
 
73
73
  ##
74
74
  # @private Creates a new Spanner Project instance.
75
- def initialize service
75
+ def initialize service, query_options: nil
76
76
  @service = service
77
+ @query_options = query_options
77
78
  end
78
79
 
79
80
  ##
@@ -452,6 +453,13 @@ module Google
452
453
  # * Label values must be between 0 and 63 characters long and must
453
454
  # conform to the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
454
455
  # * No more than 64 labels can be associated with a given resource.
456
+ # @param [Hash] query_options A hash of values to specify the custom
457
+ # query options for executing SQL query. Query options are optional.
458
+ # The following settings can be provided:
459
+ #
460
+ # * `:optimizer_version` (String) The version of optimizer to use.
461
+ # Empty to use database default. "latest" to use the latest
462
+ # available optimizer version.
455
463
  #
456
464
  # @return [Client] The newly created client.
457
465
  #
@@ -470,12 +478,20 @@ module Google
470
478
  # end
471
479
  # end
472
480
  #
473
- def client instance_id, database_id, pool: {}, labels: nil
481
+ def client instance_id, database_id, pool: {}, labels: nil,
482
+ query_options: nil
474
483
  # Convert from possible Google::Protobuf::Map
475
484
  labels = Hash[labels.map { |k, v| [String(k), String(v)] }] if labels
485
+ # Configs set by environment variables take over client-level configs.
486
+ if query_options.nil?
487
+ query_options = @query_options
488
+ else
489
+ query_options = query_options.merge @query_options unless @query_options.nil?
490
+ end
476
491
  Client.new self, instance_id, database_id,
477
492
  session_labels: labels,
478
- pool_opts: valid_session_pool_options(pool)
493
+ pool_opts: valid_session_pool_options(pool),
494
+ query_options: query_options
479
495
  end
480
496
 
481
497
  ##
@@ -501,6 +517,13 @@ module Google
501
517
  # * Label values must be between 0 and 63 characters long and must
502
518
  # conform to the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
503
519
  # * No more than 64 labels can be associated with a given resource.
520
+ # @param [Hash] query_options A hash of values to specify the custom
521
+ # query options for executing SQL query. Query options are optional.
522
+ # The following settings can be provided:
523
+ #
524
+ # * `:optimizer_version` (String) The version of optimizer to use.
525
+ # Empty to use database default. "latest" to use the latest
526
+ # available optimizer version.
504
527
  #
505
528
  # @return [Client] The newly created client.
506
529
  #
@@ -529,10 +552,12 @@ module Google
529
552
  # results = new_batch_snapshot.execute_partition \
530
553
  # new_partition
531
554
  #
532
- def batch_client instance_id, database_id, labels: nil
555
+ def batch_client instance_id, database_id, labels: nil,
556
+ query_options: nil
533
557
  # Convert from possible Google::Protobuf::Map
534
558
  labels = Hash[labels.map { |k, v| [String(k), String(v)] }] if labels
535
- BatchClient.new self, instance_id, database_id, session_labels: labels
559
+ BatchClient.new self, instance_id, database_id, session_labels: labels,
560
+ query_options: query_options
536
561
  end
537
562
 
538
563
  protected
@@ -242,10 +242,12 @@ module Google
242
242
 
243
243
  def self.execute_query service, session_path, sql, params: nil,
244
244
  types: nil, transaction: nil,
245
- partition_token: nil, seqno: nil
245
+ partition_token: nil, seqno: nil,
246
+ query_options: nil
246
247
  execute_query_options = {
247
248
  transaction: transaction, params: params, types: types,
248
- partition_token: partition_token, seqno: seqno
249
+ partition_token: partition_token, seqno: seqno,
250
+ query_options: query_options
249
251
  }
250
252
  enum = service.execute_streaming_sql session_path, sql,
251
253
  execute_query_options
@@ -20,7 +20,6 @@ require "google/cloud/spanner/v1"
20
20
  require "google/cloud/spanner/admin/instance/v1"
21
21
  require "google/cloud/spanner/admin/database/v1"
22
22
  require "google/cloud/spanner/convert"
23
- require "uri"
24
23
 
25
24
  module Google
26
25
  module Cloud
@@ -29,18 +28,17 @@ module Google
29
28
  # @private Represents the gRPC Spanner service, including all the API
30
29
  # methods.
31
30
  class Service
32
- attr_accessor :project, :credentials, :timeout, :client_config, :host,
33
- :lib_name, :lib_version
31
+ attr_accessor :project, :credentials, :timeout, :host, :lib_name,
32
+ :lib_version
34
33
 
35
34
  ##
36
35
  # Creates a new Service instance.
37
- def initialize project, credentials, host: nil, timeout: nil,
38
- client_config: nil, lib_name: nil, lib_version: nil
36
+ def initialize project, credentials,
37
+ host: nil, timeout: nil, lib_name: nil, lib_version: nil
39
38
  @project = project
40
39
  @credentials = credentials
41
- @host = host || V1::SpannerClient::SERVICE_ADDRESS
40
+ @host = host
42
41
  @timeout = timeout
43
- @client_config = client_config || {}
44
42
  @lib_name = lib_name
45
43
  @lib_version = lib_version
46
44
  end
@@ -64,45 +62,42 @@ module Google
64
62
  def service
65
63
  return mocked_service if mocked_service
66
64
  @service ||= \
67
- V1::SpannerClient.new(
68
- credentials: channel,
69
- timeout: timeout,
70
- client_config: client_config,
71
- service_address: service_address,
72
- service_port: service_port,
73
- lib_name: lib_name_with_prefix,
74
- lib_version: Google::Cloud::Spanner::VERSION
75
- )
65
+ V1::Spanner::Client.new do |config|
66
+ config.credentials = channel
67
+ config.timeout = timeout if timeout
68
+ config.endpoint = host if host
69
+ config.lib_name = lib_name_with_prefix
70
+ config.lib_version = Google::Cloud::Spanner::VERSION
71
+ config.metadata = { "google-cloud-resource-prefix" => "projects/#{@project}" }
72
+ end
76
73
  end
77
74
  attr_accessor :mocked_service
78
75
 
79
76
  def instances
80
77
  return mocked_instances if mocked_instances
81
78
  @instances ||= \
82
- Admin::Instance::V1::InstanceAdminClient.new(
83
- credentials: channel,
84
- timeout: timeout,
85
- client_config: client_config,
86
- service_address: service_address,
87
- service_port: service_port,
88
- lib_name: lib_name_with_prefix,
89
- lib_version: Google::Cloud::Spanner::VERSION
90
- )
79
+ Admin::Instance::V1::InstanceAdmin::Client.new do |config|
80
+ config.credentials = channel
81
+ config.timeout = timeout if timeout
82
+ config.endpoint = host if host
83
+ config.lib_name = lib_name_with_prefix
84
+ config.lib_version = Google::Cloud::Spanner::VERSION
85
+ config.metadata = { "google-cloud-resource-prefix" => "projects/#{@project}" }
86
+ end
91
87
  end
92
88
  attr_accessor :mocked_instances
93
89
 
94
90
  def databases
95
91
  return mocked_databases if mocked_databases
96
92
  @databases ||= \
97
- Admin::Database::V1::DatabaseAdminClient.new(
98
- credentials: channel,
99
- timeout: timeout,
100
- client_config: client_config,
101
- service_address: service_address,
102
- service_port: service_port,
103
- lib_name: lib_name_with_prefix,
104
- lib_version: Google::Cloud::Spanner::VERSION
105
- )
93
+ Admin::Database::V1::DatabaseAdmin::Client.new do |config|
94
+ config.credentials = channel
95
+ config.timeout = timeout if timeout
96
+ config.endpoint = host if host
97
+ config.lib_name = lib_name_with_prefix
98
+ config.lib_version = Google::Cloud::Spanner::VERSION
99
+ config.metadata = { "google-cloud-resource-prefix" => "projects/#{@project}" }
100
+ end
106
101
  end
107
102
  attr_accessor :mocked_databases
108
103
 
@@ -111,222 +106,180 @@ module Google
111
106
  end
112
107
 
113
108
  def list_instances token: nil, max: nil
114
- call_options = nil
115
- call_options = Google::Gax::CallOptions.new page_token: token if token
116
-
117
- execute do
118
- paged_enum = instances.list_instances project_path,
119
- page_size: max,
120
- options: call_options
121
-
122
- paged_enum.page.response
123
- end
109
+ paged_enum = instances.list_instances parent: project_path,
110
+ page_size: max,
111
+ page_token: token
112
+ paged_enum.response
124
113
  end
125
114
 
126
115
  def get_instance name
127
- execute do
128
- instances.get_instance instance_path(name)
129
- end
116
+ instances.get_instance name: instance_path(name)
130
117
  end
131
118
 
132
119
  def create_instance instance_id, name: nil, config: nil, nodes: nil,
133
120
  labels: nil
134
121
  labels = Hash[labels.map { |k, v| [String(k), String(v)] }] if labels
135
122
 
136
- create_obj = Google::Spanner::Admin::Instance::V1::Instance.new({
123
+ create_obj = Admin::Instance::V1::Instance.new({
137
124
  display_name: name, config: instance_config_path(config),
138
125
  node_count: nodes, labels: labels
139
126
  }.delete_if { |_, v| v.nil? })
140
127
 
141
- execute do
142
- instances.create_instance project_path, instance_id, create_obj
143
- end
128
+ instances.create_instance parent: project_path,
129
+ instance_id: instance_id,
130
+ instance: create_obj
144
131
  end
145
132
 
146
- def update_instance instance_obj
133
+ def update_instance instance
147
134
  mask = Google::Protobuf::FieldMask.new(
148
135
  paths: %w[display_name node_count labels]
149
136
  )
150
137
 
151
- execute do
152
- instances.update_instance instance_obj, mask
153
- end
138
+ instances.update_instance instance: instance, field_mask: mask
154
139
  end
155
140
 
156
141
  def delete_instance name
157
- execute do
158
- instances.delete_instance instance_path(name)
159
- end
142
+ instances.delete_instance name: instance_path(name)
160
143
  end
161
144
 
162
145
  def get_instance_policy name
163
- execute do
164
- instances.get_iam_policy instance_path(name)
165
- end
146
+ instances.get_iam_policy resource: instance_path(name)
166
147
  end
167
148
 
168
149
  def set_instance_policy name, new_policy
169
- execute do
170
- instances.set_iam_policy instance_path(name), new_policy
171
- end
150
+ instances.set_iam_policy resource: instance_path(name),
151
+ policy: new_policy
172
152
  end
173
153
 
174
154
  def test_instance_permissions name, permissions
175
- execute do
176
- instances.test_iam_permissions instance_path(name), permissions
177
- end
155
+ instances.test_iam_permissions resource: instance_path(name),
156
+ permissions: permissions
178
157
  end
179
158
 
180
159
  def list_instance_configs token: nil, max: nil
181
- call_options = nil
182
- call_options = Google::Gax::CallOptions.new page_token: token if token
183
-
184
- execute do
185
- paged_enum = instances.list_instance_configs project_path,
186
- page_size: max,
187
- options: call_options
188
-
189
- paged_enum.page.response
190
- end
160
+ paged_enum = instances.list_instance_configs \
161
+ parent: project_path, page_size: max, page_token: token
162
+ paged_enum.response
191
163
  end
192
164
 
193
165
  def get_instance_config name
194
- execute do
195
- instances.get_instance_config instance_config_path(name)
196
- end
166
+ instances.get_instance_config name: instance_config_path(name)
197
167
  end
198
168
 
199
169
  def list_databases instance_id, token: nil, max: nil
200
- call_options = nil
201
- call_options = Google::Gax::CallOptions.new page_token: token if token
202
-
203
- execute do
204
- paged_enum = databases.list_databases instance_path(instance_id),
205
- page_size: max,
206
- options: call_options
207
-
208
- paged_enum.page.response
209
- end
170
+ paged_enum = databases.list_databases \
171
+ parent: instance_path(instance_id),
172
+ page_size: max,
173
+ page_token: token
174
+ paged_enum.response
210
175
  end
211
176
 
212
177
  def get_database instance_id, database_id
213
- execute do
214
- databases.get_database database_path(instance_id, database_id)
215
- end
178
+ databases.get_database name: database_path(instance_id, database_id)
216
179
  end
217
180
 
218
181
  def create_database instance_id, database_id, statements: []
219
- execute do
220
- databases.create_database \
221
- instance_path(instance_id),
222
- "CREATE DATABASE `#{database_id}`",
223
- extra_statements: Array(statements)
224
- end
182
+ databases.create_database \
183
+ parent: instance_path(instance_id),
184
+ create_statement: "CREATE DATABASE `#{database_id}`",
185
+ extra_statements: Array(statements)
225
186
  end
226
187
 
227
188
  def drop_database instance_id, database_id
228
- execute do
229
- databases.drop_database database_path(instance_id, database_id)
230
- end
189
+ databases.drop_database \
190
+ database: database_path(instance_id, database_id)
231
191
  end
232
192
 
233
193
  def get_database_ddl instance_id, database_id
234
- execute do
235
- databases.get_database_ddl database_path(instance_id, database_id)
236
- end
194
+ databases.get_database_ddl \
195
+ database: database_path(instance_id, database_id)
237
196
  end
238
197
 
239
198
  def update_database_ddl instance_id, database_id, statements: [],
240
199
  operation_id: nil
241
- execute do
242
- databases.update_database_ddl \
243
- database_path(instance_id, database_id),
244
- Array(statements),
245
- operation_id: operation_id
246
- end
200
+ databases.update_database_ddl \
201
+ database: database_path(instance_id, database_id),
202
+ statements: Array(statements),
203
+ operation_id: operation_id
247
204
  end
248
205
 
249
206
  def get_database_policy instance_id, database_id
250
- execute do
251
- databases.get_iam_policy database_path(instance_id, database_id)
252
- end
207
+ databases.get_iam_policy \
208
+ resource: database_path(instance_id, database_id)
253
209
  end
254
210
 
255
211
  def set_database_policy instance_id, database_id, new_policy
256
- execute do
257
- databases.set_iam_policy \
258
- database_path(instance_id, database_id), new_policy
259
- end
212
+ databases.set_iam_policy \
213
+ resource: database_path(instance_id, database_id),
214
+ policy: new_policy
260
215
  end
261
216
 
262
217
  def test_database_permissions instance_id, database_id, permissions
263
- execute do
264
- databases.test_iam_permissions \
265
- database_path(instance_id, database_id), permissions
266
- end
218
+ databases.test_iam_permissions \
219
+ resource: database_path(instance_id, database_id),
220
+ permissions: permissions
267
221
  end
268
222
 
269
223
  def get_session session_name
270
224
  opts = default_options_from_session session_name
271
- execute do
272
- service.get_session session_name, options: opts
273
- end
225
+ service.get_session({ name: session_name }, opts)
274
226
  end
275
227
 
276
228
  def create_session database_name, labels: nil
277
229
  opts = default_options_from_session database_name
278
- session = Google::Spanner::V1::Session.new labels: labels if labels
279
- execute do
280
- service.create_session database_name, session: session,
281
- options: opts
282
- end
230
+ session = V1::Session.new labels: labels if labels
231
+ service.create_session(
232
+ { database: database_name, session: session }, opts
233
+ )
283
234
  end
284
235
 
285
236
  def batch_create_sessions database_name, session_count, labels: nil
286
237
  opts = default_options_from_session database_name
287
- session = Google::Spanner::V1::Session.new labels: labels if labels
288
- execute do
289
- # The response may have fewer sessions than requested in the RPC.
290
- service.batch_create_sessions database_name,
291
- session_count,
292
- session_template: session,
293
- options: opts
294
- end
238
+ session = V1::Session.new labels: labels if labels
239
+ # The response may have fewer sessions than requested in the RPC.
240
+ request = {
241
+ database: database_name,
242
+ session_count: session_count,
243
+ session_template: session
244
+ }
245
+ service.batch_create_sessions request, opts
295
246
  end
296
247
 
297
248
  def delete_session session_name
298
249
  opts = default_options_from_session session_name
299
- execute do
300
- service.delete_session session_name, options: opts
301
- end
250
+ service.delete_session({ name: session_name }, opts)
302
251
  end
303
252
 
304
253
  def execute_streaming_sql session_name, sql, transaction: nil,
305
254
  params: nil, types: nil, resume_token: nil,
306
- partition_token: nil, seqno: nil
255
+ partition_token: nil, seqno: nil,
256
+ query_options: nil
307
257
  opts = default_options_from_session session_name
308
- execute do
309
- service.execute_streaming_sql \
310
- session_name, sql, transaction: transaction,
311
- params: params,
312
- param_types: types,
313
- resume_token: resume_token,
314
- partition_token: partition_token,
315
- seqno: seqno,
316
- options: opts
317
- end
258
+ request = {
259
+ session: session_name,
260
+ sql: sql,
261
+ transaction: transaction,
262
+ params: params,
263
+ param_types: types,
264
+ resume_token: resume_token,
265
+ partition_token: partition_token,
266
+ seqno: seqno,
267
+ query_options: query_options
268
+ }
269
+ service.execute_streaming_sql request, opts
318
270
  end
319
271
 
320
272
  def execute_batch_dml session_name, transaction, statements, seqno
321
273
  opts = default_options_from_session session_name
322
274
  statements = statements.map(&:to_grpc)
323
- results = execute do
324
- service.execute_batch_dml session_name,
325
- transaction,
326
- statements,
327
- seqno,
328
- options: opts
329
- end
275
+ request = {
276
+ session: session_name,
277
+ transaction: transaction,
278
+ statements: statements,
279
+ seqno: seqno
280
+ }
281
+ results = service.execute_batch_dml request, opts
282
+
330
283
  if results.status.code.zero?
331
284
  results.result_sets.map { |rs| rs.stats.row_count_exact }
332
285
  else
@@ -342,13 +295,13 @@ module Google
342
295
  index: nil, transaction: nil, limit: nil,
343
296
  resume_token: nil, partition_token: nil
344
297
  opts = default_options_from_session session_name
345
- execute do
346
- service.streaming_read \
347
- session_name, table_name, columns, keys,
348
- transaction: transaction, index: index, limit: limit,
349
- resume_token: resume_token, partition_token: partition_token,
350
- options: opts
351
- end
298
+ request = {
299
+ session: session_name, table: table_name, columns: columns,
300
+ key_set: keys, transaction: transaction, index: index,
301
+ limit: limit, resume_token: resume_token,
302
+ partition_token: partition_token
303
+ }
304
+ service.streaming_read request, opts
352
305
  end
353
306
 
354
307
  def partition_read session_name, table_name, columns, transaction,
@@ -358,13 +311,12 @@ module Google
358
311
  max_partitions
359
312
 
360
313
  opts = default_options_from_session session_name
361
-
362
- execute do
363
- service.partition_read \
364
- session_name, table_name, keys,
365
- transaction: transaction, index: index, columns: columns,
366
- partition_options: partition_opts, options: opts
367
- end
314
+ request = {
315
+ session: session_name, table: table_name, key_set: keys,
316
+ transaction: transaction, index: index, columns: columns,
317
+ partition_options: partition_opts
318
+ }
319
+ service.partition_read request, opts
368
320
  end
369
321
 
370
322
  def partition_query session_name, sql, transaction, params: nil,
@@ -374,53 +326,48 @@ module Google
374
326
  max_partitions
375
327
 
376
328
  opts = default_options_from_session session_name
377
-
378
- execute do
379
- service.partition_query \
380
- session_name, sql,
381
- transaction: transaction,
382
- params: params, param_types: types,
383
- partition_options: partition_opts, options: opts
384
- end
329
+ request = {
330
+ session: session_name, sql: sql, transaction: transaction,
331
+ params: params, param_types: types,
332
+ partition_options: partition_opts
333
+ }
334
+ service.partition_query request, opts
385
335
  end
386
336
 
387
337
  def commit session_name, mutations = [], transaction_id: nil
388
338
  tx_opts = nil
389
339
  if transaction_id.nil?
390
- tx_opts = Google::Spanner::V1::TransactionOptions.new(
391
- read_write: Google::Spanner::V1::TransactionOptions::ReadWrite.new
340
+ tx_opts = V1::TransactionOptions.new(
341
+ read_write: V1::TransactionOptions::ReadWrite.new
392
342
  )
393
343
  end
394
344
  opts = default_options_from_session session_name
395
- execute do
396
- service.commit \
397
- session_name, mutations,
398
- transaction_id: transaction_id, single_use_transaction: tx_opts,
399
- options: opts
400
- end
345
+ request = {
346
+ session: session_name, transaction_id: transaction_id,
347
+ single_use_transaction: tx_opts, mutations: mutations
348
+ }
349
+ service.commit request, opts
401
350
  end
402
351
 
403
352
  def rollback session_name, transaction_id
404
353
  opts = default_options_from_session session_name
405
- execute do
406
- service.rollback session_name, transaction_id, options: opts
407
- end
354
+ request = { session: session_name, transaction_id: transaction_id }
355
+ service.rollback request, opts
408
356
  end
409
357
 
410
358
  def begin_transaction session_name
411
- tx_opts = Google::Spanner::V1::TransactionOptions.new(
412
- read_write: Google::Spanner::V1::TransactionOptions::ReadWrite.new
359
+ tx_opts = V1::TransactionOptions.new(
360
+ read_write: V1::TransactionOptions::ReadWrite.new
413
361
  )
414
362
  opts = default_options_from_session session_name
415
- execute do
416
- service.begin_transaction session_name, tx_opts, options: opts
417
- end
363
+ request = { session: session_name, options: tx_opts }
364
+ service.begin_transaction request, opts
418
365
  end
419
366
 
420
367
  def create_snapshot session_name, strong: nil, timestamp: nil,
421
368
  staleness: nil
422
- tx_opts = Google::Spanner::V1::TransactionOptions.new(
423
- read_only: Google::Spanner::V1::TransactionOptions::ReadOnly.new(
369
+ tx_opts = V1::TransactionOptions.new(
370
+ read_only: V1::TransactionOptions::ReadOnly.new(
424
371
  {
425
372
  strong: strong,
426
373
  read_timestamp: Convert.time_to_timestamp(timestamp),
@@ -430,38 +377,87 @@ module Google
430
377
  )
431
378
  )
432
379
  opts = default_options_from_session session_name
433
- execute do
434
- service.begin_transaction session_name, tx_opts, options: opts
435
- end
380
+ request = { session: session_name, options: tx_opts }
381
+ service.begin_transaction request, opts
436
382
  end
437
383
 
438
384
  def create_pdml session_name
439
- tx_opts = Google::Spanner::V1::TransactionOptions.new(
440
- partitioned_dml: \
441
- Google::Spanner::V1::TransactionOptions::PartitionedDml.new
385
+ tx_opts = V1::TransactionOptions.new(
386
+ partitioned_dml: V1::TransactionOptions::PartitionedDml.new
442
387
  )
443
388
  opts = default_options_from_session session_name
444
- execute do
445
- service.begin_transaction session_name, tx_opts, options: opts
446
- end
389
+ request = { session: session_name, options: tx_opts }
390
+ service.begin_transaction request, opts
447
391
  end
448
392
 
449
- def inspect
450
- "#{self.class}(#{@project})"
393
+ def create_backup instance_id, database_id, backup_id, expire_time
394
+ backup = {
395
+ database: database_path(instance_id, database_id),
396
+ expire_time: expire_time
397
+ }
398
+ databases.create_backup parent: instance_path(instance_id),
399
+ backup_id: backup_id,
400
+ backup: backup
451
401
  end
452
402
 
453
- protected
403
+ def get_backup instance_id, backup_id
404
+ databases.get_backup name: backup_path(instance_id, backup_id)
405
+ end
406
+
407
+ def update_backup backup, update_mask
408
+ databases.update_backup backup: backup, update_mask: update_mask
409
+ end
410
+
411
+ def delete_backup instance_id, backup_id
412
+ databases.delete_backup name: backup_path(instance_id, backup_id)
413
+ end
454
414
 
455
- def service_address
456
- return nil if host.nil?
457
- URI.parse("//#{host}").host
415
+ def list_backups instance_id,
416
+ filter: nil, page_size: nil, page_token: nil
417
+ databases.list_backups parent: instance_path(instance_id),
418
+ filter: filter,
419
+ page_size: page_size,
420
+ page_token: page_token
458
421
  end
459
422
 
460
- def service_port
461
- return nil if host.nil?
462
- URI.parse("//#{host}").port
423
+ def list_database_operations instance_id,
424
+ filter: nil,
425
+ page_size: nil,
426
+ page_token: nil
427
+ databases.list_database_operations(
428
+ parent: instance_path(instance_id),
429
+ filter: filter,
430
+ page_size: page_size,
431
+ page_token: page_token
432
+ )
433
+ end
434
+
435
+ def list_backup_operations instance_id,
436
+ filter: nil, page_size: nil,
437
+ page_token: nil
438
+ databases.list_backup_operations(
439
+ parent: instance_path(instance_id),
440
+ filter: filter,
441
+ page_size: page_size,
442
+ page_token: page_token
443
+ )
444
+ end
445
+
446
+ def restore_database backup_instance_id, backup_id,
447
+ database_instance_id, database_id
448
+ databases.restore_database(
449
+ parent: instance_path(database_instance_id),
450
+ database_id: database_id,
451
+ backup: backup_path(backup_instance_id, backup_id)
452
+ )
463
453
  end
464
454
 
455
+ def inspect
456
+ "#{self.class}(#{@project})"
457
+ end
458
+
459
+ protected
460
+
465
461
  def lib_name_with_prefix
466
462
  return "gccl" if [nil, "gccl"].include? lib_name
467
463
 
@@ -472,13 +468,12 @@ module Google
472
468
 
473
469
  def default_options_from_session session_name
474
470
  default_prefix = session_name.split("/sessions/").first
475
- Google::Gax::CallOptions.new kwargs: \
476
- { "google-cloud-resource-prefix" => default_prefix }
471
+ { metadata: { "google-cloud-resource-prefix" => default_prefix } }
477
472
  end
478
473
 
479
474
  def partition_options partition_size_bytes, max_partitions
480
475
  return nil unless partition_size_bytes || max_partitions
481
- partition_opts = Google::Spanner::V1::PartitionOptions.new
476
+ partition_opts = V1::PartitionOptions.new
482
477
  if partition_size_bytes
483
478
  partition_opts.partition_size_bytes = partition_size_bytes
484
479
  end
@@ -487,42 +482,38 @@ module Google
487
482
  end
488
483
 
489
484
  def project_path
490
- Admin::Instance::V1::InstanceAdminClient.project_path project
485
+ Admin::Instance::V1::InstanceAdmin::Paths.project_path \
486
+ project: project
491
487
  end
492
488
 
493
489
  def instance_path name
494
490
  return name if name.to_s.include? "/"
495
- Admin::Instance::V1::InstanceAdminClient.instance_path(
496
- project, name
497
- )
491
+
492
+ Admin::Instance::V1::InstanceAdmin::Paths.instance_path \
493
+ project: project, instance: name
498
494
  end
499
495
 
500
496
  def instance_config_path name
501
497
  return name if name.to_s.include? "/"
502
- Admin::Instance::V1::InstanceAdminClient.instance_config_path(
503
- project, name.to_s
504
- )
498
+
499
+ Admin::Instance::V1::InstanceAdmin::Paths.instance_config_path \
500
+ project: project, instance_config: name
505
501
  end
506
502
 
507
503
  def database_path instance_id, database_id
508
- Admin::Database::V1::DatabaseAdminClient.database_path(
509
- project, instance_id, database_id
510
- )
504
+ Admin::Database::V1::DatabaseAdmin::Paths.database_path \
505
+ project: project, instance: instance_id, database: database_id
511
506
  end
512
507
 
513
508
  def session_path instance_id, database_id, session_id
514
- V1::SpannerClient.session_path(
515
- project, instance_id, database_id, session_id
516
- )
509
+ V1::Spanner::Paths.session_path \
510
+ project: project, instance: instance_id, database: database_id,
511
+ session: session_id
517
512
  end
518
513
 
519
- def execute
520
- yield
521
- rescue Google::Gax::GaxError => e
522
- # GaxError wraps BadStatus, but exposes it as #cause
523
- raise Google::Cloud::Error.from_error(e.cause)
524
- rescue GRPC::BadStatus => e
525
- raise Google::Cloud::Error.from_error(e)
514
+ def backup_path instance_id, backup_id
515
+ Admin::Database::V1::DatabaseAdmin::Paths.backup_path \
516
+ project: project, instance: instance_id, backup: backup_id
526
517
  end
527
518
  end
528
519
  end