I have a full development of Ruby on Rails database data. I want to delete all content and rebuild the database. I am considering using something like:
rake db:recreate
is it possible?
#1st Floor
I today I do a lot of rails architecture changes. I realized I needed to add two additional models in the hierarchy, while others will need to remove the model. Model and controller requires many small changes.
I added two new models and create them using the following method:
rake db:migrate
I then edited the schema.rb file. I manually deleted the old model is no longer needed, change the foreign key fields as needed, just reordered a bit, let me be more clear. I deleted all the migration, and then run the build again in the following ways:
rake db:reset
It works very well. Of course, all data must be reloaded. Rails realize that migration has been deleted and resets the high water mark:
-- assume_migrated_upto_version(20121026094813, ["/Users/sean/rails/f4/db/migrate"])
#2nd Floor
You can use the following command line:
rake db:drop db:create db:migrate db:seed db:test:clone
#3rd floor
According Rails guidelines , you should use this one schema.rb
because it will from schema.rb
the migration file is loaded instead of one by one reload:
rake db:reset
#4th floor
You can do it manually:
rake db:drop
rake db:create
rake db:migrate
Or just rake db:reset
, it will run the above steps, but will also run the db/seeds.rb
file.
An additional nuance is rake db:reset
directly schema.rb
load the file, instead of running all the migration file again.
In all cases, your data will be blown away.
#5th Floor
To delete a specific database, you can do this on the rails console:
$rails console
Loading development environment
1.9.3 > ActiveRecord::Migration.drop_table(:<table_name>)
1.9.3 > exit
Then again migrate DB
$bundle exec rake db:migrate
#6th floor
Run from the command line
rake db:migrate:reset
#7th floor
On Rails 4, are all needed
$ rake db:schema:load
This will delete all content on the database, and recreate the pattern from schema.rb files, without having to individually apply all migration.
Building # 8
Simply issue the following steps: Remove the database, and then re-create the database, data migration, if the seed is sown database:
rake db:drop db:create db:migrate db:seed
Since the rake
default setting is to develop , if you see an exception in the spec test, you should to test re-create the db environment, as follows:
RAILS_ENV=test rake db:drop db:create db:migrate
In most cases, the test database is sown during the test, so no passing db:seed
task operation. Otherwise, you should prepare a database:
rake db:test:prepare
Or
RAILS_ENV=test rake db:seed
In addition, we want to use to re-create the task, you can add the following code to Rakefile in:
namespace :db do
task :recreate => [ :drop, :create, :migrate ] do
if ENV[ 'RAILS_ENV' ] !~ /test|cucumber/
Rake::Task[ 'db:seed' ].invoke
end
end
end
Then issue:
rake db:recreate
House # 9
With like
rake db:drop db:create db:migrate db:seed
All in a line. This is faster because the environment will not be reloaded again and again.
db: drop - will discard database.
db: the Create - Create the database (host / db / password will be taken from the config / database.yml)
db: the migrate - from the directory (db / Migration / .rb) * Run existing migration.
db: the SEED - from the directory (db / migration / seed.rb) run seed data.
I usually like:
rake db:reset
Time to do all.
Cheers!
#10th floor
Update: Rails 5, you can access this command using the following command:
rails db:purge db:create db:migrate RAILS_ENV=test
The latest 4.2 version of the rails from the start, you can now run:
rake db:purge
Source: submitted
# desc "Empty the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config). Without RAILS_ENV it defaults to purging the development and test databases."
task :purge => [:load_config] do
ActiveRecord::Tasks::DatabaseTasks.purge_current
end
It may be used together as mentioned above:
rake db:purge db:create db:migrate RAILS_ENV=test
House # 11
On the rails 4.2, delete all the data but keep the database
$ bin/rake db:purge && bin/rake db:schema:load
https://github.com/rails/rails/blob/4-2-stable/activerecord/CHANGELOG.md
House # 12
Because during development, you always want to re-create the database, you can define a rake task in lib / tasks folder.
namespace :db do
task :all => [:environment, :drop, :create, :migrate] do
end
end
In the end you will run
rake db:all
It will rebuild your database
House # 13
I think the best way to run this command is:
**rake db:reset** it does db:drop, db:setup
rake db:setup does db:create, db:schema:load, db:seed
House # 14
Depending on your needs, you can use ......
rake db:create
... from config/database.yml
scratch to build config/database.yml
, or ......
rake db:schema:load
... From the schema.rb
file from scratch to build the database.
House # 15
I use the following in a liner terminal.
$ rake db:drop && rake db:create && rake db:migrate && rake db:schema:dump && rake db:test:prepare
I use it as a shell aliases and nameremigrate
Until now, you can easily "link" Rails tasks:
$ rake db:drop db:create db:migrate db:schema:dump db:test:prepare # db:test:prepare no longer available since Rails 4.1.0.rc1+
House # 16
I know that there are two ways to do this:
This will reset your database and use all reload the current schema:
rake db:reset db:migrate
This will destroy your database, then create it, then migrate your current mode:
rake db:drop db:create db:migrate
All data will be lost in both cases.
House # 17
You can use db:reset
- for running db: drop and db: setup or db:migrate:reset
- run db: drop, db: create and db: migrate.
It depends on what you want to use exists schema.rb
House # 18
You can just run
rake db:setup
If you use certain data to create a seed file, it will delete the database, create a new database and populate the database from seed.
House # 19
3 option, the same result:
1. All the steps:
$ rake db:drop # deletes the database for the current env
$ rake db:create # creates the database for the current env
$ rake db:schema:load # loads the schema already generated from schema.rb / erases data
$ rake db:seed # seed with initial data
2. Reset:
$ rake db:reset # drop / schema:load / seed
3. Migration: Reset:
$ rake db:migrate:reset # drop / create / migrate
$ rake db:seed
notes:
- If you use a schema: load all migrate faster than execution, but the same result.
- All data will be lost.
- You can run multiple rake in a row.
- 3 applies to the guide rail.