Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Converting FileColumn store versions from old format to new format (/x) to (/0000/000x) (See related posts)

As of somewhen, the file_column rails plugin changed its storage layout and it doesn't seem to provide any way to migrate. Here's a simple snippet that'll do it.

Start an irb session in the directory where your file_column stores its files. By default that's RAILS_ROOT/public. In irb, run the following:

Dir['*/*/*'].grep(%r{[^/]+/[^/]+/\d+}).map { |s| [s.sub(/\d+$/, ''), $&.to_i] }\
  .map { |s, n| ["#{s}#{n}", (d = "#{s}%04d" % (n / 10000)), "#{d}/%04d" % (n % 10000)] }\
  .each { |f, d, t| `mkdir -p #{d} && mv #{f} #{t}` }


Or, as a migration

class UpgradeFileColumns < ActiveRecord::Migration
  def self.up
    # note I use a custom store path here
    Dir.chdir("#{RAILS_ROOT}/public/files")
    Dir['*/*/*'].grep(%r{[^/]+/[^/]+/\d+}).map { |s| [s.sub(/\d+$/, ''), $&.to_i] }\
      .map { |s, n| ["#{s}#{n}", (d = "#{s}%04d" % (n / 10000)), "#{d}/%04d" % (n % 10000)] }\
      .each { |f, d, t| `mkdir -p #{d} && mv #{f} #{t}` }
  end

  def self.down
  end
end



Comments on this post

chencui posts on Jan 04, 2010 at 08:42
During so many stylish beautiful accessories.Designer Handbags such as the Goyard Handbags,shoes such as the UGG Classic Dakota and the Tiffany &amp; Co Earrings are the most popular among the women all over the world.Here i would like to introduce the best stylish accessories to you.As for me,i like the Miu Miu Handbags.Miu Miu Handbags are one of the famous brand in the world.I like its classic and simple designe.Of course ,different people love different best stylish handbags.For example ,my sister love the Cheap Thomas Wylde Handbags.
chencui posts on Jan 04, 2010 at 08:44
During so many stylish beautiful accessories.Designer Handbags such as the Goyard Handbags,shoes such as the UGG Classic Dakota and the Tiffany &amp; Co Earrings are the most popular among the women all over the world.Here i would like to introduce the best stylish accessories to you.As for me,i like the Miu Miu Handbags.Miu Miu Handbags are one of the famous brand in the world.I like its classic and simple designe.Of course ,different people love different best stylish handbags.For example ,my sister love the Cheap Thomas Wylde Handbags.

You need to create an account or log in to post comments to this site.