Skip to content

Instantly share code, notes, and snippets.

@alex-kovshovik
Created June 17, 2016 23:08
Show Gist options
  • Select an option

  • Save alex-kovshovik/d7758cf48411a37b9d34f8702be88fce to your computer and use it in GitHub Desktop.

Select an option

Save alex-kovshovik/d7758cf48411a37b9d34f8702be88fce to your computer and use it in GitHub Desktop.
Rails migration
class MigrateToNewContentStructure < ActiveRecord::Migration
FIND_EMBEDDED_RECIPE_REGEX = /!insert_recipe\((\d+)\)/
def up
ActiveRecord::Base.transaction do # Just wrap the bastard in the transaction block.
Recipe.all.each do |recipe|
recipe.content_block = ContentBlock.create!(...)
recipe.save!
end
BlogPost.all.each do |blog_post|
replaced_markdown = replace_recipe_ids_with_content_block_ids(blog_post.body_md) # dadada long comment testing
blog_post.content_block = ContentBlock.create!(...)
blog_post.content_block.save! # This forces the markdown inserts to re-process.
blog_post.save!
end
Page.all.each do |page|
page.content_block = ContentBlock.create!(...)
page.save!
end
end
end
def down
ContentBlock.delete_all
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment