Skip to content

Commit 7ad6847

Browse files
committed
samples(pubsub): pubsub_commit_avro_schema sample
1 parent 204d35b commit 7ad6847

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

google-cloud-pubsub/samples/acceptance/schemas_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
require_relative "helper"
16+
require_relative "../pubsub_commit_avro_schema"
1617
require_relative "../pubsub_commit_proto_schema"
1718
require_relative "../pubsub_create_avro_schema"
1819
require_relative "../pubsub_create_topic_with_schema"
@@ -183,6 +184,32 @@
183184
end
184185
end
185186
end
187+
188+
it "supports pubsub_commit_avro_schema & pubsub_commit_list_schema_revisions" do
189+
schema = Google::Cloud::PubSub::V1::Schema.new name: schema_id,
190+
type: :AVRO,
191+
definition: avsc_definition
192+
@schema = schemas.create_schema parent: pubsub.project_path,
193+
schema: schema,
194+
schema_id: schema_id
195+
196+
rev_id = @schema.revision_id
197+
198+
schema1 = nil
199+
# pubsub_commit_avro_schema
200+
out, _err = capture_io do
201+
schema1 = commit_avro_schema schema_id: schema_id, avsc_file: avsc_file
202+
end
203+
refute_equal out, "Schema commited with revision #{rev_id}."
204+
assert_includes out, "Schema commited with revision"
205+
206+
# pubsub_list_schema_revisions
207+
out, _err = capture_io do
208+
list_schema_revisions schema_id: schema_id
209+
end
210+
211+
assert_includes out, schema1.revision_id
212+
end
186213
end
187214

188215
describe "PROTOCOL_BUFFER" do
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require "google/cloud/pubsub"
16+
17+
def commit_avro_schema schema_id:, avsc_file:
18+
# [START pubsub_commit_avro_schema]
19+
# schema_id = "your-schema-id"
20+
# avsc_file = "path/to/a/avsc_file.avsc"
21+
22+
pubsub = Google::Cloud::Pubsub.new
23+
24+
schemas = pubsub.schemas
25+
26+
schema = schemas.get_schema name: pubsub.schema_path(schema_id)
27+
28+
definition = File.read avsc_file
29+
30+
schema.definition = definition
31+
32+
result = schemas.commit_schema name: schema.name,
33+
schema: schema
34+
35+
puts "Schema commited with revision #{result.revision_id}."
36+
result
37+
# [END pubsub_commit_avro_schema]
38+
end

0 commit comments

Comments
 (0)