google-cloud-spanner 1.6.2 → 1.6.3
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.
- checksums.yaml +4 -4
- data/.yardopts +17 -0
- data/AUTHENTICATION.md +178 -0
- data/CHANGELOG.md +88 -0
- data/CODE_OF_CONDUCT.md +40 -0
- data/CONTRIBUTING.md +188 -0
- data/LICENSE +201 -0
- data/LOGGING.md +32 -0
- data/OVERVIEW.md +324 -0
- data/TROUBLESHOOTING.md +37 -0
- data/lib/google/cloud/spanner/version.rb +1 -1
- metadata +11 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b024e076f5a48a2b319a0670c319418abceaf4abcaa82c679079a386d53fb1f
|
4
|
+
data.tar.gz: 28a9f44dca3b6b0c412bb87297b06315007f0d9e01f732cfcc60cf37be79b166
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '088ce6f22bf8a3157d27fec39fe7b83ec72eab0fc4568bc7e219af47bb2b8bed0dacb91d64776350e53c2189a2ef3b3670d4bb294ed640cb78455c88012b66c4'
|
7
|
+
data.tar.gz: e9aff1c2b37de448de27c4ad4837c1c2e196a1c9319444a5c5071991a7a83ab0f8cef3def490bd5a6d828905668173c43e76480da33b3cd9412ecaa1baeba53d
|
data/.yardopts
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
--no-private
|
2
|
+
--title=Google Cloud Spanner
|
3
|
+
--exclude _pb\.rb$
|
4
|
+
--markup markdown
|
5
|
+
--markup-provider redcarpet
|
6
|
+
--main OVERVIEW.md
|
7
|
+
|
8
|
+
./lib/**/*.rb
|
9
|
+
-
|
10
|
+
OVERVIEW.md
|
11
|
+
AUTHENTICATION.md
|
12
|
+
LOGGING.md
|
13
|
+
CONTRIBUTING.md
|
14
|
+
TROUBLESHOOTING.md
|
15
|
+
CHANGELOG.md
|
16
|
+
CODE_OF_CONDUCT.md
|
17
|
+
LICENSE
|
data/AUTHENTICATION.md
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
# Authentication
|
2
|
+
|
3
|
+
In general, the google-cloud-spanner library uses [Service
|
4
|
+
Account](https://cloud.google.com/iam/docs/creating-managing-service-accounts)
|
5
|
+
credentials to connect to Google Cloud services. When running on Compute Engine
|
6
|
+
the credentials will be discovered automatically. When running on other
|
7
|
+
environments, the Service Account credentials can be specified by providing the
|
8
|
+
path to the [JSON
|
9
|
+
keyfile](https://cloud.google.com/iam/docs/managing-service-account-keys) for
|
10
|
+
the account (or the JSON itself) in environment variables. Additionally, Cloud
|
11
|
+
SDK credentials can also be discovered automatically, but this is only
|
12
|
+
recommended during development.
|
13
|
+
|
14
|
+
## Project and Credential Lookup
|
15
|
+
|
16
|
+
The google-cloud-spanner library aims to make authentication as simple as
|
17
|
+
possible, and provides several mechanisms to configure your system without
|
18
|
+
providing **Project ID** and **Service Account Credentials** directly in code.
|
19
|
+
|
20
|
+
**Project ID** is discovered in the following order:
|
21
|
+
|
22
|
+
1. Specify project ID in method arguments
|
23
|
+
2. Specify project ID in configuration
|
24
|
+
3. Discover project ID in environment variables
|
25
|
+
4. Discover GCE project ID
|
26
|
+
|
27
|
+
**Credentials** are discovered in the following order:
|
28
|
+
|
29
|
+
1. Specify credentials in method arguments
|
30
|
+
2. Specify credentials in configuration
|
31
|
+
3. Discover credentials path in environment variables
|
32
|
+
4. Discover credentials JSON in environment variables
|
33
|
+
5. Discover credentials file in the Cloud SDK's path
|
34
|
+
6. Discover GCE credentials
|
35
|
+
|
36
|
+
### Google Cloud Platform environments
|
37
|
+
|
38
|
+
While running on Google Cloud Platform environments such as Google Compute
|
39
|
+
Engine, Google App Engine and Google Kubernetes Engine, no extra work is needed.
|
40
|
+
The **Project ID** and **Credentials** and are discovered automatically. Code
|
41
|
+
should be written as if already authenticated. Just be sure when you [set up the
|
42
|
+
GCE instance][gce-how-to], you add the correct scopes for the APIs you want to
|
43
|
+
access. For example:
|
44
|
+
|
45
|
+
* **All APIs**
|
46
|
+
* `https://www.googleapis.com/auth/cloud-platform`
|
47
|
+
* `https://www.googleapis.com/auth/cloud-platform.read-only`
|
48
|
+
* **BigQuery**
|
49
|
+
* `https://www.googleapis.com/auth/bigquery`
|
50
|
+
* `https://www.googleapis.com/auth/bigquery.insertdata`
|
51
|
+
* **Compute Engine**
|
52
|
+
* `https://www.googleapis.com/auth/compute`
|
53
|
+
* **Datastore**
|
54
|
+
* `https://www.googleapis.com/auth/datastore`
|
55
|
+
* `https://www.googleapis.com/auth/userinfo.email`
|
56
|
+
* **DNS**
|
57
|
+
* `https://www.googleapis.com/auth/ndev.clouddns.readwrite`
|
58
|
+
* **Pub/Sub**
|
59
|
+
* `https://www.googleapis.com/auth/pubsub`
|
60
|
+
* **Storage**
|
61
|
+
* `https://www.googleapis.com/auth/devstorage.full_control`
|
62
|
+
* `https://www.googleapis.com/auth/devstorage.read_only`
|
63
|
+
* `https://www.googleapis.com/auth/devstorage.read_write`
|
64
|
+
|
65
|
+
### Environment Variables
|
66
|
+
|
67
|
+
The **Project ID** and **Credentials JSON** can be placed in environment
|
68
|
+
variables instead of declaring them directly in code. Each service has its own
|
69
|
+
environment variable, allowing for different service accounts to be used for
|
70
|
+
different services. (See the READMEs for the individual service gems for
|
71
|
+
details.) The path to the **Credentials JSON** file can be stored in the
|
72
|
+
environment variable, or the **Credentials JSON** itself can be stored for
|
73
|
+
environments such as Docker containers where writing files is difficult or not
|
74
|
+
encouraged.
|
75
|
+
|
76
|
+
The environment variables that Spanner checks for project ID are:
|
77
|
+
|
78
|
+
1. `SPANNER_PROJECT`
|
79
|
+
2. `GOOGLE_CLOUD_PROJECT`
|
80
|
+
|
81
|
+
The environment variables that Spanner checks for credentials are configured on {Google::Cloud::Spanner::V1::Credentials}:
|
82
|
+
|
83
|
+
1. `SPANNER_CREDENTIALS` - Path to JSON file, or JSON contents
|
84
|
+
2. `SPANNER_KEYFILE` - Path to JSON file, or JSON contents
|
85
|
+
3. `GOOGLE_CLOUD_CREDENTIALS` - Path to JSON file, or JSON contents
|
86
|
+
4. `GOOGLE_CLOUD_KEYFILE` - Path to JSON file, or JSON contents
|
87
|
+
5. `GOOGLE_APPLICATION_CREDENTIALS` - Path to JSON file
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
require "google/cloud/spanner"
|
91
|
+
|
92
|
+
ENV["SPANNER_PROJECT"] = "my-project-id"
|
93
|
+
ENV["SPANNER_CREDENTIALS"] = "path/to/keyfile.json"
|
94
|
+
|
95
|
+
spanner = Google::Cloud::Spanner.new
|
96
|
+
```
|
97
|
+
|
98
|
+
### Configuration
|
99
|
+
|
100
|
+
The **Project ID** and **Credentials JSON** can be configured instead of placing them in environment variables or providing them as arguments.
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
require "google/cloud/spanner"
|
104
|
+
|
105
|
+
Google::Cloud::Spanner.configure do |config|
|
106
|
+
config.project_id = "my-project-id"
|
107
|
+
config.credentials = "path/to/keyfile.json"
|
108
|
+
end
|
109
|
+
|
110
|
+
spanner = Google::Cloud::Spanner.new
|
111
|
+
```
|
112
|
+
|
113
|
+
### Cloud SDK
|
114
|
+
|
115
|
+
This option allows for an easy way to authenticate during development. If
|
116
|
+
credentials are not provided in code or in environment variables, then Cloud SDK
|
117
|
+
credentials are discovered.
|
118
|
+
|
119
|
+
To configure your system for this, simply:
|
120
|
+
|
121
|
+
1. [Download and install the Cloud SDK](https://cloud.google.com/sdk)
|
122
|
+
2. Authenticate using OAuth 2.0 `$ gcloud auth login`
|
123
|
+
3. Write code as if already authenticated.
|
124
|
+
|
125
|
+
**NOTE:** This is _not_ recommended for running in production. The Cloud SDK
|
126
|
+
*should* only be used during development.
|
127
|
+
|
128
|
+
[gce-how-to]: https://cloud.google.com/compute/docs/authentication#using
|
129
|
+
[dev-console]: https://console.cloud.google.com/project
|
130
|
+
|
131
|
+
[enable-apis]: https://raw.githubusercontent.com/GoogleCloudPlatform/gcloud-common/master/authentication/enable-apis.png
|
132
|
+
|
133
|
+
[create-new-service-account]: https://raw.githubusercontent.com/GoogleCloudPlatform/gcloud-common/master/authentication/create-new-service-account.png
|
134
|
+
[create-new-service-account-existing-keys]: https://raw.githubusercontent.com/GoogleCloudPlatform/gcloud-common/master/authentication/create-new-service-account-existing-keys.png
|
135
|
+
[reuse-service-account]: https://raw.githubusercontent.com/GoogleCloudPlatform/gcloud-common/master/authentication/reuse-service-account.png
|
136
|
+
|
137
|
+
## Creating a Service Account
|
138
|
+
|
139
|
+
Google Cloud requires a **Project ID** and **Service Account Credentials** to
|
140
|
+
connect to the APIs. You will use the **Project ID** and **JSON key file** to
|
141
|
+
connect to most services with google-cloud-spanner.
|
142
|
+
|
143
|
+
If you are not running this client on Google Compute Engine, you need a Google
|
144
|
+
Developers service account.
|
145
|
+
|
146
|
+
1. Visit the [Google Developers Console][dev-console].
|
147
|
+
1. Create a new project or click on an existing project.
|
148
|
+
1. Activate the slide-out navigation tray and select **API Manager**. From
|
149
|
+
here, you will enable the APIs that your application requires.
|
150
|
+
|
151
|
+
![Enable the APIs that your application requires][enable-apis]
|
152
|
+
|
153
|
+
*Note: You may need to enable billing in order to use these services.*
|
154
|
+
|
155
|
+
1. Select **Credentials** from the side navigation.
|
156
|
+
|
157
|
+
You should see a screen like one of the following.
|
158
|
+
|
159
|
+
![Create a new service account][create-new-service-account]
|
160
|
+
|
161
|
+
![Create a new service account With Existing Keys][create-new-service-account-existing-keys]
|
162
|
+
|
163
|
+
Find the "Add credentials" drop down and select "Service account" to be
|
164
|
+
guided through downloading a new JSON key file.
|
165
|
+
|
166
|
+
If you want to re-use an existing service account, you can easily generate a
|
167
|
+
new key file. Just select the account you wish to re-use, and click "Generate
|
168
|
+
new JSON key":
|
169
|
+
|
170
|
+
![Re-use an existing service account][reuse-service-account]
|
171
|
+
|
172
|
+
The key file you download will be used by this library to authenticate API
|
173
|
+
requests and should be stored in a secure location.
|
174
|
+
|
175
|
+
## Troubleshooting
|
176
|
+
|
177
|
+
If you're having trouble authenticating you can ask for help by following the
|
178
|
+
{file:TROUBLESHOOTING.md Troubleshooting Guide}.
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Release History
|
2
|
+
|
3
|
+
### 1.6.3 / 2018-09-12
|
4
|
+
|
5
|
+
* Add missing documentation files to package.
|
6
|
+
|
7
|
+
### 1.6.2 / 2018-09-10
|
8
|
+
|
9
|
+
* Update documentation.
|
10
|
+
|
11
|
+
### 1.6.1 / 2018-08-21
|
12
|
+
|
13
|
+
* Update documentation.
|
14
|
+
|
15
|
+
### 1.6.0 / 2018-06-28
|
16
|
+
|
17
|
+
* Add Session labels
|
18
|
+
* Add labels optional argument to Project#client and #batch_client.
|
19
|
+
* Add labels optional argument to Project#batch_client.
|
20
|
+
* Bug fix when an error is raised while returning results.
|
21
|
+
|
22
|
+
### 1.5.0 / 2018-06-12
|
23
|
+
|
24
|
+
* Support STRUCT values in query parameters.
|
25
|
+
* Add `Fields#struct` to create a `Data` object.
|
26
|
+
* Documentation updates.
|
27
|
+
|
28
|
+
### 1.4.0 / 2018-03-26
|
29
|
+
|
30
|
+
* Add support for commit_timestamp.
|
31
|
+
|
32
|
+
### 1.3.1 / 2018-02-27
|
33
|
+
|
34
|
+
* Add Batch Client
|
35
|
+
* Support partitioned reads and queries.
|
36
|
+
* Support Shared Configuration.
|
37
|
+
* Fix issue with IAM Policy not refreshing properly.
|
38
|
+
* Fix issue when using Time objects as keys.
|
39
|
+
|
40
|
+
### 1.2.0 / 2017-12-19
|
41
|
+
|
42
|
+
* Update Low Level API code
|
43
|
+
* Remove deprecated constructor arguments.
|
44
|
+
* Update documentation.
|
45
|
+
* Update google-gax dependency to 1.0.
|
46
|
+
|
47
|
+
### 1.1.1 / 2017-11-15
|
48
|
+
|
49
|
+
* Fix Admin Credentials (GAPIC) environment variable names.
|
50
|
+
|
51
|
+
### 1.1.0 / 2017-11-14
|
52
|
+
|
53
|
+
* Add `Google::Cloud::Spanner::Credentials` class.
|
54
|
+
* Rename constructor arguments to `project_id` and `credentials`.
|
55
|
+
(The previous arguments `project` and `keyfile` are still supported.)
|
56
|
+
* Document `Google::Auth::Credentials` as `credentials` value.
|
57
|
+
* Update generated low level GAPIC code.
|
58
|
+
* Updated `google-gax` (`grpc`, `google-protobuf`), `googleauth` dependencies.
|
59
|
+
|
60
|
+
### 1.0.0 / 2017-09-29
|
61
|
+
|
62
|
+
* Release 1.0
|
63
|
+
|
64
|
+
### 0.23.2 / 2017-09-12
|
65
|
+
|
66
|
+
* Update connection configuration.
|
67
|
+
|
68
|
+
### 0.23.1 / 2017-08-18
|
69
|
+
|
70
|
+
* Update connection configuration.
|
71
|
+
|
72
|
+
### 0.23.0 / 2017-07-27
|
73
|
+
|
74
|
+
* Add `Job#error` returning `Spanner::Status`.
|
75
|
+
|
76
|
+
### 0.22.0 / 2017-07-11
|
77
|
+
|
78
|
+
* Remove `Policy#deep_dup`.
|
79
|
+
* Add thread pool size to `Session` pool configuration.
|
80
|
+
* Add error handling for some GRPC errors.
|
81
|
+
* Do not allow nested snapshots or transactions.
|
82
|
+
* Update initialization to raise a better error if project ID is not specified.
|
83
|
+
* Update GAPIC configuration to exclude `UNAVAILABLE` errors from automatic retry.
|
84
|
+
* Update example code in the API documentation and guide.
|
85
|
+
|
86
|
+
### 0.21.0 / 2017-06-08
|
87
|
+
|
88
|
+
Initial implementation of the Google Cloud Spanner API Ruby client.
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct.
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or reject
|
24
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
25
|
+
not aligned to this Code of Conduct. By adopting this Code of Conduct, project
|
26
|
+
maintainers commit themselves to fairly and consistently applying these
|
27
|
+
principles to every aspect of managing this project. Project maintainers who do
|
28
|
+
not follow or enforce the Code of Conduct may be permanently removed from the
|
29
|
+
project team.
|
30
|
+
|
31
|
+
This code of conduct applies both within project spaces and in public spaces
|
32
|
+
when an individual is representing the project or its community.
|
33
|
+
|
34
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
35
|
+
reported by opening an issue or contacting one or more of the project
|
36
|
+
maintainers.
|
37
|
+
|
38
|
+
This Code of Conduct is adapted from the [Contributor
|
39
|
+
Covenant](http://contributor-covenant.org), version 1.2.0, available at
|
40
|
+
[http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
# Contributing to Google Cloud Spanner
|
2
|
+
|
3
|
+
1. **Sign one of the contributor license agreements below.**
|
4
|
+
2. Fork the repo, develop and test your code changes.
|
5
|
+
3. Send a pull request.
|
6
|
+
|
7
|
+
## Contributor License Agreements
|
8
|
+
|
9
|
+
Before we can accept your pull requests you'll need to sign a Contributor
|
10
|
+
License Agreement (CLA):
|
11
|
+
|
12
|
+
- **If you are an individual writing original source code** and **you own the
|
13
|
+
intellectual property**, then you'll need to sign an [individual
|
14
|
+
CLA](https://developers.google.com/open-source/cla/individual).
|
15
|
+
- **If you work for a company that wants to allow you to contribute your work**,
|
16
|
+
then you'll need to sign a [corporate
|
17
|
+
CLA](https://developers.google.com/open-source/cla/corporate).
|
18
|
+
|
19
|
+
You can sign these electronically (just scroll to the bottom). After that, we'll
|
20
|
+
be able to accept your pull requests.
|
21
|
+
|
22
|
+
## Setup
|
23
|
+
|
24
|
+
In order to use the google-cloud-spanner console and run the project's tests,
|
25
|
+
there is a small amount of setup:
|
26
|
+
|
27
|
+
1. Install Ruby. google-cloud-spanner requires Ruby 2.3+. You may choose to
|
28
|
+
manage your Ruby and gem installations with [RVM](https://rvm.io/),
|
29
|
+
[rbenv](https://github.com/rbenv/rbenv), or
|
30
|
+
[chruby](https://github.com/postmodern/chruby).
|
31
|
+
|
32
|
+
2. Install [Bundler](http://bundler.io/).
|
33
|
+
|
34
|
+
```sh
|
35
|
+
$ gem install bundler
|
36
|
+
```
|
37
|
+
|
38
|
+
3. Install the top-level project dependencies.
|
39
|
+
|
40
|
+
```sh
|
41
|
+
$ bundle install
|
42
|
+
```
|
43
|
+
|
44
|
+
4. Install the Spanner dependencies.
|
45
|
+
|
46
|
+
```sh
|
47
|
+
$ cd google-cloud-spanner/
|
48
|
+
$ bundle exec rake bundleupdate
|
49
|
+
```
|
50
|
+
|
51
|
+
## Console
|
52
|
+
|
53
|
+
In order to run code interactively, you can automatically load
|
54
|
+
google-cloud-spanner and its dependencies in IRB. This requires that your
|
55
|
+
developer environment has already been configured by following the steps
|
56
|
+
described in the {file:AUTHENTICATION.md Authentication Guide}. An IRB console
|
57
|
+
can be created with:
|
58
|
+
|
59
|
+
```sh
|
60
|
+
$ cd google-cloud-spanner/
|
61
|
+
$ bundle exec rake console
|
62
|
+
```
|
63
|
+
|
64
|
+
## Spanner Tests
|
65
|
+
|
66
|
+
Tests are very important part of google-cloud-spanner. All contributions
|
67
|
+
should include tests that ensure the contributed code behaves as expected.
|
68
|
+
|
69
|
+
To run the unit tests, documentation tests, and code style checks together for a
|
70
|
+
package:
|
71
|
+
|
72
|
+
``` sh
|
73
|
+
$ cd google-cloud-spanner/
|
74
|
+
$ bundle exec rake ci
|
75
|
+
```
|
76
|
+
|
77
|
+
To run the command above, plus all acceptance tests, use `rake ci:acceptance` or
|
78
|
+
its handy alias, `rake ci:a`.
|
79
|
+
|
80
|
+
### Spanner Unit Tests
|
81
|
+
|
82
|
+
|
83
|
+
The project uses the [minitest](https://github.com/seattlerb/minitest) library,
|
84
|
+
including [specs](https://github.com/seattlerb/minitest#specs),
|
85
|
+
[mocks](https://github.com/seattlerb/minitest#mocks) and
|
86
|
+
[minitest-autotest](https://github.com/seattlerb/minitest-autotest).
|
87
|
+
|
88
|
+
To run the Spanner unit tests:
|
89
|
+
|
90
|
+
``` sh
|
91
|
+
$ cd google-cloud-spanner/
|
92
|
+
$ bundle exec rake test
|
93
|
+
```
|
94
|
+
|
95
|
+
### Spanner Documentation Tests
|
96
|
+
|
97
|
+
The project tests the code examples in the gem's
|
98
|
+
[YARD](https://github.com/lsegal/yard)-based documentation.
|
99
|
+
|
100
|
+
The example testing functions in a way that is very similar to unit testing, and
|
101
|
+
in fact the library providing it,
|
102
|
+
[yard-doctest](https://github.com/p0deje/yard-doctest), is based on the
|
103
|
+
project's unit test library, [minitest](https://github.com/seattlerb/minitest).
|
104
|
+
|
105
|
+
To run the Spanner documentation tests:
|
106
|
+
|
107
|
+
``` sh
|
108
|
+
$ cd google-cloud-spanner/
|
109
|
+
$ bundle exec rake doctest
|
110
|
+
```
|
111
|
+
|
112
|
+
If you add, remove or modify documentation examples when working on a pull
|
113
|
+
request, you may need to update the setup for the tests. The stubs and mocks
|
114
|
+
required to run the tests are located in `support/doctest_helper.rb`. Please
|
115
|
+
note that much of the setup is matched by the title of the
|
116
|
+
[`@example`](http://www.rubydoc.info/gems/yard/file/docs/Tags.md#example) tag.
|
117
|
+
If you alter an example's title, you may encounter breaking tests.
|
118
|
+
|
119
|
+
### Spanner Acceptance Tests
|
120
|
+
|
121
|
+
The Spanner acceptance tests interact with the live service API. Follow the
|
122
|
+
instructions in the {file:AUTHENTICATION.md Authentication guide} for enabling
|
123
|
+
the Spanner API. Occasionally, some API features may not yet be generally
|
124
|
+
available, making it difficult for some contributors to successfully run the
|
125
|
+
entire acceptance test suite. However, please ensure that you do successfully
|
126
|
+
run acceptance tests for any code areas covered by your pull request.
|
127
|
+
|
128
|
+
To run the acceptance tests, first create and configure a project in the Google
|
129
|
+
Developers Console, as described in the {file:AUTHENTICATION.md Authentication
|
130
|
+
guide}. Be sure to download the JSON KEY file. Make note of the PROJECT_ID and
|
131
|
+
the KEYFILE location on your system.
|
132
|
+
|
133
|
+
Before you can run the Spanner acceptance tests, you must first create indexes
|
134
|
+
used in the tests.
|
135
|
+
|
136
|
+
#### Running the Spanner acceptance tests
|
137
|
+
|
138
|
+
To run the Spanner acceptance tests:
|
139
|
+
|
140
|
+
``` sh
|
141
|
+
$ cd google-cloud-spanner/
|
142
|
+
$ bundle exec rake acceptance[\\{my-project-id},\\{/path/to/keyfile.json}]
|
143
|
+
```
|
144
|
+
|
145
|
+
Or, if you prefer you can store the values in the `GCLOUD_TEST_PROJECT` and
|
146
|
+
`GCLOUD_TEST_KEYFILE` environment variables:
|
147
|
+
|
148
|
+
``` sh
|
149
|
+
$ cd google-cloud-spanner/
|
150
|
+
$ export GCLOUD_TEST_PROJECT=\\{my-project-id}
|
151
|
+
$ export GCLOUD_TEST_KEYFILE=\\{/path/to/keyfile.json}
|
152
|
+
$ bundle exec rake acceptance
|
153
|
+
```
|
154
|
+
|
155
|
+
If you want to use a different project and credentials for acceptance tests, you
|
156
|
+
can use the more specific `SPANNER_TEST_PROJECT` and `SPANNER_TEST_KEYFILE`
|
157
|
+
environment variables:
|
158
|
+
|
159
|
+
``` sh
|
160
|
+
$ cd google-cloud-spanner/
|
161
|
+
$ export SPANNER_TEST_PROJECT=\\{my-project-id}
|
162
|
+
$ export SPANNER_TEST_KEYFILE=\\{/path/to/keyfile.json}
|
163
|
+
$ bundle exec rake acceptance
|
164
|
+
```
|
165
|
+
|
166
|
+
## Coding Style
|
167
|
+
|
168
|
+
Please follow the established coding style in the library. The style is is
|
169
|
+
largely based on [The Ruby Style
|
170
|
+
Guide](https://github.com/bbatsov/ruby-style-guide) with a few exceptions based
|
171
|
+
on seattle-style:
|
172
|
+
|
173
|
+
* Avoid parenthesis when possible, including in method definitions.
|
174
|
+
* Always use double quotes strings. ([Option
|
175
|
+
B](https://github.com/bbatsov/ruby-style-guide#strings))
|
176
|
+
|
177
|
+
You can check your code against these rules by running Rubocop like so:
|
178
|
+
|
179
|
+
```sh
|
180
|
+
$ cd google-cloud-spanner/
|
181
|
+
$ bundle exec rake rubocop
|
182
|
+
```
|
183
|
+
|
184
|
+
## Code of Conduct
|
185
|
+
|
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
|
+
{file:CODE_OF_CONDUCT.md Code of Conduct} for more information.
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
https://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/LOGGING.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Enabling gRPC Logging
|
2
|
+
|
3
|
+
To enable logging for this library, set the logger for the underlying
|
4
|
+
[gRPC](https://github.com/grpc/grpc/tree/master/src/ruby) library. The logger
|
5
|
+
that you set may be a Ruby stdlib
|
6
|
+
[`Logger`](https://ruby-doc.org/stdlib-2.5.0/libdoc/logger/rdoc/Logger.html) as
|
7
|
+
shown below, or a
|
8
|
+
[`Google::Cloud::Logging::Logger`](https://googlecloudplatform.github.io/google-cloud-ruby/docs/google-cloud-logging/latest/Google/Cloud/Logging/Logger)
|
9
|
+
that will write logs to [Stackdriver
|
10
|
+
Logging](https://cloud.google.com/logging/). See
|
11
|
+
[grpc/logconfig.rb](https://github.com/grpc/grpc/blob/master/src/ruby/lib/grpc/logconfig.rb)
|
12
|
+
and the gRPC
|
13
|
+
[spec_helper.rb](https://github.com/grpc/grpc/blob/master/src/ruby/spec/spec_helper.rb)
|
14
|
+
for additional information.
|
15
|
+
|
16
|
+
Configuring a Ruby stdlib logger:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require "logger"
|
20
|
+
|
21
|
+
module MyLogger
|
22
|
+
LOGGER = Logger.new $stderr, level: Logger::WARN
|
23
|
+
def logger
|
24
|
+
LOGGER
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Define a gRPC module-level logger method before grpc/logconfig.rb loads.
|
29
|
+
module GRPC
|
30
|
+
extend MyLogger
|
31
|
+
end
|
32
|
+
```
|
data/OVERVIEW.md
ADDED
@@ -0,0 +1,324 @@
|
|
1
|
+
# Cloud Spanner
|
2
|
+
|
3
|
+
Cloud Spanner is a fully managed, mission-critical, relational database service
|
4
|
+
that offers transactional consistency at global scale, schemas, SQL (ANSI 2011
|
5
|
+
with extensions), and automatic, synchronous replication for high availability.
|
6
|
+
|
7
|
+
For more information about Cloud Spanner, read the [Cloud Spanner
|
8
|
+
Documentation](https://cloud.google.com/spanner/docs/).
|
9
|
+
|
10
|
+
The goal of google-cloud is to provide an API that is comfortable to Rubyists.
|
11
|
+
Your authentication credentials are detected automatically in Google Cloud
|
12
|
+
Platform environments such as Google Compute Engine, Google App Engine and
|
13
|
+
Google Kubernetes Engine. In other environments you can configure authentication
|
14
|
+
easily, either directly in your code or via environment variables. Read more
|
15
|
+
about the options for connecting in the {file:AUTHENTICATION.md Authentication
|
16
|
+
Guide}.
|
17
|
+
|
18
|
+
## Creating instances
|
19
|
+
|
20
|
+
When you first use Cloud Spanner, you must create an instance, which is an
|
21
|
+
allocation of resources that are used by Cloud Spanner databases. When you
|
22
|
+
create an instance, you choose where your data is stored and how many nodes are
|
23
|
+
used for your data. (For more information, see [Configuration
|
24
|
+
Guide](https://googlecloudplatform.github.io/google-cloud-ruby/docs/stackdriver/latest/file.INSTRUMENTATION_CONFIGURATION)).
|
25
|
+
|
26
|
+
Use {Google::Cloud::Spanner::Project#create_instance Project#create_instance} to
|
27
|
+
create an instance:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require "google/cloud/spanner"
|
31
|
+
|
32
|
+
spanner = Google::Cloud::Spanner.new
|
33
|
+
|
34
|
+
job = spanner.create_instance "my-instance",
|
35
|
+
name: "My Instance",
|
36
|
+
config: "regional-us-central1",
|
37
|
+
nodes: 5,
|
38
|
+
labels: { production: :env }
|
39
|
+
|
40
|
+
job.done? #=> false
|
41
|
+
job.reload! # API call
|
42
|
+
job.done? #=> true
|
43
|
+
|
44
|
+
if job.error?
|
45
|
+
status = job.error
|
46
|
+
else
|
47
|
+
instance = job.instance
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
## Creating databases
|
52
|
+
|
53
|
+
Now that you have created an instance, you can create a database. Cloud
|
54
|
+
Spanner databases hold the tables and indexes that allow you to read and
|
55
|
+
write data. You may create multiple databases in an instance.
|
56
|
+
|
57
|
+
Use {Google::Cloud::Spanner::Project#create_database Project#create_database}
|
58
|
+
(or {Google::Cloud::Spanner::Instance#create_database Instance#create_database})
|
59
|
+
to create a database:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
require "google/cloud/spanner"
|
63
|
+
|
64
|
+
spanner = Google::Cloud::Spanner.new
|
65
|
+
|
66
|
+
job = spanner.create_database "my-instance", "my-database"
|
67
|
+
|
68
|
+
job.done? #=> false
|
69
|
+
job.reload! # API call
|
70
|
+
job.done? #=> true
|
71
|
+
|
72
|
+
if job.error?
|
73
|
+
status = job.error
|
74
|
+
else
|
75
|
+
database = job.database
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
## Updating database schemas
|
80
|
+
|
81
|
+
Cloud Spanner supports schema updates to a database while the database
|
82
|
+
continues to serve traffic. Schema updates do not require taking the
|
83
|
+
database offline and they do not lock entire tables or columns; you can
|
84
|
+
continue writing data to the database during the schema update.
|
85
|
+
|
86
|
+
Use {Google::Cloud::Spanner::Database#update Database#update} to execute one or
|
87
|
+
more statements in Cloud Spanner's Data Definition Language (DDL):
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
require "google/cloud/spanner"
|
91
|
+
|
92
|
+
spanner = Google::Cloud::Spanner.new
|
93
|
+
|
94
|
+
database = spanner.database "my-instance", "my-database"
|
95
|
+
|
96
|
+
add_users_table_sql = %q(
|
97
|
+
CREATE TABLE users (
|
98
|
+
id INT64 NOT NULL,
|
99
|
+
username STRING(25) NOT NULL,
|
100
|
+
name STRING(45) NOT NULL,
|
101
|
+
email STRING(128),
|
102
|
+
) PRIMARY KEY(id)
|
103
|
+
)
|
104
|
+
|
105
|
+
database.update statements: [add_users_table_sql]
|
106
|
+
```
|
107
|
+
|
108
|
+
## Creating clients
|
109
|
+
|
110
|
+
In order to read and/or write data, you must create a database client. You can
|
111
|
+
think of a client as a database connection: All of your interactions with Cloud
|
112
|
+
Spanner data must go through a client. Typically you create a client when your
|
113
|
+
application starts up, then you re-use that client to read, write, and execute
|
114
|
+
transactions.
|
115
|
+
|
116
|
+
Use {Google::Cloud::Spanner::Project#client Project#client} to create a client:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
require "google/cloud/spanner"
|
120
|
+
|
121
|
+
spanner = Google::Cloud::Spanner.new
|
122
|
+
|
123
|
+
db = spanner.client "my-instance", "my-database"
|
124
|
+
|
125
|
+
results = db.execute "SELECT 1"
|
126
|
+
|
127
|
+
results.rows.each do |row|
|
128
|
+
puts row
|
129
|
+
end
|
130
|
+
```
|
131
|
+
|
132
|
+
## Writing data
|
133
|
+
|
134
|
+
You write data using your client object. The client object supports various
|
135
|
+
mutation operations, as well as combinations of inserts, updates, deletes, etc.,
|
136
|
+
that can be applied atomically to different rows and/or tables in a database.
|
137
|
+
|
138
|
+
Use {Google::Cloud::Spanner::Client#commit Client#commit} to execute various
|
139
|
+
mutations atomically at a single logical point in time. All changes are
|
140
|
+
accumulated in memory until the block completes. Unlike
|
141
|
+
{Google::Cloud::Spanner::Client#transaction Client#transaction}, which can also
|
142
|
+
perform reads, this operation accepts only mutations and makes a single API
|
143
|
+
request.
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
require "google/cloud/spanner"
|
147
|
+
|
148
|
+
spanner = Google::Cloud::Spanner.new
|
149
|
+
|
150
|
+
db = spanner.client "my-instance", "my-database"
|
151
|
+
|
152
|
+
db.commit do |c|
|
153
|
+
c.update "users", [{ id: 1, username: "charlie94", name: "Charlie" }]
|
154
|
+
c.insert "users", [{ id: 2, username: "harvey00", name: "Harvey" }]
|
155
|
+
end
|
156
|
+
```
|
157
|
+
|
158
|
+
## Querying data using SQL
|
159
|
+
|
160
|
+
Cloud Spanner supports a native SQL interface for reading data that is available
|
161
|
+
through {Google::Cloud::Spanner::Client#execute Client#execute}:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
require "google/cloud/spanner"
|
165
|
+
|
166
|
+
spanner = Google::Cloud::Spanner.new
|
167
|
+
|
168
|
+
db = spanner.client "my-instance", "my-database"
|
169
|
+
|
170
|
+
results = db.execute "SELECT * FROM users"
|
171
|
+
|
172
|
+
results.rows.each do |row|
|
173
|
+
puts "User #{row[:id]} is #{row[:name]}"
|
174
|
+
end
|
175
|
+
```
|
176
|
+
|
177
|
+
## Reading data using the read method
|
178
|
+
|
179
|
+
In addition to Cloud Spanner's SQL interface, Cloud Spanner also supports a read
|
180
|
+
interface. Use the {Google::Cloud::Spanner::Client#read Client#read} method to
|
181
|
+
read rows from the database, and use its `keys` option to pass unique
|
182
|
+
identifiers as both lists and ranges:
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
require "google/cloud/spanner"
|
186
|
+
|
187
|
+
spanner = Google::Cloud::Spanner.new
|
188
|
+
|
189
|
+
db = spanner.client "my-instance", "my-database"
|
190
|
+
|
191
|
+
results = db.read "users", [:id, :name], keys: 1..5
|
192
|
+
|
193
|
+
results.rows.each do |row|
|
194
|
+
puts "User #{row[:id]} is #{row[:name]}"
|
195
|
+
end
|
196
|
+
```
|
197
|
+
|
198
|
+
## Using read-write transactions
|
199
|
+
|
200
|
+
When an operation might write data depending on values it reads, you should use
|
201
|
+
a read-write transaction to perform the reads and writes atomically.
|
202
|
+
|
203
|
+
Suppose that sales of `Albums(1, 1)` are lower than expected and you want to
|
204
|
+
move $200,000 from the marketing budget of `Albums(2, 2)` to it, but only if the
|
205
|
+
budget of `Albums(2, 2)` is at least $300,000.
|
206
|
+
|
207
|
+
Use {Google::Cloud::Spanner::Client#transaction Client#transaction} to execute
|
208
|
+
both reads and writes atomically at a single logical point in time. All changes
|
209
|
+
are accumulated in memory until the block completes. Transactions will be
|
210
|
+
automatically retried when possible. This operation makes separate API requests
|
211
|
+
to begin and commit the transaction.
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
require "google/cloud/spanner"
|
215
|
+
|
216
|
+
spanner = Google::Cloud::Spanner.new
|
217
|
+
|
218
|
+
db = spanner.client "my-instance", "my-database"
|
219
|
+
|
220
|
+
db.transaction do |tx|
|
221
|
+
# Read the second album budget.
|
222
|
+
second_album_result = tx.read "Albums", ["marketing_budget"],
|
223
|
+
keys: [[2, 2]], limit: 1
|
224
|
+
second_album_row = second_album_result.rows.first
|
225
|
+
second_album_budget = second_album_row.values.first
|
226
|
+
|
227
|
+
transfer_amount = 200000
|
228
|
+
|
229
|
+
if second_album_budget < 300000
|
230
|
+
# Raising an exception will automatically roll back the transaction.
|
231
|
+
raise "The second album doesn't have enough funds to transfer"
|
232
|
+
end
|
233
|
+
|
234
|
+
# Read the first album's budget.
|
235
|
+
first_album_result = tx.read "Albums", ["marketing_budget"],
|
236
|
+
keys: [[1, 1]], limit: 1
|
237
|
+
first_album_row = first_album_result.rows.first
|
238
|
+
first_album_budget = first_album_row.values.first
|
239
|
+
|
240
|
+
# Update the budgets.
|
241
|
+
second_album_budget -= transfer_amount
|
242
|
+
first_album_budget += transfer_amount
|
243
|
+
puts "Setting first album's budget to #{first_album_budget} and the " \
|
244
|
+
"second album's budget to #{second_album_budget}."
|
245
|
+
|
246
|
+
# Update the rows.
|
247
|
+
rows = [
|
248
|
+
{singer_id: 1, album_id: 1, marketing_budget: first_album_budget},
|
249
|
+
{singer_id: 2, album_id: 2, marketing_budget: second_album_budget}
|
250
|
+
]
|
251
|
+
tx.update "Albums", rows
|
252
|
+
end
|
253
|
+
```
|
254
|
+
|
255
|
+
## Using read-only transactions
|
256
|
+
|
257
|
+
Suppose you want to execute more than one read at the same timestamp. Read-only
|
258
|
+
transactions observe a consistent prefix of the transaction commit history, so
|
259
|
+
your application always gets consistent data. Because read-only transactions are
|
260
|
+
much faster than locking read-write transactions, we strongly recommend that you
|
261
|
+
do all of your transaction reads in read-only transactions if possible.
|
262
|
+
|
263
|
+
Use a {Google::Cloud::Spanner::Snapshot Snapshot} object to execute statements
|
264
|
+
in a read-only transaction. The snapshot object is available via a block
|
265
|
+
provided to {Google::Cloud::Spanner::Client#snapshot Client#snapshot}:
|
266
|
+
|
267
|
+
```ruby
|
268
|
+
require "google/cloud/spanner"
|
269
|
+
|
270
|
+
spanner = Google::Cloud::Spanner.new
|
271
|
+
|
272
|
+
db = spanner.client "my-instance", "my-database"
|
273
|
+
|
274
|
+
db.snapshot do |snp|
|
275
|
+
results_1 = snp.execute "SELECT * FROM users"
|
276
|
+
results_1.rows.each do |row|
|
277
|
+
puts "User #{row[:id]} is #{row[:name]}"
|
278
|
+
end
|
279
|
+
|
280
|
+
# Perform another read using the `read` method. Even if the data
|
281
|
+
# is updated in-between the reads, the snapshot ensures that both
|
282
|
+
# return the same data.
|
283
|
+
results_2 = db.read "users", [:id, :name]
|
284
|
+
results_2.rows.each do |row|
|
285
|
+
puts "User #{row[:id]} is #{row[:name]}"
|
286
|
+
end
|
287
|
+
end
|
288
|
+
```
|
289
|
+
|
290
|
+
## Deleting databases
|
291
|
+
|
292
|
+
Use {Google::Cloud::Spanner::Database#drop Database#drop} to delete a database:
|
293
|
+
|
294
|
+
```ruby
|
295
|
+
require "google/cloud/spanner"
|
296
|
+
|
297
|
+
spanner = Google::Cloud::Spanner.new
|
298
|
+
|
299
|
+
database = spanner.database "my-instance", "my-database"
|
300
|
+
|
301
|
+
database.drop
|
302
|
+
```
|
303
|
+
|
304
|
+
## Deleting instances
|
305
|
+
|
306
|
+
When you delete an instance, all databases within it are automatically deleted.
|
307
|
+
(If you only delete databases and not your instance, you will still incur
|
308
|
+
charges for the instance.) Use {Google::Cloud::Spanner::Instance#delete
|
309
|
+
Instance#delete} to delete an instance:
|
310
|
+
|
311
|
+
```ruby
|
312
|
+
require "google/cloud/spanner"
|
313
|
+
|
314
|
+
spanner = Google::Cloud::Spanner.new
|
315
|
+
|
316
|
+
instance = spanner.instance "my-instance"
|
317
|
+
|
318
|
+
instance.delete
|
319
|
+
````
|
320
|
+
|
321
|
+
## Additional information
|
322
|
+
|
323
|
+
Cloud Spanner can be configured to use gRPC's logging. To learn more, see the
|
324
|
+
{file:LOGGING.md Logging guide}.
|
data/TROUBLESHOOTING.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Troubleshooting
|
2
|
+
|
3
|
+
## Where can I get more help?
|
4
|
+
|
5
|
+
### Ask the Community
|
6
|
+
|
7
|
+
If you have a question about how to use a Google Cloud client library in your
|
8
|
+
project or are stuck in the Developer's console and don't know where to turn,
|
9
|
+
it's possible your questions have already been addressed by the community.
|
10
|
+
|
11
|
+
First, check out the appropriate tags on StackOverflow:
|
12
|
+
- [`google-cloud-platform+ruby+spanner`][so-ruby]
|
13
|
+
|
14
|
+
Next, try searching through the issues on GitHub:
|
15
|
+
|
16
|
+
- [`api:spanner` issues][gh-search-ruby]
|
17
|
+
|
18
|
+
Still nothing?
|
19
|
+
|
20
|
+
### Ask the Developers
|
21
|
+
|
22
|
+
If you're experiencing a bug with the code, or have an idea for how it can be
|
23
|
+
improved, *please* create a new issue on GitHub so we can talk about it.
|
24
|
+
|
25
|
+
- [New issue][gh-ruby]
|
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
|
+
[so-ruby]: http://stackoverflow.com/questions/tagged/google-cloud-platform+ruby+spanner
|
32
|
+
|
33
|
+
[gh-search-ruby]: https://github.com/googlecloudplatform/google-cloud-ruby/issues?q=label%3A%22api%3A+spanner%22
|
34
|
+
|
35
|
+
[gh-ruby]: https://github.com/googlecloudplatform/google-cloud-ruby/issues/new
|
36
|
+
|
37
|
+
[slack-ruby]: https://gcp-slack.appspot.com/
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-cloud-spanner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.6.
|
4
|
+
version: 1.6.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Moore
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-09-
|
12
|
+
date: 2018-09-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: google-cloud-core
|
@@ -216,6 +216,15 @@ executables: []
|
|
216
216
|
extensions: []
|
217
217
|
extra_rdoc_files: []
|
218
218
|
files:
|
219
|
+
- ".yardopts"
|
220
|
+
- AUTHENTICATION.md
|
221
|
+
- CHANGELOG.md
|
222
|
+
- CODE_OF_CONDUCT.md
|
223
|
+
- CONTRIBUTING.md
|
224
|
+
- LICENSE
|
225
|
+
- LOGGING.md
|
226
|
+
- OVERVIEW.md
|
227
|
+
- TROUBLESHOOTING.md
|
219
228
|
- lib/google-cloud-spanner.rb
|
220
229
|
- lib/google/cloud/spanner.rb
|
221
230
|
- lib/google/cloud/spanner/admin/database.rb
|