Converting FileColumn store versions from old format to new format (/x) to (/0000/000x)
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:
Or, as a migration
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