Skip to content

Instantly share code, notes, and snippets.

@nownabe
Created May 18, 2023 00:56
Show Gist options
  • Save nownabe/e617d9e6d478abfc99570519b4d9106d to your computer and use it in GitHub Desktop.
Save nownabe/e617d9e6d478abfc99570519b4d9106d to your computer and use it in GitHub Desktop.
Setup Spanner Emulator
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "google-cloud-spanner"
end
require "google/cloud/spanner"
project_id = ARGV[0]
instance_id = ARGV[1]
database_id = ARGV[2]
ENV["SPANNER_EMULATOR_HOST"] = "localhost:9010"
client = Google::Cloud::Spanner.new(project_id: project_id)
if client.instance(instance_id)
puts "Instance already exists: `#{instance_id}`"
else
client.create_instance(
instance_id,
name: instance_id,
config: "emulator-config",
nodes: 1,
).wait_until_done!
puts "Create Spanner instance: `#{instance_id}`"
end
instance = client.instance(instance_id)
if instance.database(database_id)
puts "Database already exists: `#{database_id}`"
else
instance.create_database(database_id).wait_until_done!
puts "Created Spanner database: `#{database_id}`"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment