google-cloud-spanner 1.15.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +1 -1
  3. data/CHANGELOG.md +49 -0
  4. data/CONTRIBUTING.md +5 -5
  5. data/TROUBLESHOOTING.md +0 -6
  6. data/lib/google-cloud-spanner.rb +10 -13
  7. data/lib/google/cloud/spanner.rb +6 -8
  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 +2 -2
  16. data/lib/google/cloud/spanner/batch_snapshot.rb +120 -23
  17. data/lib/google/cloud/spanner/batch_update.rb +1 -1
  18. data/lib/google/cloud/spanner/client.rb +189 -30
  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/results.rb +9 -6
  38. data/lib/google/cloud/spanner/service.rb +392 -302
  39. data/lib/google/cloud/spanner/session.rb +186 -31
  40. data/lib/google/cloud/spanner/snapshot.rb +60 -6
  41. data/lib/google/cloud/spanner/transaction.rb +116 -10
  42. data/lib/google/cloud/spanner/version.rb +1 -1
  43. metadata +28 -109
  44. data/lib/google/cloud/spanner/admin/database.rb +0 -149
  45. data/lib/google/cloud/spanner/admin/database/v1.rb +0 -147
  46. data/lib/google/cloud/spanner/admin/database/v1/credentials.rb +0 -46
  47. data/lib/google/cloud/spanner/admin/database/v1/database_admin_client.rb +0 -1513
  48. data/lib/google/cloud/spanner/admin/database/v1/database_admin_client_config.json +0 -111
  49. data/lib/google/cloud/spanner/admin/database/v1/doc/google/iam/v1/iam_policy.rb +0 -64
  50. data/lib/google/cloud/spanner/admin/database/v1/doc/google/iam/v1/options.rb +0 -33
  51. data/lib/google/cloud/spanner/admin/database/v1/doc/google/iam/v1/policy.rb +0 -151
  52. data/lib/google/cloud/spanner/admin/database/v1/doc/google/longrunning/operations.rb +0 -51
  53. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/any.rb +0 -131
  54. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/empty.rb +0 -29
  55. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/field_mask.rb +0 -222
  56. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/timestamp.rb +0 -113
  57. data/lib/google/cloud/spanner/admin/database/v1/doc/google/rpc/status.rb +0 -39
  58. data/lib/google/cloud/spanner/admin/database/v1/doc/google/spanner/admin/database/v1/backup.rb +0 -325
  59. data/lib/google/cloud/spanner/admin/database/v1/doc/google/spanner/admin/database/v1/spanner_database_admin.rb +0 -368
  60. data/lib/google/cloud/spanner/admin/database/v1/doc/google/type/expr.rb +0 -45
  61. data/lib/google/cloud/spanner/admin/instance.rb +0 -164
  62. data/lib/google/cloud/spanner/admin/instance/v1.rb +0 -162
  63. data/lib/google/cloud/spanner/admin/instance/v1/credentials.rb +0 -46
  64. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/iam/v1/iam_policy.rb +0 -64
  65. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/iam/v1/options.rb +0 -33
  66. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/iam/v1/policy.rb +0 -151
  67. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/longrunning/operations.rb +0 -51
  68. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/any.rb +0 -131
  69. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/empty.rb +0 -29
  70. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/field_mask.rb +0 -222
  71. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/rpc/status.rb +0 -39
  72. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/spanner/admin/instance/v1/spanner_instance_admin.rb +0 -334
  73. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/type/expr.rb +0 -45
  74. data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client.rb +0 -975
  75. data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client_config.json +0 -76
  76. data/lib/google/cloud/spanner/v1.rb +0 -16
  77. data/lib/google/cloud/spanner/v1/credentials.rb +0 -42
  78. data/lib/google/cloud/spanner/v1/doc/google/protobuf/any.rb +0 -131
  79. data/lib/google/cloud/spanner/v1/doc/google/protobuf/duration.rb +0 -91
  80. data/lib/google/cloud/spanner/v1/doc/google/protobuf/empty.rb +0 -29
  81. data/lib/google/cloud/spanner/v1/doc/google/protobuf/struct.rb +0 -74
  82. data/lib/google/cloud/spanner/v1/doc/google/protobuf/timestamp.rb +0 -113
  83. data/lib/google/cloud/spanner/v1/doc/google/rpc/status.rb +0 -39
  84. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/keys.rb +0 -150
  85. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/mutation.rb +0 -95
  86. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/query_plan.rb +0 -121
  87. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/result_set.rb +0 -190
  88. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/spanner.rb +0 -570
  89. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/transaction.rb +0 -432
  90. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/type.rb +0 -112
  91. data/lib/google/cloud/spanner/v1/spanner_client.rb +0 -1485
  92. data/lib/google/cloud/spanner/v1/spanner_client_config.json +0 -121
  93. data/lib/google/spanner/admin/database/v1/backup_pb.rb +0 -98
  94. data/lib/google/spanner/admin/database/v1/common_pb.rb +0 -28
  95. data/lib/google/spanner/admin/database/v1/spanner_database_admin_pb.rb +0 -141
  96. data/lib/google/spanner/admin/database/v1/spanner_database_admin_services_pb.rb +0 -169
  97. data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_pb.rb +0 -125
  98. data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_services_pb.rb +0 -181
  99. data/lib/google/spanner/v1/keys_pb.rb +0 -34
  100. data/lib/google/spanner/v1/mutation_pb.rb +0 -39
  101. data/lib/google/spanner/v1/query_plan_pb.rb +0 -48
  102. data/lib/google/spanner/v1/result_set_pb.rb +0 -48
  103. data/lib/google/spanner/v1/spanner_pb.rb +0 -179
  104. data/lib/google/spanner/v1/spanner_services_pb.rb +0 -179
  105. data/lib/google/spanner/v1/transaction_pb.rb +0 -56
  106. data/lib/google/spanner/v1/type_pb.rb +0 -44
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 31f7875bf6e782a6e02f78fc4c56059b6a7a33aad2aea8e5bb8ddb93932749e6
4
- data.tar.gz: 2d0babe313899246327cdc92fe537a94e5b2a8d58d2e300913bc79bfac4ced84
3
+ metadata.gz: d7eb1575c3244eb459d8379345e2dea6f19c925bbeb526ff969aa07a34428c94
4
+ data.tar.gz: 9c8c0b2d99074ec99ff38125ccbf89713f0d7c5441445650b96b5a969b09629a
5
5
  SHA512:
