Skip to content

Instantly share code, notes, and snippets.

@rymohr
Last active July 21, 2023 09:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rymohr/8546490 to your computer and use it in GitHub Desktop.
Save rymohr/8546490 to your computer and use it in GitHub Desktop.
Simple template for migrating your rails ActiveRecord::Store's from YAML to JSON
# Override your existing class with a simple replacement class
class Token < ActiveRecord::Base
end
class ConvertStoresToJson < ActiveRecord::Migration
def process(value)
if value && !value.starts_with?("{")
result = YAML.load(value).to_json
else
result = value
end
say "\t#{value} => #{result}"
result
end
def up
# Only select what we need to avoid any additional complications
Token.select([:id, :permissions, :options]).find_each do |token|
say "Converting #{token.id}..."
token.permissions = process(token.permissions_before_type_cast)
token.options = process(token.options_before_type_cast)
token.save!
end
end
def down
end
end
# Run `bundle exec rake db:migrate` and update your stores `store :settings, :coder => JSON`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment