cirandas.net

ref: master

db/migrate/20110215153624_move_data_serialized_hash_to_setting_field_for_articles.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class MoveDataSerializedHashToSettingFieldForArticles < ActiveRecord::Migration
  def self.up
    select_all("SELECT id FROM tasks WHERE type = 'ApproveArticle' AND status = 1").each do |data|
      article = Task.find(data['id']).article
      next unless article.kind_of?(Event)
      body = ''
      begin
        body = YAML.load(article.body)
      rescue
        # do nothing
        next
      end
      if body.kind_of?(Hash)
        settings = article.setting.merge(body)
        body = ApplicationRecord.sanitize_sql_for_assignment(:body => settings[:description])
        update("UPDATE articles set %s WHERE id = %d" % [body, article.id])
        setting = ApplicationRecord.sanitize_sql_for_assignment(:setting => settings.to_yaml)
        update("UPDATE articles set %s WHERE id = %d" % [setting, article.id])
      end
    end
  end

  def self.down
    say "Nothing to undo"
  end
end