Skip to content

Commit

Permalink
Adding executable to run surveyor from the command line.
Browse files Browse the repository at this point in the history
Changing surveyor to not recreate survey items for existing items when you run the executable multiple times.
  • Loading branch information
carolyncole committed Jul 12, 2016
1 parent a100b37 commit 9fc2197
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
91 changes: 91 additions & 0 deletions exe/sufia_survey
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env ruby
require 'rails'

require ::File.join(File.dirname(Bundler.default_gemfile), 'config/environment')

options = {}

subtext = <<HELP
Commonly used command are:
clean : cleans all Sufia::Migration::Survey::Items from the database
See 'sufia_survey COMMAND --help' for more information on a specific command.
HELP

args = OptionParser.new do |opts|
opts.banner = "Survey model objects in fedora and store it in an ActiveRecord table. Usage: sufia_survey [options]"

opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end

opts.on("--limit NUMBER_OF_OBJECTS", Integer, "Survey a maximum number of objects (defaults to all)") do |v|
options[:limit] = v
end

opts.on("--ids ID_LIST", Array, "Survey only the objects in the list of IDs",
"For example to survey a Collection with the id abc123 and a GenericFile with an id def456:",
" --ids abc123,def456") do |v|
options[:ids] = v
end

opts.on("--models MODEL_LIST", Array, "Specify the models to be surveyed (defaults to GenericFile,Collection)") do |v|
options[:models] = v
end

opts.on("--models MODEL_LIST", Array, "Specify the models to be surveyed (defaults to GenericFile,Collection)") do |v|
options[:models] = v
end

opts.separator ""
opts.separator subtext
end

subcommands = {
'clean' => OptionParser.new do |opts|
opts.banner = "Usage: sufia_survey clean"
end
}

begin
args.order!
command = ARGV.shift
if command
raise "Bad subcommand: #{command}" unless subcommands[command]
subcommands[command].order!
end
args.parse!
rescue => error
$stderr.puts "ERROR: #{error}\n"
$stderr.puts args.help
exit 1
end

if command == 'clean'
$stderr.puts "cleaning Sufia::Migration::Survey::Item"
Sufia::Migration::Survey::Item.delete_all
exit 0
end

verbose = options[:verbose]

$stdout.puts "running with #{options}" if verbose
id_service = Sufia::Migration::Survey::FedoraIdService.new

if options[:models]
model_options = options.delete(:models)
$stdout.puts "processing models #{model_options}" if verbose
model_options.each do |model_str|
model_class = Object.const_get(model_str)
id_service.register_model(model_class) unless id_service.model_registry.keys.include?(model_class)
end
end

if options[:ids]
ids = options[:ids]
else
$stdout.puts "Sending fedora_id with #{id_service.model_registry} and #{options}" if verbose
ids = id_service.call(options[:limit] || :all)
end

$stdout.puts "Sending call with ids: #{ids}" if verbose
Sufia::Migration::Survey::Surveyor.call(ids)
4 changes: 3 additions & 1 deletion lib/sufia/migration/survey/surveyor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class << self
# @param [Array] id_list a list of ids to be surveyed
def call(id_list)
ActiveFedora::Base.find(id_list).each do |object|
Item.create(object_id: object.id, object_class: object.class, object_title: object.title, migration_status: :initial_state)
Item.find_or_create_by(object_id: object.id) do |item|
item.assign_attributes(object_class: object.class, object_title: object.title, migration_status: :initial_state)
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/lib/sufia/migration/surveyor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@
expect(collection_survey).not_to be_nil
end
end

context "when item is surveyed twice" do
let(:object) { file }
before do
described_class.call(ids)
described_class.call(ids)
end
it "creates a single item" do
expect(Sufia::Migration::Survey::Item.where(object_id: object.id).count).to eq 1
end
end
end

context "when one of the objects does not exist" do
Expand Down

0 comments on commit 9fc2197

Please sign in to comment.