All notes

Quick Start

Rake: Ruby make.


#---------- -W, --where [PATTERN]

# To find where the rake commands come from:
rake -W db:create
# rake db:create                      /Users/me/.rvm/gems/ruby-2.2.6@me/gems/activerecord- `block in <top (required)>'
# rake db:create:all                  /Users/me/.rvm/gems/ruby-2.2.6@me/gems/activerecord- `block (2 levels) in <top (required)>'
# rake db:create_dev_company[domain]  /Users/me/works/proj/lib/tasks/populate.rake:32:in `block in <top (required)>'
# rake db:create_scim_dev_company     /Users/me/works/proj/lib/tasks/populate.rake:15:in `block in <top (required)>'

# It can also list all the tasks
rake -W db:test

#---------- Others

-T, --tasks [PATTERN]           For example: bundle exec rake -T seed
-N, --no-search, --nosearch      Do not search parent directories for the Rakefile.

-P, --prereqs                    Display the tasks and dependencies, then exit.
-n, --dry-run                    Do a dry run without executing actions.
-t, --trace=[OUT]                Turn on invoke/execute tracing, enable full backtrace. OUT can be stderr (default) or stdout.

-q, --quiet                      Do not log messages to standard output.
-s, --silent                     Like --quiet, but also suppresses the 'in directory' announcement.
-v, --verbose                    Log message to standard output.
-V, --version                    Display the program version.

-h, -H, --help



# Drop the test table:
RAILS_ENV=test rake db:drop

# Create a new test table:
RAILS_ENV=test rake db:create
# Or create all:
# rake db:create:all

# Regenerate the test db:
rake db:test:prepare
# Or
# rake db:test:prepare_with_migrate

# Load seed
bundle exec rake db:seed

# Load fixtures into test
RAILS_ENV=test bundle exec rake db:fixtures:load

#---------- All db tasks

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. I usually run this after creating and running a new migration to ensure the migration is reversable.
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 Loads the schema into the current environment’s database.
db:schema:dump Dumps the current environment’s schema to db/schema.rb.

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.

#---------- db reset

# When working on a Rails app, you might sometimes need to drop the local database and start fresh with data loaded from db/seeds.rb, what do you do?
# Short answer: use rake db:reset. This drops the database, then loads the schema with rake db:schema:load and then seeds the data with rake db:seed. This is what you should be using for the vast majority of cases.
# However, during very early stages of development, I often edit the existing migration files instead of creating new ones. This is useful when I’m still figuring out the schema, and often need to add fields to existing models.
# In such cases, we need to run db:migrate instead of db:schema:load when we reset the database. For this, we create a new task, db:reseed.
# NOTE: Once you’ve got the app in production or in team work, you should definitely not be editing migration files.

# lib/tasks/db.rake
namespace :db do
  desc 'Drop, create, migrate then seed the development database'
  task reseed: [ 'db:drop', 'db:create', 'db:migrate', 'db:seed' ] do
    puts 'Reseeding completed.'

production rake basics.

SO: change a rails to production.

RAILS_ENV=production rake about

# Create a secret_key that you can add to config/secrets.yml
RAILS_ENV=production rake secret

RAILS_ENV=production rake db:migrate
# Push js, css, image files into the /public folder
RAILS_ENV=production rake assets:precompile
RAILS_ENV=production rails s

# Or
rails s -e production


# Use "schema_migrations" table to decide which migration to run:
rake db:migrate

rake db:migrate:status

# Bring down/up specific migration.
rake db:migrate:down VERSION=20090408054532
rake db:migrate:up VERSION=20090408054532

# This will first run down part and then up part of the migration having version:
rake db:migrate:redo VERSION=20130717110623

# This will rollback last run migration(i.e. Default STEP=1)
rake db:rollback
# Rollback last n migrations
rake db:rollback STEP=4