Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Rails know that a migration has been up'ed/run? [duplicate]

I just loaded a DB dump, and not only it created tables, and loaded the data in those tables, it also up'ed a lot of migrations.

And after running the remaining migrations, running rake db:migrate:status tells me that the status of each migration is UP, but the migrations have not actually done what they are supposed to do.

For example, I have a migration that creates a table called notes. It has been up'ed, and the tables notes doesn't exist. So I had to redo this migration, but commenting out the code in self.down method.

My question is: How can a migration be in UP status even though the table it tries to create, hasn't been created in the database. Or I guess, the db dumb that I loaded earlier, has populated schema_migrations table in such a way that Rails considers mostly migrations are up, but actually they aren't.

Now, do I need manually need to rake db:migrate:redo VERSION=xxxxxxx each migration, or there is a better solution?

like image 959
Arslan Ali Avatar asked Oct 16 '25 14:10

Arslan Ali


2 Answers

Rails adds a table to your schema named schema_migrations which stores the timestamp of each migration in a single column. It doesn't show up in the schema.rb, but Rails will compare the timestamps in db/migrate and the timestamp of schema.rb to the values of the schema_migrations table to determine which migrations have run.

enter image description here

like image 50
Anthony E Avatar answered Oct 18 '25 04:10

Anthony E


Rails uses Rack middleware ActiveRecord::Migration::CheckPending to do that.

You can view all Rack middlewares Rails is using by executing rack middleware in your console.

like image 30
qcam Avatar answered Oct 18 '25 04:10

qcam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!