Difference between rake db:migrate db:reset and db:schema:load
Solution 1
-
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.
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)
Solution 2
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 fromschema.rb
, and reseed the database -
rake db:schema:load
If you wanna reset database to schema as provided inschema.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
Solution 3
Rails 5
db:create
- Creates the database for the current RAILS_ENV environment. If RAILS_ENV is not specified it defaults to the development and test databases.
db:create:all
- Creates the database for all environments.
db:drop
- Drops the database for the current RAILS_ENV environment. If RAILS_ENV is not specified it defaults to the development and test databases.
db:drop:all
- Drops the database for all environments.
db:migrate
- Runs migrations for the current environment that have not run yet. By default it will run migrations only in the development environment.
db:migrate:redo
- Runs db:migrate:down and db:migrate:up or db:migrate:rollback and db:migrate:up depending on the specified migration.
db:migrate:up
- Runs the up for the given migration VERSION.
db:migrate:down
- Runs the down for the given migration VERSION.
db:migrate:status
- Displays the current migration status.
db:migrate:rollback
- Rolls back the last migration.
db:version
- Prints the current schema version.
db:forward
- Pushes the schema to the next version.
db:seed
- Runs the db/seeds.rb file.
db:schema:load
Recreates the database from the schema.rb file. Deletes existing data.
db:schema:dump
Dumps the current environment’s schema to db/schema.rb.
db:structure:load
- Recreates the database from the structure.sql file.
db:structure:dump
- Dumps the current environment’s schema to db/structure.sql.
(You can specify another file with SCHEMA=db/my_structure.sql
)
db:setup
Runs db:create, db:schema:load and db:seed.
db:reset
Runs db:drop and db:setup.
db:migrate:reset
- Runs db:drop, db:create and db:migrate.
db:test:prepare
- Check for pending migrations and load the test schema. (If you run rake without any arguments it will do this by default.)
db:test:clone
- Recreate the test database from the current environment’s database schema.
db:test:clone_structure
- Similar to db:test:clone, but it will ensure that your test database has the same structure, including charsets and collations, as your current environment’s database.
db:environment:set
- Set the current RAILS_ENV environment in the ar_internal_metadata table. (Used as part of the protected environment check.)
db:check_protected_environments
- Checks if a destructive action can be performed in the current RAILS_ENV environment. Used internally when running a destructive action such as db:drop or db:schema:load.
Solution 4
List all tasks
You can find all the database tasks Rails provides by running the following command from a Rails application directory.
➜ blog (main) ✗ bin/rails help | grep db:
db:create
db:drop
...
Summary
db:create: Creates the database unless it already exists.
db:drop: Drops the database if it exists.
db:environment:set: Fixes the EnvironmentMismatchError
or NoEnvironmentInSchemaError
, raised if the environment data is not found in the schema, by setting the current environment in the internal table.
db:fixtures:load: It loads the fixtures, i.e., the sample data that you want to test against. They are stored in the YAML file under the test/fixtures/
directory.
db:migrate: Runs all the migrations that have not run yet, for the current environment.
db:migrate:down: Reverts the transformations performed by the last migration's up
method by running the down
method.
db:migrate:redo: Rolls back the database one migration and re-migrates up.
db:migrate:status: Displays the status of migrations.
db:migrate:up: Runs the up
method for a given migration.
db:prepare: Runs setup
if the database does not exist. Otherwise, it runs the migrations.
db:reset: Resets your database using your migrations for the current environment. It does this by running the db:drop
, db:create
, db:migrate
tasks.
db:rollback: Rolls the schema back to the previous version, undoing the migration that you just ran. If you want to undo previous n
migrations, pass STEP=n
to this task.
db:schema:cache:clear: Clears the db/schema_cache.yml file generated by the db:schema:cache:dump
task.
db:schema:cache:dump: Creates a db/schema_cache.yml file.
db:schema:dump: Creates a database schema file (either db/schema.rb
or db/structure.sql
, depending on config.active_record.schema_format
).
db:schema:load: Loads a database schema file (either db/schema.rb
or db/structure.sql
, depending on config.active_record.schema_format
) into the database.
db:seed: Loads the seed data from db/seeds.rb
file.
db:seed:replant: Truncates tables of each database for the current environment and loads the seeds
db:setup: Creates all databases db:create
, loads all schemas db:schema:load
, and initializes with the seed data db:seed
. However, it won't drop the database first if it exists. Use db:reset
to also drop all databases first.
db:structure:dump: Deprecated. It was used to dump the structure.sql
file.
db:structure:load: Deprecated. It was used to load the structure.sql
file.
For some history behind why these tasks were deprecated, check out this wtf. No, seriously.
db:system:change: Running rails new
generator without specifying a database sets your app with sqlite
. It's a hassle to change the database later. This task helps you easily change the database by delegating to the rails db:change SYSTEM=postgresql|mysql|whatever
generator.
db:version: Prints the current schema version number.
Source: All the Database Tasks in Rails
Solution 5
As far as I understand, it is going to drop your database and re-create it based on your db/schema.rb
file. That is why you need to make sure that your schema.rb
file is always up to date and under version control.
Gaurav Agarwal
I run https://agarwalconsulting.io. You can find me @ https://algogrit.com.
Updated on February 20, 2022Comments
-
Gaurav Agarwal about 2 years
The difference between
rake db:migrate
andrake db:reset
is pretty clear to me. The thing which I don't understand is howrake 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 arake db:drop
+rake db:create
+rake db:migrate
) and runs migration on a fresh database.
-
cutalion about 12 yearsHere is the file with answers :) - github.com/rails/rails/blob/master/activerecord/lib/…
-
moritz about 12 years@cutation: db:setup does surely not run db:migrate, because it would be much too brittle to run all migrations just for a db setup (this is what schema.rb is for).
-
Alejandro Riedel over 11 yearsI'm executing db:reset and it's seeding my db. Why could it be?
-
Dan over 10 yearsdb:setup also runs
db:create
if necessary. At least as of rails 4.0.2. -
Pooyan Khosravi over 9 yearsRails 4 will perform
rake db:migrate
when callingrake db:setup
if there are pending migrations but won't execute pending migrations. -
Fred Willmore almost 8 yearsAs of Rails 5, you can run rake tasks with the
rails
keyword - egrails db:migrate
-
PhilT over 7 yearsPlease quote relevant parts of the article in case it is removed. Don't suggest doing something without explaining why.
-
Admin over 7 yearsSo if you create your production schema using db:schema:load(created from a series of previous migrations), will rake know which migrations(the ones which took part in creating the initial schema.rb) do not need to be run on future invocations of db:migrate?
-
ian over 4 yearsI just ran
rake db:setup
for a new Rails v6.0.0 project and got the message "db/schema.rb doesn't exist yet. Runrails db:migrate
to create it, then try again", so it seems it doesn't rundb:migrate
for this version. -
Chris Scott about 4 yearsWas not aware of
db:migrate:reset
, and it ended up being exactly what I needed. Thanks! -
Ridhwaan Shakeel over 3 yearsdoes
db:setup
also create the users/roles? -
jshah over 3 yearsjust fyi:
db:setup
will drop your local development database.db:setup
ends up runningdb:schema:load_if_ruby
which runsdb:schema:load
. This reloads the current schema that purges all your current data in the database. -
stevec about 3 yearsPlease consider adding the newer answer to the top, and older answer to the bottom, as rails 3.x is quite old now
-
Hellfar about 3 yearsSeems that
db:setup
also do adb:drop
?? My db got "reset" every time I do adb:setup
:/ ...