6
- metadata.gz: 73fd57fd66a11124e0e987f9dedde6bc1435a5ada931fab6eec310ecc219c977c977eef845eeb134f184dd41903a8f60a630573ba455f597ddbbcd768c00a593
7
- data.tar.gz: 39c0e508c9f06534fa6ea0ba9ecf91fff841b6d4709ccdfd2cbcafefa879b47a2c96d9678a96de109098962f4a31b462eea1893f2a00d0025a76f1c6fb2cb1b0
6
+ metadata.gz: ba900cc5a539d55baead3d8825472b05b40ab19adc3062dfb93c7cf1713805b9619c2cbd7b115f3e2e61f79371e4c53eb8a225f8cd1f7a3dc1c30f72e1590cc7
7
+ data.tar.gz: 1639fd79b6bd30af238b2d002852bec94e4f29aa581594758169180e15e6b335f86c5a6cac1fa9a13a7fef02edc105ea46eb4867a02c59bf0a069c142d8799e2
@@ -76,7 +76,7 @@ The environment variables that google-cloud-spanner checks for project ID are:
76
76
  1. `SPANNER_PROJECT`
77
77
  2. `GOOGLE_CLOUD_PROJECT`
78
78
 
79
- The environment variables that google-cloud-spanner checks for credentials are configured on {Google::Cloud::Spanner::V1::Credentials}:
79
+ The environment variables that google-cloud-spanner checks for credentials are configured on `Google::Cloud::Spanner::V1::Spanner::Credentials`:
80
80
 
