Skip to content

Instantly share code, notes, and snippets.

@detkin
Created February 10, 2016 18:11
Show Gist options
  • Save detkin/e779e699060dacc7cc71 to your computer and use it in GitHub Desktop.
Save detkin/e779e699060dacc7cc71 to your computer and use it in GitHub Desktop.
This is to merge the two twilio components into on new component in the same group and with all of the subscribers automatically subscribed to the new component.
def merge_twilio_components
c1 = Component.find('pysn09gt7s41')
c2 = Component.find('8slry56nyr1z')
new_comp = Component.new(name: 'SIP', description: 'Network Traversal Service - SIP', page: c1.page, group_id: c1.group_id, only_show_if_degraded: true)
new_comp.save
# Run through and move subscribers from the old comps to the new
page_sub_set = Set.new
page_sub_set.merge(c1.subscribers.to_set)
page_sub_set.merge(c2.subscribers.to_set)
page_sub_set.each do |page_sub|
# Delete the old sub comp if they exist for each old comp, do it this way so no emails are sent
page_sub.subscriber_components.where(component_id: c1).delete_all
page_sub.subscriber_components.where(component_id: c2).delete_all
# Create a new sub comp for the new component, do it this way so no emails are sent
SubscriberComponent.create!(subscriber_id: page_sub.id, component_id: new_comp)
end
# Run through the 3rd party components and update the based_on_id for the old ones to point to the new one, looks like the 4 that are using it include both so just delete the others
# Also none of them have subscribers so we don't have to worry about that
Component.where(based_on_id: c1).each do |comp|
comp.update_attribute(:based_on_id, new_comp.id)
end
Component.where(based_on_id: c2).delete_all
# Do the actual switch over
c1.destroy
c2.destroy
new_comp.update_attribute(:only_show_if_degraded, false)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment