Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the current "version number" of migrations?

I have a bunch of migrations, but due to some weird reason, rails suddenly forgot that it has ran most of these already. Now when I try to run rake db:migrate, rails will try to run ALL of the migrations.

How do I set the current "version number" or whatever it's called so that rails knows that "oh I already ran the first 5 migrations, I should start from the 6th one"

like image 228
Razor Storm Avatar asked Dec 24 '11 21:12

Razor Storm


People also ask

What are active record migrations?

Migrations are a feature of Active Record that allows you to evolve your database schema over time. Rather than write schema modifications in pure SQL, migrations allow you to use a Ruby DSL to describe changes to your tables.

How does rails keep track of migrations?

Every migration file that Rails generates has a timestamp that is present in the filename. This timestamp is important and is used by Rails to confirm whether a migration has run or not, as we'll see later. The migration contains a class that inherits from ActiveRecord::Migration[6.0] .

How do I rollback migration in Rails?

You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.


1 Answers

The schema_migrations table stores the numbers for all the migrations that have been run (just the numeric part). If you fill it with all the migration numbers that you think have already run rails shouldn't try to run them again.

like image 159
Frederick Cheung Avatar answered Oct 03 '22 19:10

Frederick Cheung