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
@@ -0,0 +1,169 @@
1
+ # Copyright 2020 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "delegate"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Spanner
21
+ class Backup
22
+ ##
23
+ # # List
24
+ #
25
+ # Google::Cloud::Spanner::Backup::List is a special case Array with
26
+ # additional values.
27
+ #
28
+ class List < DelegateClass(::Array)
29
+ # @private
30
+ # The gRPC Service object.
31
+ attr_accessor :service
32
+
33
+ # @private
34
+ # The gRPC page enumerable object.
35
+ attr_accessor :grpc
36
+
37
+ ##
38
+ # @private Create a new Backup::List with an array of
39
+ # Backup instances.
40
+ def initialize arr = []
41
+ super arr
42
+ end
43
+
44
+ ##
45
+ # Whether there is a next page of backups.
46
+ #
47
+ # @return [Boolean]
48
+ #
49
+ # @example
50
+ # require "google/cloud/spanner"
51
+ #
52
+ # spanner = Google::Cloud::Spanner.new
53
+ #
54
+ # instance = spanner.instance "my-instance"
55
+ # backups = instance.backups
56
+ #
57
+ # if backups.next?
58
+ # next_backups = backups.next
59
+ # end
60
+ #
61
+ def next?
62
+ grpc.next_page?
63
+ end
64
+
65
+ ##
66
+ # Retrieve the next page of backups.
67
+ #
68
+ # @return [Google::Cloud::Spanner::Backup::List]
69
+ #
70
+ # @example
71
+ # require "google/cloud/spanner"
72
+ #
73
+ # spanner = Google::Cloud::Spanner.new
74
+ #
75
+ # instance = spanner.instance "my-instance"
76
+ # backups = instance.backups
77
+
78
+ # if backups.next?
79
+ # next_backups = backups.next
80
+ # end
81
+ #
82
+ def next
83
+ ensure_service!
84
+
85
+ return nil unless next?
86
+ grpc.next_page
87
+ self.class.from_grpc grpc, service
88
+ end
89
+
90
+ ##
91
+ # Retrieves remaining results by repeatedly invoking {#next} until
92
+ # {#next?} returns `false`. Calls the given block once for each
93
+ # result, which is passed as the argument to the block.
94
+ #
95
+ # An Enumerator is returned if no block is given.
96
+ #
97
+ # This method will make repeated API calls until all remaining results
98
+ # are retrieved. (Unlike `#each`, for example, which merely iterates
99
+ # over the results returned by a single API call.) Use with caution.
100
+ #
101
+ # @yield [backup] The block for accessing each backup.
102
+ # @yieldparam [Google::Cloud::Spanner::Backup] backup The backup
103
+ # object.
104
+ #
105
+ # @return [Enumerator]
106
+ #
107
+ # @example Iterating each backup by passing a block:
108
+ # require "google/cloud/spanner"
109
+ #
110
+ # spanner = Google::Cloud::Spanner.new
111
+ #
112
+ # instance = spanner.instance "my-instance"
113
+ # backups = instance.backups
114
+ #
115
+ # backups.all do |backup|
116
+ # puts backup.backup_id
117
+ # end
118
+ #
119
+ # @example Using the enumerator by not passing a block:
120
+ # require "google/cloud/spanner"
121
+ #
122
+ # spanner = Google::Cloud::Spanner.new
123
+ #
124
+ # instance = spanner.instance "my-instance"
125
+ # backups = instance.backups
126
+ #
127
+ # all_backup_ids = backups.all.map do |backup|
128
+ # backup.backup_id
129
+ # end
130
+ #
131
+ def all
132
+ return enum_for :all unless block_given?
133
+
134
+ results = self
135
+ loop do
136
+ results.each { |r| yield r }
137
+ break unless next?
138
+ grpc.next_page
139
+ results = self.class.from_grpc grpc, service
140
+ end
141
+ end
142
+
143
+ ##
144
+ # @private
145
+ # New Backup::List from a
146
+ # `Gapic::PagedEnumerable<Google::Cloud::Spanner::Admin::Database::V1::Backup>`
147
+ # object.
148
+ def self.from_grpc grpc, service
149
+ backups = List.new(Array(grpc.response.backups).map do |backup|
150
+ Backup.from_grpc backup, service
151
+ end)
152
+
153
+ backups.grpc = grpc
154
+ backups.service = service
155
+ backups
156
+ end
157
+
158
+ protected
159
+
160
+ ##
161
+ # Raise an error unless an active service is available.
162
+ def ensure_service!
163
+ raise "Must have active connection" unless @service
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,246 @@
1
+ # Copyright 2020 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "google/cloud/spanner/status"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Spanner
21
+ class Backup
22
+ class Restore
23
+ ##
24
+ # # Job
25
+ #
26
+ # A resource representing the long-running, asynchronous processing of
27
+ # a backup restore. The job can be refreshed to retrieve the restored
28
+ # database object once the operation has been completed.
29
+ #
30
+ # See {Backup#restore}
31
+ #
32
+ # @see https://cloud.google.com/spanner/reference/rpc/google.longrunning#google.longrunning.Operation
33
+ # Long-running Operation
34
+ #
35
+ # @example
36
+ # require "google/cloud/spanner"
37
+ #
38
+ # spanner = Google::Cloud::Spanner.new
39
+ #
40
+ # instance = spanner.instance "my-instance"
41
+ # backup = instance.backup "my-backup"
42
+ # job = backup.restore "my-restored-database"
43
+ #
44
+ # job.done? #=> false
45
+ # job.reload! # API call
46
+ # job.done? #=> true
47
+ #
48
+ # if job.error?
49
+ # status = job.error
50
+ # else
51
+ # database = job.database
52
+ # end
53
+ #
54
+ class Job
55
+ ##
56
+ # @private The `Gapic::Operation` gRPC object.
57
+ attr_accessor :grpc
58
+
59
+ ##
60
+ # @private The gRPC Service object.
61
+ attr_accessor :service
62
+
63
+ ##
64
+ # @private Creates a new Restore::Job instance.
65
+ def initialize
66
+ @grpc = nil
67
+ @service = nil
68
+ end
69
+
70
+ ##
71
+ # The database is the object of the operation.
72
+ #
73
+ # @return [Database, nil] The database instance, or
74
+ # `nil` if the operation is not complete.
75
+ #
76
+ # @example
77
+ # require "google/cloud/spanner"
78
+ #
79
+ # spanner = Google::Cloud::Spanner.new
80
+ #
81
+ # instance = spanner.instance "my-instance"
82
+ # backup = instance.backup "my-backup"
83
+ # job = backup.restore "my-restored-database"
84
+ #
85
+ # job.done? #=> false
86
+ # job.reload!
87
+ # job.done? #=> true
88
+ # database = job.database
89
+ #
90
+ def database
91
+ return nil unless done?
92
+ return nil unless @grpc.grpc_op.result == :response
93
+ Database.from_grpc @grpc.results, service
94
+ end
95
+
96
+ ##
97
+ # Checks if the processing of the restore operation is complete.
98
+ #
99
+ # @return [boolean] `true` when complete, `false` otherwise.
100
+ #
101
+ # @example
102
+ # require "google/cloud/spanner"
103
+ #
104
+ # spanner = Google::Cloud::Spanner.new
105
+ #
106
+ # instance = spanner.instance "my-instance"
107
+ # backup = instance.backup "my-backup"
108
+ # job = backup.restore "my-restored-database"
109
+ #
110
+ # job.done? #=> false
111
+ #
112
+ def done?
113
+ @grpc.done?
114
+ end
115
+
116
+ ##
117
+ # Checks if the processing of the restore operation has errored.
118
+ #
119
+ # @return [boolean] `true` when errored, `false` otherwise.
120
+ #
121
+ # @example
122
+ # require "google/cloud/spanner"
123
+ #
124
+ # spanner = Google::Cloud::Spanner.new
125
+ #
126
+ # instance = spanner.instance "my-instance"
127
+ # backup = instance.backup "my-backup"
128
+ # job = backup.restore "my-restored-database"
129
+ #
130
+ # job.error? #=> false
131
+ #
132
+ def error?
133
+ @grpc.error?
134
+ end
135
+
136
+ ##
137
+ # The status if the operation associated with this job produced an
138
+ # error.
139
+ #
140
+ # @return [Google::Cloud::Spanner::Status, nil] A status object with
141
+ # the status code and message, or `nil` if no error occurred.
142
+ #
143
+ # @example
144
+ # require "google/cloud/spanner"
145
+ #
146
+ # spanner = Google::Cloud::Spanner.new
147
+ #
148
+ # instance = spanner.instance "my-instance"
149
+ # backup = instance.backup "my-backup"
150
+ # job = backup.restore "my-restored-database"
151
+ #
152
+ # job.error? # true
153
+ #
154
+ # error = job.error
155
+ #
156
+ def error
157
+ return nil unless error?
158
+ Google::Cloud::Spanner::Status.from_grpc @grpc.error
159
+ end
160
+
161
+ ##
162
+ # Reloads the job with current data from the long-running,
163
+ # asynchronous processing of a restore operation.
164
+ #
165
+ # @return [Backup::Job] The same job instance.
166
+ #
167
+ # @example
168
+ # require "google/cloud/spanner"
169
+ #
170
+ # spanner = Google::Cloud::Spanner.new
171
+ #
172
+ # instance = spanner.instance "my-instance"
173
+ # backup = instance.backup "my-backup"
174
+ # job = backup.restore "my-restored-database"
175
+ #
176
+ # job.done? #=> false
177
+ # job.reload! # API call
178
+ # job.done? #=> true
179
+ #
180
+ def reload!
181
+ @grpc.reload!
182
+ self
183
+ end
184
+ alias refresh! reload!
185
+
186
+ ##
187
+ # Reloads the job until the operation is complete. The delay between
188
+ # reloads will incrementally increase.
189
+ #
190
+ # @example
191
+ # require "google/cloud/spanner"
192
+ #
193
+ # spanner = Google::Cloud::Spanner.new
194
+ #
195
+ # instance = spanner.instance "my-instance"
196
+ # backup = instance.backup "my-backup"
197
+ # job = backup.restore "my-restored-database"
198
+ #
199
+ # job.done? #=> false
200
+ # job.wait_until_done!
201
+ # job.done? #=> true
202
+ #
203
+ def wait_until_done!
204
+ @grpc.wait_until_done!
205
+ end
206
+
207
+ ##
208
+ # The operation progress in percentage.
209
+ #
210
+ # @return [Integer]
211
+ def progress_percent
212
+ @grpc.metadata.progress.progress_percent
213
+ end
214
+
215
+ ##
216
+ # The operation start time.
217
+ #
218
+ # @return [Time, nil]
219
+ def start_time
220
+ return nil unless @grpc.metadata.progress.start_time
221
+ Convert.timestamp_to_time @grpc.metadata.progress.start_time
222
+ end
223
+
224
+ ##
225
+ # The operation end time.
226
+ #
227
+ # @return [Time, nil]
228
+ def end_time
229
+ return nil unless @grpc.metadata.progress.end_time
230
+ Convert.timestamp_to_time @grpc.metadata.progress.end_time
231
+ end
232
+
233
+ ##
234
+ # @private New Restore::Job from a `Gapic::Operation` object.
235
+ def self.from_grpc grpc, service
236
+ new.tap do |job|
237
+ job.instance_variable_set :@grpc, grpc
238
+ job.instance_variable_set :@service, service
239
+ end
240
+ end
241
+ end
242
+ end
243
+ end
244
+ end
245
+ end
246
+ end
@@ -65,11 +65,13 @@ module Google
65
65
  class BatchClient
