Sha256: d830a37d5273c2cfd854e3166263226ecaf694b264cf20ab17025d3beb868e44

Contents?: true

Size: 909 Bytes

Versions: 7

Compression:

Stored size: 909 Bytes

Contents

class ChangeFurtherSubscribedUsersToSet < ActiveRecord::Migration
  def up
    # Change the other tables that were missing from the previous migration
    %w(campaigns opportunities leads tasks accounts).each do |table|
      entities = connection.select_all %(
        SELECT id, subscribed_users
        FROM #{table}
        WHERE subscribed_users IS NOT NULL
            )

      puts "#{table}: Converting #{entities.size} subscribed_users arrays into sets..." unless entities.empty?

      # Run as one atomic action.
      ActiveRecord::Base.transaction do
        entities.each do |entity|
          subscribed_users_set = Set.new(YAML.load(entity["subscribed_users"]))

          connection.execute %(
            UPDATE #{table}
            SET subscribed_users = '#{subscribed_users_set.to_yaml}'
            WHERE id = #{entity['id']}
                    )
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
fat_free_crm-0.14.2 db/migrate/20120405080742_change_further_subscribed_users_to_set.rb
fat_free_crm-0.14.1 db/migrate/20120405080742_change_further_subscribed_users_to_set.rb
fat_free_crm-0.15.0.beta.2 db/migrate/20120405080742_change_further_subscribed_users_to_set.rb
fat_free_crm-0.15.0.beta db/migrate/20120405080742_change_further_subscribed_users_to_set.rb
fat_free_crm-0.14.0 db/migrate/20120405080742_change_further_subscribed_users_to_set.rb
reduced_fat_crm-0.15.0.beta db/migrate/20120405080742_change_further_subscribed_users_to_set.rb
reduced_fat_crm-0.14.0 db/migrate/20120405080742_change_further_subscribed_users_to_set.rb