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



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