Skip to content

Instantly share code, notes, and snippets.

@Malet
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Malet/27a10b063932377182c1 to your computer and use it in GitHub Desktop.
Save Malet/27a10b063932377182c1 to your computer and use it in GitHub Desktop.
Future-proof Ruby on Rails migrations
class DoubleUserBalance < ActiveRecord::Migration
def up
# Double user's balance
prepared = ActiveRecord::Base.connection.raw_connection.prepare("
UPDATE users
SET balance = balance * ?
")
prepared.execute(2)
prepared.close
end
def down
# Halve user's balance
prepared = ActiveRecord::Base.connection.raw_connection.prepare("
UPDATE users
SET balance = balance * ?
")
prepared.execute(1/2)
prepared.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment