Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pubsub/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

source "https://rubygems.org"

gem "google-cloud-pubsub", "~> 3.0"

group :test do
gem "rspec"
gem "rspec_junit_formatter"
end
33 changes: 33 additions & 0 deletions pubsub/rollback_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START pubsub_rollback_schema]
require "google/cloud/pubsub"

def rollback_schema project_id:, schema_id:, revision_id:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to the community Ruby Style Guide, parentheses should be used for method definitions that have arguments. Adding parentheses also ensures that the code complies with standard RuboCop rules (such as Style/MethodDefParentheses), which is important since the PR checklist includes a RuboCop lint pass.

def rollback_schema(project_id:, schema_id:, revision_id:)
References
  1. Use parentheses for method definitions when they have arguments. (link)

# project_id = "your-project-id"
# schema_id = "your-schema-id"
# revision_id = "your-revision-id"

pubsub = Google::Cloud::PubSub.new project_id: project_id
schema_client = pubsub.schema_service_client

schema_path = "projects/#{project_id}/schemas/#{schema_id}"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of manually constructing the schema resource path using string interpolation, it is more robust and idiomatic to use the path helper method schema_path provided by the GAPIC client. This prevents potential errors in path formatting.

  schema_path = schema_client.schema_path project: project_id, schema: schema_id


response = schema_client.rollback_schema name: schema_path, revision_id: revision_id

puts "Rolled back schema: #{response.name}"
puts "New revision ID: #{response.revision_id}"
end
# [END pubsub_rollback_schema]
84 changes: 84 additions & 0 deletions pubsub/spec/rollback_schema_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "rspec"
require "google/cloud/pubsub"
require "securerandom"
require_relative "../rollback_schema"

RSpec.describe "Rollback Schema" do
before :all do
@project_id = ENV["GOOGLE_CLOUD_PROJECT"]
if @project_id.nil?
skip "GOOGLE_CLOUD_PROJECT not defined"
end

@pubsub = Google::Cloud::PubSub.new project_id: @project_id
@schema_client = @pubsub.schema_service_client
@schema_id = "test-schema-#{SecureRandom.hex(4)}"
@schema_path = "projects/#{@project_id}/schemas/#{@schema_id}"

# Create schema (Revision 1)
@definition_1 = "syntax = 'proto3'; message Message { string data = 1; }"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Protocol Buffers specification strictly requires double quotes for the syntax statement (i.e., syntax = "proto3";). While some parsers might be lenient, using single quotes ('proto3') can lead to parsing errors in strict environments or future updates. It is safer and more standard to use single quotes for the Ruby string literal and double quotes for the protobuf definition.

    @definition_1 = 'syntax = "proto3"; message Message { string data = 1; }'

schema = {
name: @schema_path,
type: :PROTOCOL_BUFFER,
definition: @definition_1
}
@initial_schema = @schema_client.create_schema(
parent: "projects/#{@project_id}",
schema: schema,
schema_id: @schema_id
)
@revision_1_id = @initial_schema.revision_id

# Commit revision (Revision 2)
@definition_2 = "syntax = 'proto3'; message Message { string data = 1; string new_field = 2; }"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Protocol Buffers specification strictly requires double quotes for the syntax statement (i.e., syntax = "proto3";). It is safer and more standard to use single quotes for the Ruby string literal and double quotes for the protobuf definition.

    @definition_2 = 'syntax = "proto3"; message Message { string data = 1; string new_field = 2; }'

@second_schema = @schema_client.commit_schema(
name: @schema_path,
schema: {
name: @schema_path,
type: :PROTOCOL_BUFFER,
definition: @definition_2
}
)
@revision_2_id = @second_schema.revision_id
end

after :all do
if @schema_client && @schema_path
begin
@schema_client.delete_schema name: @schema_path
rescue StandardError => e
puts "Error cleaning up schema: #{e.message}"
end
end
end

it "rolls back the schema to a previous revision" do
expect {
rollback_schema(
project_id: @project_id,
schema_id: @schema_id,
revision_id: @revision_1_id
)
}.to output(/Rolled back schema:.*#{@schema_id}/).to_stdout

# Verify the current schema revision is indeed a copy of revision 1
current_schema = @schema_client.get_schema name: @schema_path
expect(current_schema.definition).to eq(@definition_1)
expect(current_schema.revision_id).not_to eq(@revision_1_id)
expect(current_schema.revision_id).not_to eq(@revision_2_id)
end
end