66
66
  ##
67
67
  # @private Creates a new Spanner BatchClient instance.
68
- def initialize project, instance_id, database_id, session_labels: nil
68
+ def initialize project, instance_id, database_id, session_labels: nil,
69
+ query_options: nil
69
70
  @project = project
70
71
  @instance_id = instance_id
71
72
  @database_id = database_id
72
73
  @session_labels = session_labels
74
+ @query_options = query_options
73
75
  end
74
76
 
75
77
  # The unique identifier for the project.
@@ -231,7 +233,7 @@ module Google
231
233
  def load_batch_snapshot serialized_snapshot
232
234
  ensure_service!
233
235
 
234
- BatchSnapshot.load serialized_snapshot, service: @project.service
236
+ BatchSnapshot.load serialized_snapshot, service: @project.service, query_options: @query_options
235
237
  end
236
238
 
237
239
  ##
@@ -405,11 +407,11 @@ module Google
405
407
  def session
406
408
  ensure_service!
407
409
  grpc = @project.service.create_session \
408
- Admin::Database::V1::DatabaseAdminClient.database_path(
409
- project_id, instance_id, database_id
410
+ V1::Spanner::Paths.database_path(
411
+ project: project_id, instance: instance_id, database: database_id
410
412
  ),
411
413
  labels: @session_labels
412
- Session.from_grpc grpc, @project.service
414
+ Session.from_grpc grpc, @project.service, query_options: @query_options
413
415
  end
414
416
 
415
417
  ##