Skip to content

Commit

Permalink
fix: model import handles case where model already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
drstrangelooker committed May 19, 2023
1 parent ccc5ab2 commit 302b4db
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/gzr/commands/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def cat(model_id)
end

desc 'import MODEL_FILE', 'Import a model configuration from a file containing json information'
method_option :force, type: :boolean,
desc: 'Overwrite an existing connection'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
def import(model_file)
Expand Down
23 changes: 18 additions & 5 deletions lib/gzr/commands/model/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,25 @@ def execute(input: $stdin, output: $stdout)
say_warning("options: #{@options.inspect}", output: output) if @options[:debug]
with_session do
read_file(@file) do |data|
data.select! do |k,v|
keys_to_keep('create_lookml_model').include? k
if !!cat_model(data[:name])
name = data[:name]
if !@options[:force]
raise Gzr::CLI::Error, "Model #{name} already exists\nUse --force if you want to overwrite it"
end
data.select! do |k,v|
keys_to_keep('update_lookml_model').include? k
end
model = update_model(data[:name],data)
output.puts "Updated model #{model[:name]}" unless @options[:plain]
output.puts model[:name] if @options[:plain]
else
data.select! do |k,v|
keys_to_keep('create_lookml_model').include? k
end
model = create_model(data)
output.puts "Created model #{model[:name]}" unless @options[:plain]
output.puts model[:name] if @options[:plain]
end
model = create_model(data)
output.puts "Created model #{model[:name]}" unless @options[:plain]
output.puts model[:name] if @options[:plain]
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions lib/gzr/modules/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,15 @@ def create_model(body)
end
end

def update_model(model_name,body)
begin
return @sdk.update_lookml_model(model_name,body)&.to_attrs
rescue LookerSDK::Error => e
say_error "Error running update_lookml_model(#{model_name},#{JSON.pretty_generate(body)})"
say_error e
raise
end
end

end
end

0 comments on commit 302b4db

Please sign in to comment.