Rails Migrations
Let's say you git check feature1
Then you run a migration that creates a table.
Then you git check feature2
Ideally you'd remember to rails db:rollback
before switching to that branch, but you forgot or maybe you spent some time creating some good test data already and don't want to remove it.
In the feature2 branch, you've already run some migrations and the developer forgot to add a column, so he rolled back, add the column, then force pushed the branch. Now you don't have the new column and you can't run the migration because rails thinks it already ran it. And if you try to rollback, you'll get
rails db:rollback
ActiveRecord::UnknownMigrationVersionError: (ActiveRecord::UnknownMigrationVersionError)
No migration with version number 20250303155013.
Because that migration is in another branch. You could create an empty migration with that number maybe?? Or today I learned you can do this:
rails db:migrate:status
up 20241217201044 Create ai tutor messages
up 20250220155121 ********** NO FILE **********
up 20250224095708 ********** NO FILE **********
up 20250303133136 ********** NO FILE **********
up 20250303135434 ********** NO FILE **********
up 20250303155013 ********** NO FILE **********
Which allows you to see what the database thinks has been migrated and what versions have a migrate file.
So now can you can clean up the migrations with no files.
delete from schema_migrations where version > '2024';
Now I can rollback.
Update: 5 min later
You can also:
rails db:migrate:down VERSION=20241217201044
I'd tried step before, but it doesn't work if your migrations are broken like described above.