class RestoreSlugIndexToGroups < ActiveRecord::Migration
  Group.all.each do |group|
    group.slug = nil
    group.save!
  end

  def change
    add_index :groups, :slug
  end
end

class RestoreSlugIndexToGroups < ActiveRecord::Migration
  class Group < ActiveRecord::Base

    extend FriendlyId
    friendly_id :slug_candidates, use: :slugged

    has_ancestry cache_depth: true

    def slug_candidates
      candidates = [name]
      candidates <<  [parent.name, name] if parent.present?
      candidates
    end
  end

  def change
   Group.all.each do |group|
      group.slug = nil
      group.save
    end

    add_index :groups, :slug
  end
end

Update the group slugs and restore the index
Update the group slugs and restore the index

- Note that this migration fails when run in a
batch with this error:
PG::FeatureNotSupported: ERROR:
cached plan must not change result type

- This appears to be caused by executing sql
in the previous migration that adds ancestry
to the group

- If the migration fails, it works the next time
