Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between rake db:migrate db:reset and db:schema:load

The difference between rake db:migrate and rake db:reset is pretty clear to me. The thing which I don't understand is how rake db:schema:load is different from the former two.

Just to be sure that I am on the same page:

  • rake db:migrate - Runs the migrations which haven't been run yet.
  • rake db:reset - Clears the database (presumably does a rake db:drop + rake db:create + rake db:migrate) and runs migration on a fresh database.
like image 692
Gaurav Agarwal Avatar asked Apr 24 '12 16:04

Gaurav Agarwal


People also ask

What is db Migrate?

A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined.

What is Rails db Migrate?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.

What is schema RB?

The schema. rb serves mainly two purposes: It documents the final current state of the database schema. Often, especially when you have more than a couple of migrations, it's hard to deduce the schema just from the migrations alone. With a present schema.


2 Answers

  • db:migrate runs (single) migrations that have not run yet.

  • db:create creates the database

  • db:drop deletes the database

  • db:schema:load creates tables and columns within the existing database following schema.rb. This will delete existing data.

  • db:setup does db:create, db:schema:load, db:seed

  • db:reset does db:drop, db:setup

  • db:migrate:reset does db:drop, db:create, db:migrate

Typically, you would use db:migrate after having made changes to the schema via new migration files (this makes sense only if there is already data in the database). db:schema:load is used when you setup a new instance of your app.

I hope that helps.


UPDATE for rails 3.2.12:

I just checked the source and the dependencies are like this now:

  • db:create creates the database for the current env

  • db:create:all creates the databases for all envs

  • db:drop drops the database for the current env

  • db:drop:all drops the databases for all envs

  • db:migrate runs migrations for the current env that have not run yet

  • db:migrate:up runs one specific migration

  • db:migrate:down rolls back one specific migration

  • db:migrate:status shows current migration status

  • db:rollback rolls back the last migration

  • db:forward advances the current schema version to the next one

  • db:seed (only) runs the db/seed.rb file

  • db:schema:load loads the schema into the current env's database

  • db:schema:dump dumps the current env's schema (and seems to create the db as well)

  • db:setup runs db:create db:schema:load db:seed

  • db:reset runs db:drop db:setup

  • db:migrate:redo runs (db:migrate:down db:migrate:up) or (db:rollback db:migrate) depending on the specified migration

  • db:migrate:reset runs db:drop db:create db:migrate

For further information please have a look at https://github.com/rails/rails/blob/v3.2.12/activerecord/lib/active_record/railties/databases.rake (for Rails 3.2.x) and https://github.com/rails/rails/blob/v4.0.5/activerecord/lib/active_record/railties/databases.rake (for Rails 4.0.x)

like image 105
moritz Avatar answered Sep 29 '22 06:09

moritz


TLDR

Use

  • rake db:migrate If you wanna make changes to the schema
  • rake db:reset If you wanna drop the database, reload the schema from schema.rb, and reseed the database
  • rake db:schema:load If you wanna reset database to schema as provided in schema.rb (This will delete all data)

Explanations

rake db:schema:load will set up the schema as provided in schema.rb file. This is useful for a fresh install of app as it doesn't take as much time as db:migrate

Important note, db:schema:load will delete data on server.

rake db:migrate makes changes to the existing schema. Its like creating versions of schema. db:migrate will look in db/migrate/ for any ruby files and execute the migrations that aren't run yet starting with the oldest. Rails knows which file is the oldest by looking at the timestamp at the beginning of the migration filename. db:migrate comes with a benefit that data can also be put in the database. This is actually not a good practice. Its better to use rake db:seed to add data.

rake db:migrate provides tasks up, down etc which enables commands like rake db:rollback and makes it the most useful command.

rake db:reset does a db:drop and db:setup
It drops the database, create it again, loads the schema, and initializes with the seed data

Relevant part of the commands from databases.rake


namespace :schema do   desc 'Creates a db/schema.rb file that is portable against any DB supported by Active Record'   task :dump => [:environment, :load_config] do     require 'active_record/schema_dumper'     filename = ENV['SCHEMA'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema.rb')     File.open(filename, "w:utf-8") do |file|       ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)     end     db_namespace['schema:dump'].reenable   end    desc 'Loads a schema.rb file into the database'   task :load => [:environment, :load_config, :check_protected_environments] do     ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV['SCHEMA'])   end 

  # desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'   task :reset => [ 'db:drop', 'db:setup' ] 

namespace :migrate do   # desc  'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'   task :redo => [:environment, :load_config] do     if ENV['VERSION']       db_namespace['migrate:down'].invoke       db_namespace['migrate:up'].invoke     else       db_namespace['rollback'].invoke       db_namespace['migrate'].invoke     end   end 
like image 37
sudo bangbang Avatar answered Sep 29 '22 06:09

sudo bangbang