My Migration references an nonexistent class:
class AddDataToUsers < ActiveRecord::Migration[4.2]
def up
UsersService.load_data
end
def down
User.delete_all
end
end
UsersService was deleted but I still have this old migration referencing it.
Devs are not able to run migrations from start now. How should I handle it to fix my migrations?
I have tried to change the old migration file to check if the UserService class is defined. I'm not sure if this is a good practice though.
As a best practice, you shouldn't be referencing models (or other classes!) in migrations. If you need to load / modify / delete data, you should be creating a rake task for it instead of a migration.
In your case, since the error has already been made, you have to remediate somehow. Since in this particular case it looks like you're loading data into a table that doesn't exist anymore, it should be safe to just delete this migration or comment the lines that reference the non-existing model.
Keep in mind that the "correct thing to do" will be different in different instances of the problem.
@spickermann is not wrong in that you can potentially delete old migrations that have run everywhere and rely on the
db/schema.rbfile. I however prefer to "squash" old migrations into a single file, getting the same speed benefit, but without breaking the Rails expectation ofdb:migrateto work from a scratch database.