Last active
July 21, 2023 09:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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