Sha256: a2626c681be62135cfc49e2a1b0c066603cd42c93391981ebdb3fa7b7c784e1c

Contents?: true

Size: 1.95 KB

Versions: 13

Compression:

Stored size: 1.95 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.map{|k| "`#{k}`"}.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.map{|k| "`#{k}`"}.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

13 entries across 13 versions & 1 rubygems

Version Path
zena-0.16.9 db/migrate/046_fix_zazen_image_tag.rb
zena-0.16.8 db/migrate/046_fix_zazen_image_tag.rb
zena-0.16.7 db/migrate/046_fix_zazen_image_tag.rb
zena-0.16.6 db/migrate/046_fix_zazen_image_tag.rb
zena-0.16.5 db/migrate/046_fix_zazen_image_tag.rb
zena-0.16.4 db/migrate/046_fix_zazen_image_tag.rb
zena-0.16.3 db/migrate/046_fix_zazen_image_tag.rb
zena-0.16.2 db/migrate/046_fix_zazen_image_tag.rb
zena-0.16.1 db/migrate/046_fix_zazen_image_tag.rb
zena-0.16.0 db/migrate/046_fix_zazen_image_tag.rb
zena-0.15.2 db/migrate/046_fix_zazen_image_tag.rb
zena-0.15.1 db/migrate/046_fix_zazen_image_tag.rb
zena-0.15.0 db/migrate/046_fix_zazen_image_tag.rb