cirandas.net

ref: master

db/migrate/20101129234429_convert_folders_to_galleries.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class ConvertFoldersToGalleries < ActiveRecord::Migration
  def self.up
    select_all("select id, setting from articles where type = 'Folder'").each do |folder|
      view_as = YAML.load(folder['setting'] || {}.to_yaml)[:view_as]
      update("update articles set type = 'Gallery' where id = %d" % folder['id']) if view_as == 'image_gallery'
    end
  end

  def self.down
    select_all("select id, setting from articles where type = 'Gallery'").each do |folder|
      settings = YAML.load(folder['setting'] || {}.to_yaml)
      settings[:view_as] = 'image_gallery'
      assignments = ApplicationRecord.sanitize_sql_for_assignment(:setting => settings.to_yaml)
      update("update articles set %s, type = 'Folder' where id = %d" % [assignments, folder['id']])
    end
  end
end