Sha256: b2fba6eb4f71581e39e08cf925e21a1a6e0a59865a8ce5b936a87ec503ea1a3a

Contents?: true

Size: 1.91 KB

Versions: 20

Compression:

Stored size: 1.91 KB

Contents

class FixZazenImageTag < ActiveRecord::Migration
  def self.up
    # Change all zazen image tags from !34.pv! to !34_pv! for better consistency with 'modes'.
    {
      Version.table_name        => [:text, :summary],
      'dyn_attributes'          => [:value],
      ContactContent.table_name => [:address],
      Comment.table_name        => [:text],
      DataEntry.table_name      => [:text],
    }.each do |table_name, keys|
      select_all("SELECT id,#{keys.join(',')} FROM #{table_name}", "#{table_name} Load").each do |record|
        new_value = {}
        keys.each do |k|
          next unless record[k.to_s]
          value = record[k.to_s].gsub(/\!([^0-9]{0,2}[0-9]+)(\.([^\/\!]+)|)(\/[^\!]*|)\!/) do
            "!#{$1}#{$3 ? "_#{$3}" : ''}#{$4}!"
          end
          new_value[k] = value if value != record[k.to_s]
        end
        execute "UPDATE #{table_name} SET #{new_value.map{|k,v| "#{k} = #{quote(v)}"}.join(', ')} WHERE id = #{record['id']}" if new_value != {}
      end
    end
  end

  def self.down
    # Change back all zazen image tags from !34_pv! to !34.pv!
    {
      Version.table_name        => [:text, :summary],
      'dyn_attributes'          => [:value],
      ContactContent.table_name => [:address],
      Comment.table_name        => [:text],
      DataEntry.table_name      => [:text],
    }.each do |table_name, keys|
      select_all("SELECT id,#{keys.join(',')} FROM #{table_name}", "#{table_name} Load").each do |record|
        new_value = {}
        keys.each do |k|
          next unless record[k.to_s]
          value = record[k.to_s].gsub(/\!([^0-9]{0,2}[0-9]+)(_([^\/\!]+)|)(\/[^\!]*|)\!/) do
            "!#{$1}#{$3 ? ".#{$3}" : ''}#{$4}!"
          end
          new_value[k] = value if value != record[k.to_s]
        end
        execute "UPDATE #{table_name} SET #{new_value.map{|k,v| "#{k} = #{quote(v)}"}.join(', ')} WHERE id = #{record['id']}" if new_value != {}
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
zena-1.2.8 db/fix/046_fix_zazen_image_tag.rb
zena-1.2.7 db/fix/046_fix_zazen_image_tag.rb
zena-1.2.6 db/fix/046_fix_zazen_image_tag.rb
zena-1.2.5 db/fix/046_fix_zazen_image_tag.rb
zena-1.2.4 db/fix/046_fix_zazen_image_tag.rb
zena-1.2.3 db/fix/046_fix_zazen_image_tag.rb
zena-1.2.2 db/fix/046_fix_zazen_image_tag.rb
zena-1.2.1 db/fix/046_fix_zazen_image_tag.rb
zena-1.2.0 db/fix/046_fix_zazen_image_tag.rb
zena-1.1.3 db/fix/046_fix_zazen_image_tag.rb
zena-1.1.2 db/fix/046_fix_zazen_image_tag.rb
zena-1.1.1 db/fix/046_fix_zazen_image_tag.rb
zena-1.1.0 db/fix/046_fix_zazen_image_tag.rb
zena-1.0.0 db/fix/046_fix_zazen_image_tag.rb
zena-1.0.0.rc3 db/fix/046_fix_zazen_image_tag.rb
zena-1.0.0.rc2 db/fix/046_fix_zazen_image_tag.rb
zena-1.0.0.rc1 db/fix/046_fix_zazen_image_tag.rb
zena-1.0.0.beta3 db/migrate/046_fix_zazen_image_tag.rb
zena-1.0.0.beta2 db/migrate/046_fix_zazen_image_tag.rb
zena-1.0.0.beta1 db/migrate/046_fix_zazen_image_tag.rb