81
81
  1. `SPANNER_CREDENTIALS` - Path to JSON file, or JSON contents
82
82
  2. `SPANNER_KEYFILE` - Path to JSON file, or JSON contents
@@ -1,5 +1,54 @@
1
1
  # Release History
2
2
 
3
+ ### 2.1.0 / 2020-08-05
4
+
5
+ #### Features
6
+
7
+ * Support custom setting of timeout and retry
8
+
9
+ ### 2.0.0 / 2020-07-23
10
+
11
+ This is a major update that removes the "low-level" client interface code, and
12
+ instead adds `google-cloud-spanner-v1`, `google-cloud-spanner-admin-database-v1`,
13
+ and `google-cloud-spanner-admin-instance-v1` as dependencies.
14
+ The new dependencies are rewritten low-level clients, produced by a next-
15
+ generation client code generator, with improved performance and stability.
16
+
17
+ This change should have no effect on the high-level interface that most users
18
+ will use. The one exception is that the (mostly undocumented) `client_config`
19
+ argument, for adjusting low-level parameters such as RPC retry settings on
20
+ client objects, has been removed. If you need to adjust these parameters, use
21
+ the configuration interface in low-level clients.
22
+
23
+ Substantial changes have been made in the low-level interfaces, however. If you
24
+ are using the low-level classes under the old `Google::Spanner::V1` module,
25
+ please review the docs for the new `google-cloud-spanner-v1` gem. In particular:
26
+
27
+ * Some classes have been renamed, notably the client class itself.
28
+ * The client constructor takes a configuration block instead of configuration
29
+ keyword arguments.
30
+ * All RPC method arguments are now keyword arguments.
31
+
32
+ ### 1.16.2 / 2020-05-28
33
+
34
+ #### Documentation
35
+
36
+ * Fix a few broken links
37
+
38
+ ### 1.16.1 / 2020-05-21
39
+
40
+ #### Bug Fixes
41
+
42
+ * Increased default timeouts to match clients in other languages
43
+ * Run system tests against the emulator, skipping those not supported by the emulator
44
+ * Do not require a key file when running against the emulator
45
+
46
+ ### 1.16.0 / 2020-03-20
47
+
48
+ #### Features
49
+
50
+ * Added support for backing up and restoring databases ([#5053](https://www.github.com/googleapis/google-cloud-ruby/issues/5053))
51
+
3
52
  ### 1.15.0 / 2020-03-15
4
53
 
5
54
  #### Features
@@ -53,7 +53,7 @@ there is a small amount of setup:
53
53
  In order to run code interactively, you can automatically load
54
54
  google-cloud-spanner and its dependencies in IRB. This requires that your
55
55
  developer environment has already been configured by following the steps
56
- described in the [Authentication Guide](AUTHENTICATION.md). An IRB console
56
+ described in the {file:AUTHENTICATION.md Authentication Guide}. An IRB console
57
57
  can be created with:
58
58
 
59
59
  ```sh
@@ -119,7 +119,7 @@ If you alter an example's title, you may encounter breaking tests.
119
119
  ### Spanner Acceptance Tests
120
120
 
121
121
  The Spanner acceptance tests interact with the live service API. Follow the
122
- instructions in the [Authentication Guide](AUTHENTICATION.md) for enabling
122
+ instructions in the {file:AUTHENTICATION.md Authentication Guide} for enabling
123
123
  the Spanner API. Occasionally, some API features may not yet be generally
124
124
  available, making it difficult for some contributors to successfully run the
125
125
  entire acceptance test suite. However, please ensure that you do successfully
@@ -127,7 +127,7 @@ run acceptance tests for any code areas covered by your pull request.
127
127
 
128
128
  To run the acceptance tests, first create and configure a project in the Google
129
129
  Developers Console, as described in the
130
- [Authentication Guide](AUTHENTICATION.md). Be sure to download the JSON KEY
130
+ {file:AUTHENTICATION.md Authentication Guide}. Be sure to download the JSON KEY
131
131
  file. Make note of the PROJECT_ID and the KEYFILE location on your system.
132
132
 
133
133
  Before you can run the Spanner acceptance tests, you must first create indexes
@@ -184,5 +184,5 @@ $ bundle exec rake rubocop
184
184
  ## Code of Conduct
185
185
 
186
186
  Please note that this project is released with a Contributor Code of Conduct. By
187
- participating in this project you agree to abide by its terms. See
188
- [Code of Conduct](CODE_OF_CONDUCT.md) for more information.
187
+ participating in this project you agree to abide by its terms. See the
188
+ {file:CODE_OF_CONDUCT.md Code of Conduct} for more information.
@@ -24,14 +24,8 @@ improved, *please* create a new issue on GitHub so we can talk about it.
24
24
 
25
25
  - [New issue][gh-ruby]
26
26
 
27
- Or, you can ask questions on the [Google Cloud Platform Slack][slack-ruby]. You
28
- can use the "ruby" channel for general Ruby questions, or use the
29
- "google-cloud-ruby" channel if you have questions about this gem in particular.
30
-
31
27
  [so-ruby]: http://stackoverflow.com/questions/tagged/google-cloud-platform+ruby+spanner
32
28
 
33
29
  [gh-search-ruby]: https://github.com/googlecloudplatform/google-cloud-ruby/issues?q=label%3A%22api%3A+spanner%22
34
30
 
35
31
  [gh-ruby]: https://github.com/googlecloudplatform/google-cloud-ruby/issues/new
36
-
37
- [slack-ruby]: https://gcp-slack.appspot.com/
@@ -43,8 +43,6 @@ module Google
43
43
  # * `https://www.googleapis.com/auth/spanner`
44
44
  # * `https://www.googleapis.com/auth/spanner.data`
45
45
  # @param [Integer] timeout Default timeout to use in requests. Optional.
46
- # @param [Hash] client_config A hash of values to override the default
47
- # behavior of the API client. Optional.
48
46
  # @param [String] lib_name Library name. This will be added as a prefix
49
47
  # to the API call tracking header `x-goog-api-client` with provided
50
48
  # lib version for telemetry. Optional. For example prefix looks like
@@ -75,11 +73,9 @@ module Google
75
73
  # platform_scope = "https://www.googleapis.com/auth/cloud-platform"
76
74
  # spanner = gcloud.spanner scope: platform_scope
77
75
  #
78
- def spanner scope: nil, timeout: nil, client_config: nil, lib_name: nil,
79
- lib_version: nil
76
+ def spanner scope: nil, timeout: nil, lib_name: nil, lib_version: nil
80
77
  Google::Cloud.spanner @project, @keyfile, scope: scope,
81
78
  timeout: (timeout || @timeout),
82
- client_config: client_config,
83
79
  lib_name: lib_name,
84
80
  lib_version: lib_version
85
81
  end
@@ -107,8 +103,6 @@ module Google
107
103
  # * `https://www.googleapis.com/auth/spanner`
108
104
  # * `https://www.googleapis.com/auth/spanner.data`
109
105
  # @param [Integer] timeout Default timeout to use in requests. Optional.
110
- # @param [Hash] client_config A hash of values to override the default
111
- # behavior of the API client. Optional.
112
106
  # @param [String] lib_name Library name. This will be added as a prefix
113
107
  # to the API call tracking header `x-goog-api-client` with provided
114
108
  # lib version for telemetry. Optional. For example prefix looks like
@@ -132,13 +126,11 @@ module Google
132
126
  # spanner = Google::Cloud.spanner
133
127
  #
134
128
  def self.spanner project_id = nil, credentials = nil, scope: nil,
135
- timeout: nil, client_config: nil, lib_name: nil,
136
- lib_version: nil
129
+ timeout: nil, lib_name: nil, lib_version: nil
137
130
  require "google/cloud/spanner"
138
131
  Google::Cloud::Spanner.new project_id: project_id,
139
132
  credentials: credentials,
140
133
  scope: scope, timeout: timeout,
141
- client_config: client_config,
142
134
  lib_name: lib_name, lib_version: lib_version
143
135
  end
144
136
  end
@@ -169,16 +161,21 @@ Google::Cloud.configure.add_config! :spanner do |config|
169
161
  query_options
170
162
  end
171
163
 
164
+ default_scopes = [
165
+ "https://www.googleapis.com/auth/cloud-platform",
166
+ "https://www.googleapis.com/auth/spanner.admin",
167
+ "https://www.googleapis.com/auth/spanner.data"
168
+ ]
169
+
172
170
  config.add_field! :project_id, default_project, match: String, allow_nil: true
173
171
  config.add_alias! :project, :project_id
174
172
  config.add_field! :credentials, default_creds,
175
173
  match: [String, Hash, Google::Auth::Credentials],
176
174
  allow_nil: true
177
175
  config.add_alias! :keyfile, :credentials
178
- config.add_field! :scope, nil, match: [String, Array]
176
+ config.add_field! :scope, default_scopes, match: [String, Array]
179
177
  config.add_field! :timeout, nil, match: Integer
180
- config.add_field! :client_config, nil, match: Hash
181
- config.add_field! :endpoint, nil, match: String
178
+ config.add_field! :endpoint, "spanner.googleapis.com", match: String
182
179
  config.add_field! :emulator_host, default_emulator, match: String, allow_nil: true
183
180
  config.add_field! :lib_name, nil, match: String, allow_nil: true
184
181
  config.add_field! :lib_version, nil, match: String, allow_nil: true
@@ -59,8 +59,6 @@ module Google
59
59
  # * `https://www.googleapis.com/auth/spanner`
60
60
  # * `https://www.googleapis.com/auth/spanner.data`
61
61
  # @param [Integer] timeout Default timeout to use in requests. Optional.
62
- # @param [Hash] client_config A hash of values to override the default
63
- # behavior of the API client. Optional.
64
62
  # @param [String] endpoint Override of the endpoint host name. Optional.
65
63
  # If the param is nil, uses the default endpoint.
66
64
  # @param [String] project Alias for the `project_id` argument. Deprecated.
@@ -91,12 +89,11 @@ module Google
91
89
  # spanner = Google::Cloud::Spanner.new
92
90
  #
93
91
  def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil,
94
- client_config: nil, endpoint: nil, project: nil, keyfile: nil,
92
+ endpoint: nil, project: nil, keyfile: nil,
95
93
  emulator_host: nil, lib_name: nil, lib_version: nil
96
94
  project_id ||= (project || default_project_id)
97
95
  scope ||= configure.scope
98
96
  timeout ||= configure.timeout
99
- client_config ||= configure.client_config
100
97
  endpoint ||= configure.endpoint
101
98
  credentials ||= (keyfile || default_credentials(scope: scope))
102
99
  emulator_host ||= configure.emulator_host
@@ -122,8 +119,8 @@ module Google
122
119
  Spanner::Project.new(
123
120
  Spanner::Service.new(
124
121
  project_id, credentials,
125
- host: endpoint, timeout: timeout, client_config: client_config,
126
- lib_name: lib_name, lib_version: lib_version
122
+ host: endpoint, timeout: timeout, lib_name: lib_name,
123
+ lib_version: lib_version
127
124
  ),
128
125
  query_options: configure.query_options
129
126
  )
@@ -145,8 +142,6 @@ module Google
145
142
  # * `scope` - (String, Array<String>) The OAuth 2.0 scopes controlling
146
143
  # the set of resources and operations that the connection can access.
147
144
  # * `timeout` - (Integer) Default timeout to use in requests.
148
- # * `client_config` - (Hash) A hash of values to override the default
149
- # behavior of the API client.
150
145
  # * `endpoint` - (String) Override of the endpoint host name, or `nil`
151
146
  # to use the default endpoint.
152
147
  # * `emulator_host` - (String) Host name of the emulator. Defaults to
@@ -184,4 +179,7 @@ module Google
184
179
  end
185
180
  end
186
181
  end
182
+
183
+ # @private
184
+ Spanner = Cloud::Spanner unless const_defined? :Spanner
187
185
  end
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- require "googleauth"
16
+ require "google/cloud/spanner/admin/database/v1/database_admin/credentials"
17
17
 
18
18
  module Google
19
19
  module Cloud
@@ -23,7 +23,7 @@ module Google
23
23
  ##
24
24
  # @deprecated Use version-specific credentials classes
25
25
  #
26
- Credentials = Google::Cloud::Spanner::Admin::Database::V1::Credentials
26
+ Credentials = V1::DatabaseAdmin::Credentials
27
27
  end
28
28
  end
29
29
  end
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- require "googleauth"
16
+ require "google/cloud/spanner/admin/instance/v1/instance_admin/credentials"
17
17
 
18
18
  module Google
19
19
  module Cloud
@@ -23,7 +23,7 @@ module Google
23
23
  ##
24
24
  # @deprecated Use version-specific credentials classes
25
25
  #
26
- Credentials = Google::Cloud::Spanner::Admin::Instance::V1::Credentials
26
+ Credentials = V1::InstanceAdmin::Credentials
27
27
  end
28
28
  end
29
29
  end
@@ -0,0 +1,315 @@
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/backup/job"
17
+ require "google/cloud/spanner/backup/list"
18
+ require "google/cloud/spanner/backup/restore/job"
19
+
20
+ module Google
21
+ module Cloud
22
+ module Spanner
23
+ ##
24
+ # # Backup
25
+ #
26
+ # A backup is a representation of Cloud Spanner database backup.
27
+ #
28
+ # See {Google::Cloud::Spanner::Instance#backups},
29
+ # {Google::Cloud::Spanner::Instance#backup}, and
30
+ # {Google::Cloud::Spanner::Database#create_backup}.
31
+ #
32
+ # @example
33
+ # require "google/cloud"
34
+ #
35
+ # spanner = Google::Cloud::Spanner.new
36
+ # database = spanner.database "my-instance", "my-database"
37
+ #
38
+ # expire_time = Time.now + 36000
39
+ # job = database.create_backup "my-backup", expire_time
40
+ #
41
+ # job.done? #=> false
42
+ # job.reload! # API call
43
+ # job.done? #=> true
44
+ #
45
+ # if job.error?
46
+ # status = job.error
47
+ # else
48
+ # backup = job.backup
49
+ # end
50
+ #
51
+ class Backup
52
+ ##
53
+ # @private The gRPC Service object.
54
+ attr_accessor :service
55
+
56
+ ##
57
+ # @private Creates a new Backup instance.
58
+ def initialize grpc, service
59
+ @grpc = grpc
60
+ @service = service
61
+ end
62
+
63
+ ##
64
+ # The unique identifier for the project.
65
+ # @return [String]
66
+ def project_id
67
+ @grpc.name.split("/")[1]
68
+ end
69
+
70
+ ##
71
+ # The unique identifier for the instance.
72
+ # @return [String]
73
+ def instance_id
74
+ @grpc.name.split("/")[3]
75
+ end
76
+
77
+ ##
78
+ # The unique identifier for the backup.
79
+ # @return [String]
80
+ def backup_id
81
+ @grpc.name.split("/")[5]
82
+ end
83
+
84
+ ##
85
+ # Name of the database from which this backup was created.
86
+ # @return [String]
87
+ def database_id
88
+ @grpc.database.split("/")[5]
89
+ end
90
+
91
+ ##
92
+ # The full path for the backup. Values are of the form
93
+ # `projects/<project>/instances/<instance>/backups/<backup_id>`.
94
+ # @return [String]
95
+ def path
96
+ @grpc.name
97
+ end
98
+
99
+ ##
100
+ # The current backup state. Possible values are `:CREATING` and
101
+ # `:READY`.
102
+ # @return [Symbol]
103
+ def state
104
+ @grpc.state
105
+ end
106
+
107
+ ##
108
+ # The backup is still being created. A backup is not yet available
109
+ # for the database restore operation.
110
+ # @return [Boolean]
111
+ def creating?
112
+ state == :CREATING
113
+ end
114
+
115
+ ##
116
+ # The backup is created and can be used to restore a database.
117
+ # @return [Boolean]
118
+ def ready?
119
+ state == :READY
120
+ end
121
+
122
+ ##
123
+ # The expiration time of the backup, with microseconds granularity.
124
+ # @return [Time]
125
+ def expire_time
126
+ Convert.timestamp_to_time @grpc.expire_time
127
+ end
128
+
129
+ ##
130
+ # Update backup expiration time.
131
+ #
132
+ # Set expiration time of the backup, with microseconds granularity
133
+ # that must be at least 6 hours and at most 366 days from the time the
134
+ # request is received. Once the `expire_time` has passed, Cloud Spanner
135
+ # will delete the backup and free the resources used by the backup.
136
+ #
137
+ # @param [Time] time Backup expiration time.
138
+ # @raise [Google::Cloud::Error] if expire time is in past or update
139
+ # call is aborted.
140
+ #
141
+ # @example
142
+ # require "google/cloud/spanner"
143
+ #
144
+ # spanner = Google::Cloud::Spanner.new
145
+ #
146
+ # instance = spanner.instance "my-instance"
147
+ # backup = instance.backup "my-backup"
148
+ # backup.expire_time = Time.now + 36000
149
+ # puts backup.expire_time
150
+ #
151
+ def expire_time= time
152
+ ensure_service!
153
+
154
+ expire_time_was = @grpc.expire_time
155
+ @grpc.expire_time = Convert.time_to_timestamp time
156
+ update_mask = Google::Protobuf::FieldMask.new paths: ["expire_time"]
157
+ @grpc = service.update_backup @grpc, update_mask
158
+ rescue Google::Cloud::Error => error
159
+ @grpc.expire_time = expire_time_was
160
+ raise error
161
+ end
162
+
163
+ ##
164
+ # Create time is approximately the time when the backup request was
165
+ # received.
166
+ # @return [Time]
167
+ def create_time
168
+ Convert.timestamp_to_time @grpc.create_time
169
+ end
170
+
171
+ ##
172
+ # Size of the backup in bytes.
173
+ # @return [Integer]
174
+ def size_in_bytes
175
+ @grpc.size_bytes
176
+ end
177
+
178
+ ##
179
+ # The instances of the restored databases that reference the backup.
180
+ # Referencing databases may exist in different instances.
181
+ # The existence of any referencing database prevents the backup from
182
+ # being deleted. When a restored database from the backup enters the
183
+ # `READY` state, the reference to the backup is removed.
184
+ #
185
+ # @return [Array<Google::Cloud::Spanner::Database>] Returns list of
186
+ # referencing database instances.
187
+ #
188
+ # @example
189
+ # spanner = Google::Cloud::Spanner.new
190
+ #
191
+ # instance = spanner.instance "my-instance"
192
+ # backup = instance.backup "my-backup"
193
+ #
194
+ # backup.referencing_databases.each do |database|
195
+ # puts database.database_id
196
+ # end
197
+ #
198
+ def referencing_databases
199
+ ensure_service!
200
+
201
+ @grpc.referencing_databases.map do |referencing_database|
202
+ segments = referencing_database.split "/"
203
+ database_grpc = service.get_database segments[3], segments[5]
204
+ Database.from_grpc database_grpc, service
205
+ end
206
+ end
207
+
208
+ ##
209
+ # Permanently deletes the backup.
210
+ #
211
+ # @return [Boolean] Returns `true` if the backup was deleted.
212
+ #
213
+ # @example
214
+ # require "google/cloud/spanner"
215
+ #
216
+ # spanner = Google::Cloud::Spanner.new
217
+ #
218
+ # instance = spanner.instance "my-instance"
219
+ # backup = instance.backup "my-backup"
220
+ # backup.delete # true
221
+ #
222
+ def delete
223
+ ensure_service!
224
+ service.delete_backup instance_id, backup_id
225
+ true
226
+ end
227
+
228
+ ##
229
+ # Restores deleted database from the backup.
230
+ #
231
+ # @param [String] database_id The unique identifier for the database,
232
+ # which cannot be changed after the database is created. Values are of
233
+ # the form `[a-z][a-z0-9_\-]*[a-z0-9]` and must be between 2 and 30
234
+ # characters in length. Required.
235
+ # @param [String] instance_id The name of the instance in which to
236
+ # create the restored database. This instance must be in the same
237
+ # project and have the same instance configuration as the instance
238
+ # containing the source backup. Optional. Default value is same as a
239
+ # backup instance.
240
+ # @return [Database] Restored database.
241
+ #
242
+ # @example
243
+ # require "google/cloud/spanner"
244
+ #
245
+ # spanner = Google::Cloud::Spanner.new
246
+ #
247
+ # instance = spanner.instance "my-instance"
248
+ # backup = instance.backup "my-backup"
249
+ # job = backup.restore "my-restored-database"
250
+ #
251
+ # job.done? #=> false
252
+ # job.reload! # API call
253
+ # job.done? #=> true
254
+ #
255
+ # if job.error?
256
+ # status = job.error
257
+ # else
258
+ # database = job.database
259
+ # end
260
+ #
261
+ # @example Restore database in provided instance id
262
+ # require "google/cloud/spanner"
263
+ #
264
+ # spanner = Google::Cloud::Spanner.new
265
+ #
266
+ # instance = spanner.instance "my-instance"
267
+ # backup = instance.backup "my-backup"
268
+ # job = backup.restore(
269
+ # "my-restored-database",
270
+ # instance_id: "other-instance"
271
+ # )
272
+ #
273
+ # job.done? #=> false
274
+ # job.reload! # API call
275
+ # job.done? #=> true
276
+ #
277
+ # if job.error?
278
+ # status = job.error
279
+ # else
280
+ # database = job.database
281
+ # end
282
+ #
283
+ def restore database_id, instance_id: nil
284
+ ensure_service!
285
+
286
+ instance_id ||= self.instance_id
287
+
288
+ grpc = service.restore_database \
289
+ self.instance_id,
290
+ backup_id,
291
+ instance_id,
292
+ database_id
293
+ Restore::Job.from_grpc grpc, service
294
+ end
295
+
296
+ ##
297
+ # @private
298
+ # Creates a new Backup instance from a
299
+ # `Google::Cloud::Spanner::Admin::Database::V1::Backup`.
300
+ def self.from_grpc grpc, service
301
+ new grpc, service
302
+ end
303
+
304
+ protected
305
+
306
+ ##
307
+ # @private Raise an error unless an active connection to the service is
308
+ # available.
309
+ def ensure_service!
310
+ raise "Must have active connection to service" unless service
311
+ end
312
+ end
313
+ end
314
+ end
315
+ end