I'd to see where this rake task is written and how it finds the db to migrate to.
-
5[Link](https://github.com/rails/rails/blob/2dfe8f1f84b7c4ff6673e7035adbeecaf31734b0/railties/lib/rails/tasks/engine.rake#L31) – ndnenkov Jun 21 '17 at 06:57
2 Answers
The db:migrate
is a rake task. db:migrate task (a built-in Rails support program) will search through your project's db/migrate directory and use the files therein to update the database's schema.
When you run db:migrate, rails will check a special table called schema_migrations which contains the time-stamp of the last migration applied to the database. it will store the times-tamps of the migration files that were already run.

- 2,639
- 1
- 19
- 34
This task is part of ActiveRecord
. You can see where it is defined here : database_tasks.rb
Basically the task call ActiveRecord::Migrator
which is the module in charge of applying migrations to the database.
For example if you create a new migration and you get the following output :
Running via Spring preloader in process 4675
invoke active_record
create db/migrate/20170621091940_create_user.rb
When you call the db:migrate
task it will call ActiveRecord::Migrator.migrate("db/migrate", 20170621091940)
and apply your migration.

- 1,691
- 11
- 15
-
Thanks @Aschen So rake can be used to do a lot of things, but rails comes with some rake tasks and one of them is db:migrate and it is just a build tool (script) that accesses the `ActiveRecord::Migrator` and then calls its `migrate` method? Is this correct? – stackjlei Jun 22 '17 at 06:44
-
Yes it's correct :-) As @hardik says it also check in the table `schema_migrations` which migrations have already been executed. – Aschen Jun 22 '17 at 08:17