spanner_emulator_toolkit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 301422fd77428e32cd400aedd54592589af35979bdf101a31e93a8f95980a8e5
4
+ data.tar.gz: 5a7ef1dda527e64a78b7187164beea42aa95692ffb3d2ac78ca2521c4c2fe5c6
5
+ SHA512:
6
+ metadata.gz: efbd3eb4f4daaaf31e97970f84e4588b1afab132ac19f543ae2d5e322e7c2605a0509b1ab6798f537c015c5b9b5e8950d2f30c6a57a77cc20130196c8ab512e6
7
+ data.tar.gz: b4f4fc0b7a46f33ec747cfdbfbe6111db169b562ef28a65a1b9d19abfee0767570b2cdaa18e285cd11d4100b244d3dcb234e004aebcd6463bf41c94dbee4b8e8
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ require:
2
+ - standard
3
+ - standard-custom
4
+ - standard-performance
5
+ - rubocop-performance
6
+
7
+ inherit_gem:
8
+ standard: config/base.yml
9
+ standard-custom: config/base.yml
10
+ standard-performance: config/base.yml
11
+
12
+ AllCops:
13
+ SuggestExtensions: false
14
+ TargetRubyVersion: 3.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## [0.1.0]
2
+
3
+ Initial release.
4
+
5
+ - Global configuration with `SpannerEmulatorToolkit.configure { |config| ... }` or ENV vars
6
+ - Google::Cloud::Spanner client methods: `create_instance`, `create_database`, `drop_instance`, `drop_database`
7
+ - `reset_session!` to reset transactions all instances and databases of the configured project and emulator
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at abachman@recurly.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in spanner_emulator_toolkit.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "standard", "~> 1.28", require: false
11
+ gem "rbs", "~> 3.3"
12
+ gem "debug", "~> 1.8"
data/Gemfile.lock ADDED
@@ -0,0 +1,175 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ spanner_emulator_toolkit (0.1.0)
5
+ concurrent-ruby (~> 1.2)
6
+ google-cloud-spanner (~> 2.10)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ abbrev (0.1.1)
12
+ addressable (2.8.5)
13
+ public_suffix (>= 2.0.2, < 6.0)
14
+ ast (2.4.2)
15
+ base64 (0.2.0)
16
+ concurrent-ruby (1.2.2)
17
+ debug (1.8.0)
18
+ irb (>= 1.5.0)
19
+ reline (>= 0.3.1)
20
+ diff-lcs (1.5.0)
21
+ faraday (2.7.12)
22
+ base64
23
+ faraday-net_http (>= 2.0, < 3.1)
24
+ ruby2_keywords (>= 0.0.4)
25
+ faraday-net_http (3.0.2)
26
+ faraday-retry (2.2.0)
27
+ faraday (~> 2.0)
28
+ gapic-common (0.20.0)
29
+ faraday (>= 1.9, < 3.a)
30
+ faraday-retry (>= 1.0, < 3.a)
31
+ google-protobuf (~> 3.14)
32
+ googleapis-common-protos (>= 1.3.12, < 2.a)
33
+ googleapis-common-protos-types (>= 1.3.1, < 2.a)
34
+ googleauth (~> 1.0)
35
+ grpc (~> 1.36)
36
+ google-cloud-core (1.6.0)
37
+ google-cloud-env (~> 1.0)
38
+ google-cloud-errors (~> 1.0)
39
+ google-cloud-env (1.6.0)
40
+ faraday (>= 0.17.3, < 3.0)
41
+ google-cloud-errors (1.3.1)
42
+ google-cloud-spanner (2.18.1)
43
+ concurrent-ruby (~> 1.0)
44
+ google-cloud-core (~> 1.5)
45
+ google-cloud-spanner-admin-database-v1 (~> 0.1)
46
+ google-cloud-spanner-admin-instance-v1 (~> 0.1)
47
+ google-cloud-spanner-v1 (~> 0.2)
48
+ google-cloud-spanner-admin-database-v1 (0.15.0)
49
+ gapic-common (>= 0.20.0, < 2.a)
50
+ google-cloud-errors (~> 1.0)
51
+ grpc-google-iam-v1 (~> 1.1)
52
+ google-cloud-spanner-admin-instance-v1 (0.12.0)
53
+ gapic-common (>= 0.20.0, < 2.a)
54
+ google-cloud-errors (~> 1.0)
55
+ grpc-google-iam-v1 (~> 1.1)
56
+ google-cloud-spanner-v1 (0.19.0)
57
+ gapic-common (>= 0.20.0, < 2.a)
58
+ google-cloud-errors (~> 1.0)
59
+ google-protobuf (3.25.1-arm64-darwin)
60
+ google-protobuf (3.25.1-x86_64-linux)
61
+ googleapis-common-protos (1.4.0)
62
+ google-protobuf (~> 3.14)
63
+ googleapis-common-protos-types (~> 1.2)
64
+ grpc (~> 1.27)
65
+ googleapis-common-protos-types (1.11.0)
66
+ google-protobuf (~> 3.18)
67
+ googleauth (1.8.1)
68
+ faraday (>= 0.17.3, < 3.a)
69
+ jwt (>= 1.4, < 3.0)
70
+ multi_json (~> 1.11)
71
+ os (>= 0.9, < 2.0)
72
+ signet (>= 0.16, < 2.a)
73
+ grpc (1.60.0-arm64-darwin)
74
+ google-protobuf (~> 3.25)
75
+ googleapis-common-protos-types (~> 1.0)
76
+ grpc (1.60.0-x86_64-linux)
77
+ google-protobuf (~> 3.25)
78
+ googleapis-common-protos-types (~> 1.0)
79
+ grpc-google-iam-v1 (1.4.0)
80
+ google-protobuf (~> 3.18)
81
+ googleapis-common-protos (~> 1.4)
82
+ grpc (~> 1.41)
83
+ io-console (0.6.0)
84
+ irb (1.10.1)
85
+ rdoc
86
+ reline (>= 0.3.8)
87
+ json (2.7.1)
88
+ jwt (2.7.1)
89
+ language_server-protocol (3.17.0.3)
90
+ lint_roller (1.1.0)
91
+ multi_json (1.15.0)
92
+ os (1.1.4)
93
+ parallel (1.23.0)
94
+ parser (3.2.2.4)
95
+ ast (~> 2.4.1)
96
+ racc
97
+ psych (5.1.1.1)
98
+ stringio
99
+ public_suffix (5.0.4)
100
+ racc (1.7.3)
101
+ rainbow (3.1.1)
102
+ rake (13.1.0)
103
+ rbs (3.3.2)
104
+ abbrev
105
+ rdoc (6.6.1)
106
+ psych (>= 4.0.0)
107
+ regexp_parser (2.8.3)
108
+ reline (0.4.1)
109
+ io-console (~> 0.5)
110
+ rexml (3.2.6)
111
+ rspec (3.12.0)
112
+ rspec-core (~> 3.12.0)
113
+ rspec-expectations (~> 3.12.0)
114
+ rspec-mocks (~> 3.12.0)
115
+ rspec-core (3.12.2)
116
+ rspec-support (~> 3.12.0)
117
+ rspec-expectations (3.12.3)
118
+ diff-lcs (>= 1.2.0, < 2.0)
119
+ rspec-support (~> 3.12.0)
120
+ rspec-mocks (3.12.6)
121
+ diff-lcs (>= 1.2.0, < 2.0)
122
+ rspec-support (~> 3.12.0)
123
+ rspec-support (3.12.1)
124
+ rubocop (1.57.2)
125
+ json (~> 2.3)
126
+ language_server-protocol (>= 3.17.0)
127
+ parallel (~> 1.10)
128
+ parser (>= 3.2.2.4)
129
+ rainbow (>= 2.2.2, < 4.0)
130
+ regexp_parser (>= 1.8, < 3.0)
131
+ rexml (>= 3.2.5, < 4.0)
132
+ rubocop-ast (>= 1.28.1, < 2.0)
133
+ ruby-progressbar (~> 1.7)
134
+ unicode-display_width (>= 2.4.0, < 3.0)
135
+ rubocop-ast (1.30.0)
136
+ parser (>= 3.2.1.0)
137
+ rubocop-performance (1.19.1)
138
+ rubocop (>= 1.7.0, < 2.0)
139
+ rubocop-ast (>= 0.4.0)
140
+ ruby-progressbar (1.13.0)
141
+ ruby2_keywords (0.0.5)
142
+ signet (0.18.0)
143
+ addressable (~> 2.8)
144
+ faraday (>= 0.17.5, < 3.a)
145
+ jwt (>= 1.5, < 3.0)
146
+ multi_json (~> 1.10)
147
+ standard (1.32.1)
148
+ language_server-protocol (~> 3.17.0.2)
149
+ lint_roller (~> 1.0)
150
+ rubocop (~> 1.57.2)
151
+ standard-custom (~> 1.0.0)
152
+ standard-performance (~> 1.2)
153
+ standard-custom (1.0.2)
154
+ lint_roller (~> 1.0)
155
+ rubocop (~> 1.50)
156
+ standard-performance (1.2.1)
157
+ lint_roller (~> 1.1)
158
+ rubocop-performance (~> 1.19.1)
159
+ stringio (3.1.0)
160
+ unicode-display_width (2.5.0)
161
+
162
+ PLATFORMS
163
+ arm64-darwin-21
164
+ x86_64-linux
165
+
166
+ DEPENDENCIES
167
+ debug (~> 1.8)
168
+ rake (~> 13.0)
169
+ rbs (~> 3.3)
170
+ rspec (~> 3.0)
171
+ spanner_emulator_toolkit!
172
+ standard (~> 1.28)
173
+
174
+ BUNDLED WITH
175
+ 2.4.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Adam Bachman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,203 @@
1
+ # SpannerEmulatorToolkit
2
+
3
+ Peform some simple or otherwise impossible tasks with [the Cloud Spanner Emulator][emulator], with specific focus on resetting all session transactions for a given Emulator.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ ```console
10
+ $ bundle add spanner_emulator_toolkit
11
+ ```
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ ```console
16
+ $ gem install spanner_emulator_toolkit
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Configuration
22
+
23
+ All functionality require configuration first. All required settings have reasonably safe defaults and can be set through environment variables.
24
+
25
+ | Option | Description | Default | Required | Env |
26
+ | --- | --- | --- | --- | --- |
27
+ | `project_id` | The project ID to use. | `"example-project"` | **Yes** | `SPANNER_PROJECT_ID` |
28
+ | `instance_id` | The instance ID to use. | `"example-instance"` | no | `SPANNER_PROJECT_ID` |
29
+ | `project_id` | The database ID to use. | `"example-database"` | no | `SPANNER_DATABASE_ID` |
30
+ | `emulator_host` | Local hostname for the [cloud-spanner-emulator][emulator]. | `"localhost:9010"` | **Yes** | `SPANNER_EMULATOR_HOST` |
31
+ | `logger` | A Ruby Logger instance. | `Logger.new(STDOUT)` | no | |
32
+ | `log_level` | The log level to use for the logger. | `Logger::FATAL` | no | |
33
+ | `schema` | A raw SQL schema to use when creating the database. | `nil` | no | |
34
+
35
+ ```ruby
36
+ # if all you're doing is resetting sessions
37
+ SpannEmulatorToolkit.configure do |config|
38
+ config.project_id = "test-project"
39
+ config.emulator_host = "localhost:9010"
40
+ end
41
+
42
+ # if you're creating a specific instance and database
43
+ SpannEmulatorToolkit.configure do |config|
44
+ config.project_id = "test-project"
45
+ config.instance_id = "test-instance"
46
+ config.database_id = "test-database"
47
+ config.emulator_host = "localhost:9010"
48
+ end
49
+
50
+ # If you want to use an existing logger. Nothing too interesting is produced by
51
+ # the library, just debug logging.
52
+ SpannEmulatorToolkit.configure do |config|
53
+ config.logger = Rails.logger
54
+ end
55
+
56
+ # if you want to see debug logs on STDOUT
57
+ SpannEmulatorToolkit.configure do |config|
58
+ config.log_level = Logger::DEBUG
59
+ end
60
+
61
+ # to create a database with a schema
62
+ SpannEmulatorToolkit.configure do |config|
63
+ config.schema = <<~SQL
64
+ CREATE TABLE users (
65
+ id INT64 NOT NULL,
66
+ username STRING(255) NOT NULL,
67
+ name STRING(255) NOT NULL
68
+ ) PRIMARY KEY (id)
69
+ SQL
70
+ end
71
+ ```
72
+
73
+ ### Client methods
74
+
75
+ #### Instance and Database Management
76
+
77
+ Helper methods to work with Spanner instances and databases in the emulator.
78
+
79
+ Creating:
80
+
81
+ ```ruby
82
+ # create the configured instance
83
+ SpannerEmulatorToolkit.create_instance
84
+
85
+ # create the configured instance and database
86
+ SpannerEmulatorToolkit.create_database
87
+ ```
88
+
89
+ And dropping:
90
+
91
+ ```ruby
92
+ # drop the configured instance and all its databases
93
+ SpannerEmulatorToolkit.drop_instance
94
+
95
+ # just drop the configured database
96
+ SpannerEmulatorToolkit.drop_database
97
+ ```
98
+
99
+ #### Getting a Google::Cloud::Spanner::Client
100
+
101
+ The toolkit provides a simple wrapper around Spanner client initialization. It is not intended to be a complete replacement for the client, just a wrapper around some common patterns in prototyping.
102
+
103
+ [Google's documentation](https://cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/index.html) for the Spanner client is a good place to start.
104
+
105
+ ```ruby
106
+ # Google::Cloud::Spanner::Client
107
+ client = SpannerEmulatorToolkit.client
108
+
109
+ client.commit do |c|
110
+ c.update "users", [{ id: 1, username: "charlie94", name: "Charlie" }]
111
+ c.insert "users", [{ id: 2, username: "harvey00", name: "Harvey" }]
112
+ end
113
+
114
+ results = client.read "users", [:id, :name], keys: 1..5
115
+ results.rows.each do |row|
116
+ puts "User #{row[:id]} is #{row[:name]}"
117
+ end
118
+ ```
119
+
120
+ Or more specific classes from the [google-cloud-spanner](https://github.com/googleapis/ruby-spanner/) gem:
121
+
122
+ ```ruby
123
+ # Google::Cloud::Spanner::Project
124
+ SpannerEmulatorToolkit.project
125
+
126
+ # Google::Cloud::Spanner::Database
127
+ SpannerEmulatorToolkit.database
128
+ SpannerEmulatorToolkit.database_exists?
129
+ SpannerEmulatorToolkit.database_path # the full "projects/.../instances/.../databases/..." path
130
+
131
+ # Google::Cloud::Spanner::Instance
132
+ SpannerEmulatorToolkit.instance
133
+ SpannerEmulatorToolkit.instance_exists?
134
+ ```
135
+
136
+ ### Reset all session transactions
137
+
138
+ Why this gem exists in the first place: to reset all sessions on all databases in all instances for the configured project in the emulator.
139
+
140
+ ```ruby
141
+ SpannerEmulatorToolkit.reset_sessions!
142
+ ```
143
+
144
+ **Background**: [GoogleCloudPlatform/cloud-spanner-emulator#137](https://github.com/GoogleCloudPlatform/cloud-spanner-emulator/issues/137)
145
+
146
+ The Spanner Local Emulator has a known failure mode in which transactions left open by a process that has crashed are not cleaned up. This will cause the emulator to reject all future transactions.
147
+
148
+ This can be fixed by restarting the emulator, but that drops all your databases and data which can be a huge pain in a large project.
149
+
150
+ This tool will reset all open transactions in the emulator so you can continue working.
151
+
152
+ Useful for development and any environment where you're killing processes (like test suites) which may have a pending transaction open when they are killed.
153
+
154
+ ## Development
155
+
156
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
157
+
158
+ Run `rake rubocop` to run the linter before committing changes.
159
+
160
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
161
+
162
+ ### Running the examples
163
+
164
+ Before running an example, you'll need to [start the emulator][emulator]. The easiest way to do that is [with the pre-built Docker image](https://github.com/GoogleCloudPlatform/cloud-spanner-emulator#via-pre-built-docker-image).
165
+
166
+ ```console
167
+ $ docker pull gcr.io/cloud-spanner-emulator/emulator
168
+ $ docker run -p 9010:9010 -p 9020:9020 gcr.io/cloud-spanner-emulator/emulator
169
+ ```
170
+
171
+ Or through docker compose:
172
+
173
+ ```yaml
174
+ services:
175
+ google-cloud-spanner:
176
+ image: gcr.io/cloud-spanner-emulator/emulator:latest
177
+ ports:
178
+ - "9010:9010"
179
+ - "9020:9020"
180
+
181
+ # docker compose up google-cloud-spanner
182
+ ```
183
+
184
+ There are some examples in the `examples` directory. These are not run as part of the test suite, but can be run manually.
185
+
186
+ ```console
187
+ $ bundle exec ruby examples/create_instance.rb
188
+ ```
189
+
190
+ ## Contributing
191
+
192
+ Bug reports and pull requests are welcome on GitHub at https://github.com/abachman/spanner_emulator_toolkit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/abachman/spanner_emulator_toolkit/blob/main/CODE_OF_CONDUCT.md).
193
+
194
+ ## License
195
+
196
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
197
+
198
+ ## Code of Conduct
199
+
200
+ Everyone interacting in the SpannerEmulatorToolkit project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/abachman/spanner_emulator_toolkit/blob/main/CODE_OF_CONDUCT.md).
201
+
202
+
203
+ [emulator]: https://github.com/GoogleCloudPlatform/cloud-spanner-emulator
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,8 @@
1
+ version: '3'
2
+
3
+ services:
4
+ google-cloud-spanner:
5
+ image: gcr.io/cloud-spanner-emulator/emulator:latest
6
+ ports:
7
+ - "9010:9010"
8
+ - "9020:9020"
@@ -0,0 +1,10 @@
1
+ $: << File.expand_path("../lib", __FILE__)
2
+
3
+ require "spanner_emulator_toolkit"
4
+
5
+ SpannerEmulatorToolkit.configure do |config|
6
+ config.schema = File.read File.expand_path("../spec/schema.sql", __dir__)
7
+ config.log_level = Logger::DEBUG
8
+ end
9
+
10
+ SpannerEmulatorToolkit.create_database
data/examples/drop.rb ADDED
@@ -0,0 +1,9 @@
1
+ $: << File.expand_path("../lib", __FILE__)
2
+
3
+ require "spanner_emulator_toolkit"
4
+
5
+ SpannerEmulatorToolkit.configure do |config|
6
+ config.log_level = Logger::DEBUG
7
+ end
8
+
9
+ SpannerEmulatorToolkit.drop_instance
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Force a stale-open transaction on the Spanner Emulator by running this script
5
+ # and then killing it with Ctrl-C.
6
+ #
7
+ # Rerunning the script will result in a hung process followed by the "one
8
+ # transaction at a time" error.
9
+ #
10
+ ##
11
+ require "google/cloud/spanner"
12
+
13
+ PROJECT_ID = "example-project"
14
+ INSTANCE_ID = "example-instance"
15
+ DATABASE_ID = "example-database"
16
+ EMULATOR_HOST = "localhost:9010"
17
+
18
+ project = Google::Cloud::Spanner.new(
19
+ project_id: PROJECT_ID,
20
+ emulator_host: EMULATOR_HOST
21
+ )
22
+ unless project.instance(INSTANCE_ID)
23
+ project.create_instance(INSTANCE_ID, name: INSTANCE_ID, nodes: 1).wait_until_done!
24
+ end
25
+ unless project.instance(INSTANCE_ID).database(DATABASE_ID)
26
+ schema = "CREATE TABLE Customers (Id STRING(36) NOT NULL) PRIMARY KEY (Id)"
27
+ project.instance(INSTANCE_ID).create_database(DATABASE_ID, statements: [schema]).wait_until_done!
28
+ end
29
+ client = project.client(INSTANCE_ID, DATABASE_ID)
30
+ puts
31
+ puts "| starting transactions on #{client.database.path}"
32
+ puts "| press \e[1mCtrl-C\e[0m to interrupt, rerun until you get a 'one transaction at a time' error"
33
+ puts "| then run examples/session_reset.rb to clear the hung transaction"
34
+ puts
35
+ loop do
36
+ client.transaction(deadline: 5) do |tx|
37
+ tx.execute "INSERT INTO Customers (Id) VALUES ('#{SecureRandom.hex(6)}')"
38
+ sleep 0.5
39
+ print "."
40
+ tx.execute "INSERT INTO Customers (Id) VALUES ('#{SecureRandom.hex(6)}-2')"
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ $: << File.expand_path("../lib", __FILE__)
2
+
3
+ require "spanner_emulator_toolkit"
4
+
5
+ SpannerEmulatorToolkit.configure do |config|
6
+ config.log_level = Logger::DEBUG
7
+ end
8
+
9
+ SpannerEmulatorToolkit.reset_sessions!
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "google/cloud/spanner"
4
+ require "concurrent"
5
+
6
+ module SpannerEmulatorToolkit
7
+ class << self
8
+ # raises error unless database already exists
9
+ def client
10
+ configuration.validate!
11
+
12
+ thread_local_client
13
+ end
14
+
15
+ # Resets the client instance.
16
+ def reset_client
17
+ @client = nil
18
+ self
19
+ end
20
+
21
+ def project
22
+ configuration.validate!
23
+
24
+ thread_local_project
25
+ end
26
+
27
+ # returns nil unless instance already exists
28
+ def instance
29
+ project.instance(configuration.instance_id)
30
+ end
31
+
32
+ def database
33
+ instance&.database(configuration.database_id)
34
+ end
35
+
36
+ def instance_exists?
37
+ !instance.nil?
38
+ end
39
+
40
+ def database_exists?
41
+ !instance&.database(configuration.database_id).nil?
42
+ end
43
+
44
+ # no-op if instance already exists
45
+ def create_instance
46
+ return if instance_exists?
47
+
48
+ logger.debug "creating instance: #{configuration.instance_id}"
49
+ project.create_instance(
50
+ configuration.instance_id, name: configuration.instance_id
51
+ ).wait_until_done!
52
+ end
53
+
54
+ def drop_instance
55
+ return unless instance_exists?
56
+
57
+ logger.debug "dropping instance: #{configuration.instance_id}"
58
+ instance.delete
59
+ end
60
+
61
+ # no-op if database already exists
62
+ def create_database
63
+ return if database_exists?
64
+
65
+ create_instance
66
+
67
+ logger.debug "creating database: #{configuration.database_id}"
68
+ instance.create_database(
69
+ configuration.database_id,
70
+ statements: configuration.schema_statements
71
+ ).wait_until_done!
72
+ end
73
+
74
+ def drop_database
75
+ return unless database_exists?
76
+
77
+ logger.debug "dropping database: #{configuration.database_id}"
78
+ client.database.drop
79
+ end
80
+
81
+ def database_path
82
+ client.database.path
83
+ end
84
+
85
+ private
86
+
87
+ # ensure that multiple threads do not share the same client instance
88
+ def thread_local_client
89
+ @client ||= Concurrent::ThreadLocalVar.new(nil)
90
+ @client.value ||= project.client(configuration.instance_id, configuration.database_id)
91
+ @client.value
92
+ end
93
+
94
+ # ensure that multiple threads do not share the same project connection instance
95
+ def thread_local_project
96
+ @project ||= Concurrent::ThreadLocalVar.new(nil)
97
+ @project.value ||= Google::Cloud::Spanner.new(
98
+ project_id: configuration.project_id,
99
+ emulator_host: configuration.emulator_host
100
+ )
101
+ @project.value
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,72 @@
1
+ module SpannerEmulatorToolkit
2
+ class Configuration
3
+ # Spanner settings
4
+ attr_accessor :project_id, :instance_id, :database_id, :emulator_host, :schema
5
+
6
+ # Generic settings
7
+ attr_accessor :logger, :log_level
8
+
9
+ def initialize
10
+ @project_id = "example-project"
11
+ @instance_id = "example-instance"
12
+ @database_id = "example-database"
13
+ @emulator_host = "localhost:9010"
14
+ end
15
+
16
+ def validate!
17
+ load_from_env
18
+ prepare_logger
19
+
20
+ %w[project_id instance_id database_id emulator_host].each do |attr|
21
+ raise "configuration.#{attr} must be set" unless send(attr)
22
+ end
23
+ end
24
+
25
+ def schema_statements
26
+ return [] unless schema
27
+
28
+ schema.split(";").map(&:strip).reject(&:empty?)
29
+ end
30
+
31
+ private
32
+
33
+ def load_from_env
34
+ %w[project_id instance_id database_id emulator_host].each do |attr|
35
+ env_key = "SPANNER_#{attr.upcase}"
36
+ if ENV[env_key] && !send(attr)
37
+ send("#{attr}=", ENV[env_key])
38
+ end
39
+ end
40
+ end
41
+
42
+ def colorized(text, logger_severity)
43
+ case logger_severity
44
+ when "ERROR"
45
+ "\e[31;1m#{text}\e[0m"
46
+ when "DEBUG"
47
+ "\e[32;1m#{text}\e[0m"
48
+ when "WARN"
49
+ "\e[33;1m#{text}\e[0m"
50
+ when "INFO"
51
+ "\e[34;1m#{text}\e[0m"
52
+ else
53
+ text
54
+ end
55
+ end
56
+
57
+ def prepare_logger
58
+ if logger.nil?
59
+ self.logger = Logger.new($stdout)
60
+ logger.formatter = proc do |severity, datetime, progname, msg|
61
+ [
62
+ "[SpannerEmulatorToolkit #{datetime.strftime("%Y-%m-%d %H:%M:%S")}]",
63
+ colorized(severity, severity),
64
+ msg
65
+ ].join(" ") + "\n"
66
+ end
67
+ end
68
+
69
+ logger.level = (log_level || Logger::ERROR)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Google
4
+ module Cloud
5
+ module Spanner
6
+ class Service
7
+ # add a missing list_sessions method
8
+ # @param database [String] in the form of a full Spanner identifier like
9
+ # "project/.../instance/.../database/..."
10
+ def list_sessions(database:, call_options: nil, token: nil, max: nil)
11
+ opts = default_options call_options: call_options
12
+ request = {
13
+ database: database,
14
+ page_size: max,
15
+ page_token: token
16
+ }
17
+ paged_enum = service.list_sessions request, opts
18
+ paged_enum.response
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "google/cloud/spanner"
4
+
5
+ module SpannerEmulatorToolkit
6
+ class << self
7
+ # reset all sessions on all databases on all instances for the configured emulator project
8
+ def reset_sessions!
9
+ configuration.validate!
10
+
11
+ logger.debug "resetting sessions"
12
+ project.instances.all do |instance|
13
+ logger.debug "instance: #{instance.path}"
14
+ instance.databases.all do |database|
15
+ logger.debug " database: #{database.path}"
16
+ each_session_for_database(database) do |session|
17
+ logger.debug " session: #{session.path}"
18
+ tx = session.create_empty_transaction
19
+ session.rollback tx.transaction_id
20
+ rescue => e
21
+ logger.debug " error resetting session: #{e.details}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def each_session_for_database(database)
30
+ # patched method, paginated
31
+ session_result = database.service.list_sessions(database: database.path)
32
+ next_page_token = session_result.next_page_token
33
+
34
+ loop do
35
+ session_result.sessions.each do |grpc_session|
36
+ yield Google::Cloud::Spanner::Session.new(grpc_session, database.service)
37
+ end
38
+
39
+ break if next_page_token.empty?
40
+
41
+ session_result = database.service.list_sessions(database: database.path, token: next_page_token)
42
+ next_page_token = session_result.next_page_token
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SpannerEmulatorToolkit
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "spanner_emulator_toolkit/version"
4
+ require_relative "spanner_emulator_toolkit/configuration"
5
+ require_relative "spanner_emulator_toolkit/client"
6
+ require_relative "spanner_emulator_toolkit/reset_sessions"
7
+ require_relative "spanner_emulator_toolkit/google_cloud_spanner_ext/service"
8
+
9
+ module SpannerEmulatorToolkit
10
+ class Error < StandardError; end
11
+
12
+ class << self
13
+ def configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def configure
18
+ yield(configuration)
19
+ end
20
+
21
+ def logger
22
+ configuration.logger
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ module SpannerEmulatorToolkit
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+
5
+ # configuration.rb
6
+ def self.configure: () { (SpannerEmulatorToolkit::Configuration) -> void } -> void
7
+ def self.logger: () -> Logger
8
+
9
+ # client.rb
10
+ def self.project: () -> Google::Cloud::Spanner::Project
11
+ def self.client: () -> Google::Cloud::Spanner::Client
12
+ def self.instance: () -> Google::Cloud::Spanner::Instance
13
+ def self.database: () -> Google::Cloud::Spanner::Database
14
+
15
+ def self.instance_exists?: () -> bool
16
+ def self.database_exists?: () -> bool
17
+
18
+ def self.create_instance: () -> void
19
+ def self.drop_instance: () -> void
20
+ def self.create_database: () -> void
21
+ def self.drop_database: () -> void
22
+
23
+ def self.database_path: () -> String
24
+
25
+ # reset_sessions.rb
26
+ def self.reset_sessions!: () -> void
27
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/spanner_emulator_toolkit/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "spanner_emulator_toolkit"
7
+ spec.version = SpannerEmulatorToolkit::VERSION
8
+ spec.authors = ["Adam Bachman"]
9
+ spec.email = ["adam.bachman@gmail.com"]
10
+
11
+ spec.summary = "Google Cloud Spanner emulator toolkit"
12
+ spec.description = "Helpers for working with the Google Cloud Spanner emulator."
13
+ spec.homepage = "https://github.com/abachman/spanner_emulator_toolkit"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.1.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ spec.metadata["changelog_uri"] = "https://github.com/abachman/spanner_emulator_toolkit/blob/main/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency "google-cloud-spanner", "~> 2.10"
33
+ spec.add_dependency "concurrent-ruby", "~> 1.2"
34
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spanner_emulator_toolkit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Bachman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-cloud-spanner
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: concurrent-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ description: Helpers for working with the Google Cloud Spanner emulator.
42
+ email:
43
+ - adam.bachman@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - CHANGELOG.md
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - Gemfile.lock
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - docker-compose.yml
58
+ - examples/create.rb
59
+ - examples/drop.rb
60
+ - examples/force_transaction_error.rb
61
+ - examples/session_reset.rb
62
+ - lib/spanner_emulator_toolkit.rb
63
+ - lib/spanner_emulator_toolkit/client.rb
64
+ - lib/spanner_emulator_toolkit/configuration.rb
65
+ - lib/spanner_emulator_toolkit/google_cloud_spanner_ext/service.rb
66
+ - lib/spanner_emulator_toolkit/reset_sessions.rb
67
+ - lib/spanner_emulator_toolkit/version.rb
68
+ - sig/spanner_emulator_toolkit.rbs
69
+ - spanner_emulator_toolkit.gemspec
70
+ homepage: https://github.com/abachman/spanner_emulator_toolkit
71
+ licenses:
72
+ - MIT
73
+ metadata:
74
+ allowed_push_host: https://rubygems.org
75
+ homepage_uri: https://github.com/abachman/spanner_emulator_toolkit
76
+ source_code_uri: https://github.com/abachman/spanner_emulator_toolkit
77
+ changelog_uri: https://github.com/abachman/spanner_emulator_toolkit/blob/main/CHANGELOG.md
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 3.1.0
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubygems_version: 3.4.22
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Google Cloud Spanner emulator toolkit
97
+ test_files: []