본문 바로가기

Web Programming/Ruby on Rails

Rake db 명령어(feat. schema:load vs migrate)

Rake DB 명령어

 

Ruby on Rails를 사용하게 되면 Rake 명령어를 접하게 되는데 '레일즈가 여러 목적으로 사용하는 범용 명령 실행 도구'로 Rails 설치 시 기본적으로 포함되어 있는 도구 모음이다. 여러 가지 명령어들이 많이 있는데 이는 'rake -T' 옵션으로 확인해볼 수 있다.

 

물론 기본적으로 제공하는 옵션 외에도 task를 만들어 실행할 수 있다. 참조

 

[Rails] Rake task 사용하기

Rake는 Ruby 개발 환경에서 사용되는 빌드 프로그램이다. Unix에서 사용되는 Make와 비슷한 용도로 사용되며 Makefile과 비슷한 Rakefile이 존재한다.

blog.bluesh.io

 

일반적으로 rake db:명령어 옵션으로 많이 활용되는데 주로 사용하거나 자주 접하게 되는 명령어 모음을 기록해두려고 한다.

 

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

The difference between rake db:migrate and rake db:reset is pretty clear in my head. The thing which I don't understand is how rake db:schema:load different from the former two. Just to be sure th...

stackoverflow.com

 

rake db:create # creates the database for the current env
rake db:create:all # creates the databases for all envs
rake db:drop drops # the database for the current env
rake db:drop:all # drops the databases for all envs

rake db:migrate # runs migrations for the current env that have not run yet
rake db:migrate:up # runs one specific migration
rake db:migrate:down # rolls back one specific migration
rake db:migrate:status # shows current migration status

rake db:rollback # rolls back the last migration
rake db:forward # advances the current schema version to the next one
rake db:seed # (only) runs the db/seed.rb file
rake db:schema:load # loads the schema into the current env's database
rake db:schema:dump # dumps the current env's schema (and seems to create the db as well)

rake db:setup # runs db:schema:load, db:seed
rake db:reset # runs db:drop db:setup
rake db:migrate:redo # runs (db:migrate:down db:migrate:up) or (db:rollback db:migrate) depending on the specified migration
rake db:migrate:reset # runs db:drop db:create db:migrate

참고: Schema:load vs  migrate 차이

rake db:schema:load 옵션의 경우, db/schema.rb 파일을 database에 로드하는 동작을 명령한다.

그러나 rake db:migrate 옵션의 경우, 동작중인 app의 database에 적용되지 않은 schema를 추적하여 migration 파일을 생성하는 작업을 명령한다.

 

일반적으로 schema:load는 아래의 경우에 사용한다.

  • You run the application for the first time.
  • When you drop the database and you need to create it again.

그러나, production 서버에 옵션을 적용하고자 할 경우, data 삭제를 완전히 완료한 후에 수행할 것을 권장한다.

 

참조