Skip to content

Commit

Permalink
Add avro:register_all_schemas task (#19)
Browse files Browse the repository at this point in the history
* Add avro:register_all_schemas task

* Update README.md

* Feedback

* Exit if no schemas are found
  • Loading branch information
Josh Branham authored Apr 1, 2022
1 parent c56e3f5 commit 241cbca
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# avrolution

## v0.8.0

- Added the ability to register all schemas found under `Avrolution.root` with the task
`rake avro:register_all_schemas`.

## v0.7.2
- Fix a bug related to Ruby 3.0 keyword arguments in
AvroSchemaRegistry::Client#register_without_lookup.
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ require 'avroluation/rake/register_schemas_task'
Avrolution::Rake::RegisterSchemasTask.define
```

### Avro Register All Schemas Rake Task

This rake task allows you to register all schemas discovered under `Avrolution.root`.

Similarly to the task `avro:register_schemas`, it will register them against the configured
registry. Additionally, this task will be auto included for Rails applications.

```bash
rake avro:register_all_schemas
```

For non-Rails projects, tasks can be defined as:

```ruby
require 'avroluation/rake/register_all_schemas_task'
Avrolution::Rake::RegisterAllSchemasTask.define
```

### Avro Add Compatibility Break Rake Task

There is a rake task add an entry to the `Avrolution.compatibility_breaks_file`.
Expand Down
4 changes: 2 additions & 2 deletions avrolution.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Gem::Specification.new do |spec|
end

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = 'bin'
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.required_ruby_version = '>= 2.6'
Expand Down
1 change: 1 addition & 0 deletions lib/avrolution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def initialize(*)
require 'avrolution/compatibility_break'
require 'avrolution/compatibility_breaks_file'
require 'avrolution/compatibility_check'
require 'avrolution/discover_schemas'
require 'avrolution/register_schemas'

require 'avrolution/railtie' if defined?(Rails)
5 changes: 1 addition & 4 deletions lib/avrolution/compatibility_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ def success?
private

def check_schemas(path)
vendor_bundle_path = File.join(path, 'vendor/bundle/')
Dir[File.join(path, '**/*.avsc')].reject do |file|
file.start_with?(vendor_bundle_path)
end.each do |schema_file|
Avrolution::DiscoverSchemas.discover(path).each do |schema_file|
check_schema_compatibility(schema_file)
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/avrolution/discover_schemas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Avrolution
class DiscoverSchemas
def self.discover(path)
vendor_bundle_path = File.join(path, 'vendor/bundle/')
Dir[File.join(path, '**/*.avsc')].reject do |file|
file.start_with?(vendor_bundle_path)
end
end
end
end
2 changes: 2 additions & 0 deletions lib/avrolution/rake/rails_avrolution.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

require 'avrolution/rake/check_compatibility_task'
require 'avrolution/rake/add_compatibility_break_task'
require 'avrolution/rake/register_all_schemas_task'
require 'avrolution/rake/register_schemas_task'

Avrolution::Rake::AddCompatibilityBreakTask.define(dependencies: [:environment])
Avrolution::Rake::CheckCompatibilityTask.define(dependencies: [:environment])
Avrolution::Rake::RegisterAllSchemasTask.define(dependencies: [:environment])
Avrolution::Rake::RegisterSchemasTask.define(dependencies: [:environment])
29 changes: 29 additions & 0 deletions lib/avrolution/rake/register_all_schemas_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'avrolution/rake/base_task'

module Avrolution
module Rake
class RegisterAllSchemasTask < BaseTask

def initialize(**)
super
@name ||= :register_all_schemas
@task_desc ||= 'Register all discovered Avro JSON schemas (using Avrolution.root)'
end

private

def perform
schemas = Avrolution::DiscoverSchemas.discover(Avrolution.root)

if schemas.blank?
puts 'could not find any schemas'
exit(1)
else
Avrolution::RegisterSchemas.call(schemas)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/avrolution/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Avrolution
VERSION = '0.7.2'
VERSION = '0.8.0'
end
17 changes: 17 additions & 0 deletions spec/avrolution/tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
end
end

describe "register_all_schemas" do
let(:task_name) { 'avro:register_all_schemas' }
let(:schema_files) { Avrolution::DiscoverSchemas.discover(Dir.pwd) }
let(:register_schemas) { instance_spy(Avrolution::RegisterSchemas) }

before do
allow(Avrolution::RegisterSchemas).to receive(:new).and_return(register_schemas)
allow(Avrolution::DiscoverSchemas).to receive(:discover).and_return(schema_files)
end

it "dispatches to a RegisterSchemas instance" do
task.invoke

expect(register_schemas).to have_received(:call)
end
end

describe "check_compatibility" do
let(:task_name) { 'avro:check_compatibility' }
let(:compatibility_check) { Avrolution::CompatibilityCheck.new }
Expand Down
9 changes: 9 additions & 0 deletions spec/fixtures/sample_gem/avro/schema/com/foo/person.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type" : "record",
"namespace" : "foo",
"name" : "person",
"fields" : [
{ "name" : "Name" , "type" : "string" },
{ "name" : "Age" , "type" : "int" }
]
}
9 changes: 9 additions & 0 deletions spec/fixtures/sample_gem/avro/schema/com/foo/pet.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type" : "record",
"namespace" : "foo",
"name" : "pet",
"fields" : [
{ "name" : "Name" , "type" : "string" },
{ "name" : "Age" , "type" : "int" }
]
}

0 comments on commit 241cbca

Please sign in to comment.