Created
January 29, 2011 01:08
-
-
Save edavis10/801368 to your computer and use it in GitHub Desktop.
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 Alert < ActiveRecord::Base | |
def self.delete_stale | |
execute <<-EOS | |
DELETE FROM alerts | |
WHERE alerts.alertable_type = 'Post' | |
AND alerts.alertable_id IN ( | |
SELECT id FROM posts WHERE deleted = 1 | |
) | |
EOS | |
end | |
end |
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
# Use normal testing code since this is just a unit test now... |
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 DeleteAlertsForDeletedPosts < ActiveRecord::Migration | |
def self.up | |
Alert.delete_stale | |
end | |
def self.down | |
say "Previous migration removed stale alerts, there is nothing to do in this .down" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My issue with this is pointed out below:
http://gem-session.com/2010/03/how-to-use-models-in-your-migrations-without-killing-kittens
Since the resolution would be to move the code to the migration itself (ie: by reopening the Alert model and adding the delete_stale method within your migration .rb) it seems like the test focus moves back from the model and into the migration.
I follow where you're going with this though, and still find that this gray area of data migration (and not schema change) is not a well defined best practice. What do you think?