Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

chore(pubsub): Add pubsub_list_schema_revisions sample #29174

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
23 changes: 23 additions & 0 deletions google-cloud-pubsub/lib/google/cloud/pubsub/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,29 @@ def exists?
@exists = false
end

##
# Lists all schema revisions for the named schema.
#
# @param view [Symbol, String, nil] The set of fields to return in the response. Possible values:
# * `BASIC` - Include the `name` and `type` of the schema, but not the `definition`.
# * `FULL` - Include all Schema object fields.
#
# @return [Google::Cloud::PubSub::V1::ListSchemaRevisionsResponse]
#
# @example
# require "google/cloud/pubsub"
#
# pubsub = Google::Cloud::PubSub.new
#
# schema = pubsub.schema "my-schema"
# schema.list_revisions
#
def list_revisions options = {}, view: nil
Copy link
Member

Choose a reason for hiding this comment

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

For new methods, please make all "optional" arguments keyword arguments instead of using an "options" hash. The "options" hash pattern is a legacy from older Ruby versions that didn't have good support for keyword arguments.

In particular, it's exceedingly awkward to have an options hash in addition to keyword arguments, because it forces users to do things like this in order to distinguish which keys should go into the options hash vs which should be interpreted as a keyword argument:

my_schema.list_revisions({max: 10, token: "abcde"}, view: "FULL")

For this reason, if an existing method already uses an "options" hash, then we should just add new keys to the existing options. But if a method doesn't already have an "options" hash, we should just add keyword arguments and not include an "options" hash at all.

service.list_schema_revisions name: name,
view: view || @view,
options: options
end

##
# Determines whether the schema object was created without retrieving the
# resource representation from the Pub/Sub service.
Expand Down
15 changes: 15 additions & 0 deletions google-cloud-pubsub/lib/google/cloud/pubsub/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,21 @@ def list_schemas view, options = {}
paged_enum.response
end

##
# Lists all schema revisions for the named schema.
# @param name [String] The name of the schema to list revisions for.
# @param view [String, Symbol, nil] Possible values:
# * `BASIC` - Include the name and type of the schema, but not the definition.
# * `FULL` - Include all Schema object fields.
#
def list_schema_revisions name, view, options = {}
Copy link
Member

Choose a reason for hiding this comment

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

Similarly, use keyword arguments here instead of an "options" hash.

schema_view = Google::Cloud::PubSub::V1::SchemaView.const_get view.to_s.upcase
schemas.list_schema_revisions name: name,
view: schema_view,
page_size: options[:max],
page_token: options[:token]
end

##
# Creates a schema in the current (or given) project.
def create_schema schema_id, type, definition, options = {}
Expand Down
30 changes: 19 additions & 11 deletions google-cloud-pubsub/samples/acceptance/schemas_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
# limitations under the License.

require_relative "helper"
require_relative "../pubsub_create_avro_schema.rb"
require_relative "../pubsub_create_topic_with_schema.rb"
require_relative "../pubsub_create_proto_schema.rb"
require_relative "../pubsub_delete_schema.rb"
require_relative "../pubsub_get_schema.rb"
require_relative "../pubsub_list_schemas.rb"
require_relative "../pubsub_publish_avro_records.rb"
require_relative "../pubsub_subscribe_avro_records.rb"
require_relative "../pubsub_publish_proto_messages.rb"
require_relative "../pubsub_subscribe_proto_messages.rb"
require_relative "../pubsub_create_avro_schema"
require_relative "../pubsub_create_topic_with_schema"
require_relative "../pubsub_create_proto_schema"
require_relative "../pubsub_delete_schema"
require_relative "../pubsub_get_schema"
require_relative "../pubsub_list_schema_revisions"
require_relative "../pubsub_list_schemas"
require_relative "../pubsub_publish_avro_records"
require_relative "../pubsub_subscribe_avro_records"
require_relative "../pubsub_publish_proto_messages"
require_relative "../pubsub_subscribe_proto_messages"


describe "schemas" do
Expand All @@ -38,7 +39,8 @@
@schema.delete if @schema
end

it "supports pubsub_create_schema, pubsub_get_schema, pubsub_list_schemas, pubsub_delete_schema" do
it "supports pubsub_create_schema, pubsub_get_schema, pubsub_List_schema_revisions, " \
"pubsub_list_schemas, pubsub_delete_schema" do
# create_avro_schema
assert_output "Schema projects/#{pubsub.project}/schemas/#{schema_id} created.\n" do
create_avro_schema schema_id: schema_id, avsc_file: avsc_file
Expand All @@ -52,6 +54,12 @@
get_schema schema_id: schema_id
end

# pubsub_list_schema_revisions
out, _err = capture_io do
list_schema_revisions
end
assert_includes out, "Listed revisions of schema"

# pubsub_list_schemas
out, _err = capture_io do
list_schemas
Expand Down
32 changes: 32 additions & 0 deletions google-cloud-pubsub/samples/pubsub_list_schema_revisions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2025 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 "google/cloud/pubsub"

def list_schema_revisions
# [START pubsub_list_schema_revisions]
# schema_id = "your-schema-id"

pubsub = Google::Cloud::Pubsub.new

schema = pubsub.schema schema_id

response = schema.list_revisions

puts "Listed revisions of schema #{schema_id}"
response.schemas.each do |revision|
puts revision.revision_id
end
# [END pubsub_list_schema_revisions]
end