Last active
August 29, 2015 14:24
-
-
Save Malet/27a10b063932377182c1 to your computer and use it in GitHub Desktop.
Future-proof Ruby on Rails migrations
This file contains hidden or 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 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