railsとrakeコマンドの違いについて
目次
結論から申し上げますと
Rails 4系までは、このコマンドはrailsから実行する、このコマンドはrakeから実行する
という形で分かれていたのですが、Rails 5系からはrakeで実行していたコマンドもrailsで実行できるようになりました。
ですので、5系以降のシステムではrailsコマンドのみ使用するという認識で問題ないのですが
4系ではどのコマンドでrakeを用いる必要があったのかをまとめてみようと思います。
ちなみにrakeとはmakeと似た機能でRubyで書かれたビルドツールのことです。
参考:https://docs.ruby-lang.org/ja/2.7.0/library/rake.html
確認環境
1 2 |
ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-linux] Rails 7.0.4.2 |
rakeコマンド
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
$rake -T rake about # List versions of all Rails frameworks and the environment rake action_mailbox:ingress:exim # Relay an inbound email from Exim to Action Mailbox (URL and INGRESS_PASSWORD required) rake action_mailbox:ingress:postfix # Relay an inbound email from Postfix to Action Mailbox (URL and INGRESS_PASSWORD required) rake action_mailbox:ingress:qmail # Relay an inbound email from Qmail to Action Mailbox (URL and INGRESS_PASSWORD required) rake action_mailbox:install # Installs Action Mailbox and its dependencies rake action_mailbox:install:migrations # Copy migrations from action_mailbox to application rake action_text:install # Copy over the migration, stylesheet, and JavaScript files rake action_text:install:migrations # Copy migrations from action_text to application rake active_storage:install # Copy over the migration needed to the application rake app:template # Applies the template supplied by LOCATION=(/path/to/template) or URL rake app:update # Update configs and some other initially generated files (or use just update:configs or... rake assets:clean[keep] # Remove old compiled assets rake assets:clobber # Remove compiled assets rake assets:environment # Load asset compile environment rake assets:precompile # Compile all the assets named in config.assets.precompile rake cache_digests:dependencies # Lookup first-level dependencies for TEMPLATE (like messages/show or comments/_comment.... rake cache_digests:nested_dependencies # Lookup nested dependencies for TEMPLATE (like messages/show or comments/_comment.html) rake db:create # Creates the database from DATABASE_URL or config/database.yml for the current RAILS_EN... rake db:drop # Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV ... rake db:encryption:init # Generate a set of keys for configuring Active Record encryption in a given environment rake db:environment:set # Set the environment value for the database rake db:fixtures:load # Loads fixtures into the current environment's database rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog) rake db:migrate:down # Runs the "down" for a given migration VERSION rake db:migrate:redo # Rolls back the database one migration and re-migrates up (options: STEP=x, VERSION=x) rake db:migrate:status # Display status of migrations rake db:migrate:up # Runs the "up" for a given migration VERSION rake db:prepare # Runs setup if database does not exist, or runs migrations if it does rake db:reset # Drops and recreates all databases from their schema for the current environment and lo... rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n) rake db:schema:cache:clear # Clears a db/schema_cache.yml file rake db:schema:cache:dump # Creates a db/schema_cache.yml file rake db:schema:dump # Creates a database schema file (either db/schema.rb or db/structure.sql, depending on ... rake db:schema:load # Loads a database schema file (either db/schema.rb or db/structure.sql, depending on `E... rake db:seed # Loads the seed data from db/seeds.rb rake db:seed:replant # Truncates tables of each database for current environment and loads the seeds rake db:setup # Creates all databases, loads all schemas, and initializes with the seed data (use db:r... rake db:version # Retrieves the current schema version number rake importmap:install # Setup Importmap for the app rake log:clear # Truncates all/specified *.log files in log/ to zero bytes (specify which logs with LOG... rake middleware # Prints out your Rack middleware stack rake restart # Restart app by touching tmp/restart.txt rake secret # Generate a cryptographically secure secret key (this is typically used to generate a s... rake stats # Report code statistics (KLOCs, etc) from the application or engine rake stimulus:install # Install Stimulus into the app rake stimulus:install:importmap # Install Stimulus on an app running importmap-rails rake stimulus:install:node # Install Stimulus on an app running node rake test # Runs all tests in test folder except system ones rake test:all # Runs all tests, including system tests rake test:db # Run tests quickly, but also reset db rake test:system # Run system tests only rake time:zones[country_or_offset] # List all time zones, list by two-letter country code (`bin/rails time:zones[US]`), or ... rake tmp:clear # Clear cache, socket and screenshot files from tmp/ (narrow w/ tmp:cache:clear, tmp:soc... rake tmp:create # Creates tmp directories for cache, sockets, and pids rake turbo:install # Install Turbo into the app rake turbo:install:importmap # Install Turbo into the app with asset pipeline rake turbo:install:node # Install Turbo into the app with webpacker rake turbo:install:redis # Switch on Redis and use it in development rake yarn:install # Install all JavaScript dependencies as specified via Yarn rake zeitwerk:check # Checks project structure for Zeitwerk compatibility |
railsコマンド
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
$rails -T rails about # List versions of all Rails frameworks and the environment rails action_mailbox:ingress:exim # Relay an inbound email from Exim to Action Mailbox (URL and INGRESS_PASSWORD required) rails action_mailbox:ingress:postfix # Relay an inbound email from Postfix to Action Mailbox (URL and INGRESS_PASSWORD requi... rails action_mailbox:ingress:qmail # Relay an inbound email from Qmail to Action Mailbox (URL and INGRESS_PASSWORD required) rails action_mailbox:install # Installs Action Mailbox and its dependencies rails action_mailbox:install:migrations # Copy migrations from action_mailbox to application rails action_text:install # Copy over the migration, stylesheet, and JavaScript files rails action_text:install:migrations # Copy migrations from action_text to application rails active_storage:install # Copy over the migration needed to the application rails app:template # Applies the template supplied by LOCATION=(/path/to/template) or URL rails app:update # Update configs and some other initially generated files (or use just update:configs o... rails assets:clean[keep] # Remove old compiled assets rails assets:clobber # Remove compiled assets rails assets:environment # Load asset compile environment rails assets:precompile # Compile all the assets named in config.assets.precompile rails cache_digests:dependencies # Lookup first-level dependencies for TEMPLATE (like messages/show or comments/_comment... rails cache_digests:nested_dependencies # Lookup nested dependencies for TEMPLATE (like messages/show or comments/_comment.html) rails db:create # Creates the database from DATABASE_URL or config/database.yml for the current RAILS_E... rails db:drop # Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV... rails db:encryption:init # Generate a set of keys for configuring Active Record encryption in a given environment rails db:environment:set # Set the environment value for the database rails db:fixtures:load # Loads fixtures into the current environment's database rails db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog) rails db:migrate:down # Runs the "down" for a given migration VERSION rails db:migrate:redo # Rolls back the database one migration and re-migrates up (options: STEP=x, VERSION=x) rails db:migrate:status # Display status of migrations rails db:migrate:up # Runs the "up" for a given migration VERSION rails db:prepare # Runs setup if database does not exist, or runs migrations if it does rails db:reset # Drops and recreates all databases from their schema for the current environment and l... rails db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n) rails db:schema:cache:clear # Clears a db/schema_cache.yml file rails db:schema:cache:dump # Creates a db/schema_cache.yml file rails db:schema:dump # Creates a database schema file (either db/schema.rb or db/structure.sql, depending on... rails db:schema:load # Loads a database schema file (either db/schema.rb or db/structure.sql, depending on `... rails db:seed # Loads the seed data from db/seeds.rb rails db:seed:replant # Truncates tables of each database for current environment and loads the seeds rails db:setup # Creates all databases, loads all schemas, and initializes with the seed data (use db:... rails db:version # Retrieves the current schema version number rails importmap:install # Setup Importmap for the app rails log:clear # Truncates all/specified *.log files in log/ to zero bytes (specify which logs with LO... rails middleware # Prints out your Rack middleware stack rails restart # Restart app by touching tmp/restart.txt rails secret # Generate a cryptographically secure secret key (this is typically used to generate a ... rails stats # Report code statistics (KLOCs, etc) from the application or engine rails stimulus:install # Install Stimulus into the app rails stimulus:install:importmap # Install Stimulus on an app running importmap-rails rails stimulus:install:node # Install Stimulus on an app running node rails test # Runs all tests in test folder except system ones rails test:all # Runs all tests, including system tests rails test:db # Run tests quickly, but also reset db rails test:system # Run system tests only rails time:zones[country_or_offset] # List all time zones, list by two-letter country code (`bin/rails time:zones[US]`), or... rails tmp:clear # Clear cache, socket and screenshot files from tmp/ (narrow w/ tmp:cache:clear, tmp:so... rails tmp:create # Creates tmp directories for cache, sockets, and pids rails turbo:install # Install Turbo into the app rails turbo:install:importmap # Install Turbo into the app with asset pipeline rails turbo:install:node # Install Turbo into the app with webpacker rails turbo:install:redis # Switch on Redis and use it in development rails yarn:install # Install all JavaScript dependencies as specified via Yarn rails zeitwerk:check # Checks project structure for Zeitwerk compatibility |
コマンドの詳細については本家のドキュメントをご参考に
参考:https://railsdoc.com/rails
実行コマンドについて
1 2 3 |
rails db:migrate bin/rails db:migrate bundle exec rails db:migrate |
rake/railsの実行コマンドは
Railsアプリケーションのルートディレクトリ直下で
上記のコマンドのどれかを実行するかと思いますが
挙動としては全て同じになります。
仕組みは以下となります。
1.bin/railsはconfig/bootを読み込んでいる
※ bin/railsファイルの中身
1 2 3 4 |
#!/usr/bin/env ruby APP_PATH = File.expand_path("../config/application", __dir__) require_relative "../config/boot" require "rails/commands" |
railsコマンドはbin配下のディレクトリに存在していれば
bin/railsコマンドを実行するということで
1 2 |
rails bin/rails |
が同じ意味合いとなります。
2.config/bootではbundler/setupを読み込んでいる
3.bundler/setupが実行されるとbundle execと同じことが実行される
上記の事から
1 2 |
bin/rails bundle exec rails |
が同じという事が分かり、全てが同じ意味合いのコマンドという事が分かります。
こう読み解くと分かりやすいですね。