Skip to content

Instantly share code, notes, and snippets.

@jozr
Last active July 18, 2018 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jozr/063ad517eaaf836f1cd5dd88b8a823b6 to your computer and use it in GitHub Desktop.
Save jozr/063ad517eaaf836f1cd5dd88b8a823b6 to your computer and use it in GitHub Desktop.
SALT-821: Migrate "ConversationElements::Feedback" to "ConversationElements::Plaintext"
@count = Conversation.where("conversation_groups.conversation_elements._type" => "ConversationElements::Feedback").count
Conversation.where("conversation_groups.conversation_elements._type" => "ConversationElements::Feedback").each_with_index do |conversation, i|
percentage = (i.to_f / @count) * 100
puts "PROGRESS: #{percentage.to_s}" if percentage % 10 == 0
for group in conversation.conversation_groups do
for element in group.conversation_elements do
if element._type == "ConversationElements::Feedback"
text = element.text
group.conversation_elements << ConversationElements::Plaintext.new(text: text)
element.delete
end
end
end
end
@jozr
Copy link
Author

jozr commented Jul 10, 2018

For

Conversation.where("conversation_groups.conversation_elements._type" => "ConversationElements::Feedback").count

there are 198 on staging and 2185 on production.

@jozr
Copy link
Author

jozr commented Jul 10, 2018

there has to be a better way to do this...

@jozr
Copy link
Author

jozr commented Jul 10, 2018

apparently not after looking through MondoDB documentation

@graffzon
Copy link

Yeah, there is no better way, unfortunately :)
If we would prepare this code to run in a continuous mode, I'd try to find a way to improve it (like iterators instead of for loops or remove a where condition, which will return 99.99% of all conversations anyway, but at the same time consumes some time to calculate), or even rewrite it to two more idiomatic mongo queries, but for our purpose it totally fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment