Sha256: 9ac431de4516f15cee7f9b840c9fb909a27f84aa38e2ea85d433548d07cc90f4

Contents?: true

Size: 1.51 KB

Versions: 30

Compression:

Stored size: 1.51 KB

Contents

class MoveTextFilterToTextFilterId < ActiveRecord::Migration
  class BareArticle < ActiveRecord::Base
    include BareMigration
  end

  class BarePage < ActiveRecord::Base
    include BareMigration
  end

  class BareTextFilter < ActiveRecord::Base
    include BareMigration
  end

  def self.up
    STDERR.puts "Converting Article and Page to use text_filter_id instead of text_filter"
    id_of = BareTextFilter.find(:all).inject({}) {|h, f| h.merge({ h[f.name] => f.id }) }

    modify_tables_and_update([:add_column, BareArticle, :text_filter_id, :integer],
                             [:add_column, BarePage,    :text_filter_id, :integer]) do
      (BareArticle.find(:all) + BarePage.find(:all)).each do |content|
        content.text_filter_id = id_of[content.attributes['text_filter']]
        content.save!
      end
    end
    remove_column :articles, :text_filter
    remove_column :pages,    :text_filter
  end

  def self.down
    STDERR.puts "Dropping text_filter_id in favor of text_filter"
    name_of = BareTextFilter.find(:all).inject({}) {|h, f| h.merge({ h[f.id] => f.name })}

    modify_tables_and_update([:add_column, BareArticle, :text_filter, :string],
                             [:add_column, BarePage,    :text_filter, :string]) do
      (BareArticle.find(:all) + BarePage.find(:all)).each do |content|
        content.attributes['text_filter'] = name_of[content.text_filter_id]
        content.save!
      end
    end
    remove_column :articles, :text_filter_id
    remove_column :pages,    :text_filter_id
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
typo-5.5 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-5.4.4 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-5.4.3 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-5.4.2 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-5.4.1 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-5.4 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-3.99.0 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-3.99.2 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-3.99.3 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-3.99.1 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-4.0.2 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-4.0.0 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-3.99.4 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-4.0.1 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-4.0.3 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-4.1.1 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-5.0.2 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-4.1 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-5.0.1 db/migrate/014_move_text_filter_to_text_filter_id.rb
typo-5.0.3.98.1 db/migrate/014_move_text_filter_to_text_filter_id.rb