Ruby on Rails 데이터베이스 제거 또는 재생성
데이터로 가득 찬 dev Ruby on Rails 데이터베이스가 있습니다. 모든 것을 삭제하고 데이터베이스를 다시 만들고 싶습니다. 다음과 같은 것을 사용할 생각입니다.
rake db:recreate
이것이 가능한가?
이 작업을 수행하는 두 가지 방법을 알고 있습니다.
이렇게하면 데이터베이스가 재설정되고 현재 스키마가 모두 다시로드됩니다.
rake db:reset db:migrate
그러면 db가 파괴되고 생성 된 다음 현재 스키마가 마이그레이션됩니다.
rake db:drop db:create db:migrate
두 시나리오 모두에서 모든 데이터가 손실됩니다.
Rails 4에서 필요한 모든 것은
$ rake db:schema:load
이렇게하면 모든 마이그레이션을 하나씩 적용 할 필요없이 DB의 전체 내용이 삭제되고 schema.rb 파일에서 스키마가 다시 생성됩니다.
나는 터미널에서 다음과 같은 하나의 라이너를 사용합니다.
$ rake db:drop && rake db:create && rake db:migrate && rake db:schema:dump && rake db:test:prepare
나는 이것을 셸 별칭으로 넣고 이름을 지정했습니다. remigrate
이제 Rails 작업을 쉽게 "체인"할 수 있습니다.
$ 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+
업데이트 : Rails 5에서는 다음 명령을 통해이 명령에 액세스 할 수 있습니다.
rails db:purge db:create db:migrate RAILS_ENV=test
최신 Rails 4.2 릴리스부터 다음을 실행할 수 있습니다.
rake db:purge
출처 : commit
# 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
위에서 언급 한 것처럼 함께 사용할 수 있습니다.
rake db:purge db:create db:migrate RAILS_ENV=test
원하는 항목에 따라 다음을 사용할 수 있습니다.
rake db:create
… 처음부터 데이터베이스를 구축하려면 config/database.yml
, 또는…
rake db:schema:load
… schema.rb
파일 에서 처음부터 데이터베이스를 구축 합니다.
일련의 단계를 실행하십시오. 데이터베이스를 삭제 한 다음 다시 생성하고 데이터를 마이그레이션하고 시드가있는 경우 데이터베이스를 심습니다.
rake db:drop db:create db:migrate db:seed
의 기본 환경 rake
은 development 이므로 사양 테스트에서 예외가 발견 되면 다음과 같이 테스트 환경에 대한 db를 다시 만들어야 합니다.
RAILS_ENV=test rake db:drop db:create db:migrate
대부분의 경우 테스트 데이터베이스는 테스트 절차 중에 파종되므로 db:seed
작업 조치를 통과 할 필요가 없습니다. 그렇지 않으면 데이터베이스를 준비해야합니다.
rake db:test:prepare
또는
RAILS_ENV=test rake db:seed
Additionally, to use the recreate task you can add into Rakefile the following code:
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
From the command line run
rake db:migrate:reset
Use like
rake db:drop db:create db:migrate db:seed
All in one line. This is faster since the environment doesn't get reloaded again and again.
db:drop - will drop database.
db:create - will create database (host/db/password will be taken from config/database.yml)
db:migrate - will run existing migrations from directory (db/migration/.rb)*.
db:seed - will run seed data possible from directory (db/migration/seed.rb)..
I usually prefer:
rake db:reset
to do all at once.
Cheers!
You can manually do:
rake db:drop
rake db:create
rake db:migrate
Or just rake db:reset
, which will run the above steps but will also run your db/seeds.rb
file.
An added nuance is that rake db:reset
loads directly from your schema.rb
file as opposed to running all the migrations files again.
You data gets blown away in all cases.
You can use this following command line:
rake db:drop db:create db:migrate db:seed db:test:clone
To drop a particular database, you can do this on rails console:
$rails console
Loading development environment
1.9.3 > ActiveRecord::Migration.drop_table(:<table_name>)
1.9.3 > exit
And then migrate DB again
$bundle exec rake db:migrate
On rails 4.2, to remove all data but preserve the database
$ bin/rake db:purge && bin/rake db:schema:load
https://github.com/rails/rails/blob/4-2-stable/activerecord/CHANGELOG.md
You can use db:reset
- for run db:drop and db:setup or db:migrate:reset
- which runs db:drop, db:create and db:migrate.
dependent at you want to use exist schema.rb
According to Rails guide, this one liner should be used because it would load from the schema.rb
instead of reloading the migration files one by one:
rake db:reset
Because in development , you will always want to recreate the database,you can define a rake task in your lib/tasks folder like that.
namespace :db do
task :all => [:environment, :drop, :create, :migrate] do
end
end
and in terminal you will run
rake db:all
it will rebuild your database
I think the best way to run this command:
**rake db:reset** it does db:drop, db:setup
rake db:setup does db:create, db:schema:load, db:seed
Simply you can run
rake db:setup
It will drop database, create new database and populate db from seed if you created seed file with some data.
3 options, same result:
1. All 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. Migrate:reset:
$ rake db:migrate:reset # drop / create / migrate
$ rake db:seed
Notes:
- If schema:load is used is faster than doing all migrations, but same result.
- All data will be lost.
- You can run multiple rakes in one line.
- Works with rails 3.
I've today made quite a few changes to my rails schema. I realised I needed an additional two models in a hierarchy and some others to be deleted. There were many little changes required to the models and controllers.
I added the two new models and created them, using:
rake db:migrate
Then I edited the schema.rb file. I manually removed the old models that were no longer required, changed the foreign key field as required and just reordered it a bit to make it clearer to me. I deleted all the migrations, and then re-ran the build via:
rake db:reset
It worked perfectly. All the data has to be reloaded, of course. Rails realised the migrations had been deleted and reset the high-water mark:
-- assume_migrated_upto_version(20121026094813, ["/Users/sean/rails/f4/db/migrate"])
참고URL : https://stackoverflow.com/questions/4116067/purge-or-recreate-a-ruby-on-rails-database
'Nice programing' 카테고리의 다른 글
UTF-8로 인코딩 된 NSData를 NSString으로 변환 (0) | 2020.10.03 |
---|---|
jQuery로 왼쪽과 오른쪽 마우스 클릭을 구별하는 방법 (0) | 2020.10.03 |
PNG, GIF, JPEG, SVG의 다른 사용 사례는 무엇입니까? (0) | 2020.10.03 |
.NET을 사용하여 16 진수 색상 코드에서 색상을 얻으려면 어떻게합니까? (0) | 2020.10.03 |
속성 이름을 포함하는 변수로 개체 속성이 있는지 확인하는 방법은 무엇입니까? (0) | 2020.10.02 |