Created
April 27, 2019 17:14
-
-
Save stephancom/886e65c46ac0cc045b141adc9d807462 to your computer and use it in GitHub Desktop.
migrate to actiontext (untested)
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
class MigrateFieldsToActionText < ActiveRecord::Migration[5.2] | |
class ActionTextRichText < ActiveRecord::Base | |
belongs_to :record, polymorphic: true | |
end | |
TARGETS = { | |
ModelName => [:field], | |
OtherModelName => %i[field1 field2 field3] | |
}.freeze | |
def up | |
TARGETS.each do |model, fields| | |
fields.each do |field| | |
model.where.not(field => nil).each do |record| | |
ActionTextRichText.where( | |
record: record, | |
name: first | |
).first_or_create( | |
body: model.send(field) | |
) | |
end | |
remove_column model.table_name.to_sym, field.to_sym | |
end | |
end | |
end | |
def down | |
TARGETS.each do |model, fields| | |
fields.each do |field| | |
add_column model.table_name.to_sym, :tmp, :text | |
ActionTextRichText.where(record_type: model.name, name: field).each do |entry| | |
entry.record.update_column(field => entry.body) | |
entry.destroy | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment