Skip to content

Instantly share code, notes, and snippets.

@stevenyap
Last active October 18, 2025 14:39
Show Gist options
  • Select an option

  • Save stevenyap/7038932 to your computer and use it in GitHub Desktop.

Select an option

Save stevenyap/7038932 to your computer and use it in GitHub Desktop.

Revisions

  1. stevenyap revised this gist Feb 20, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Rake Database.md
    Original file line number Diff line number Diff line change
    @@ -88,14 +88,14 @@ Roll back the last migration
    rake db:rollback
    ```

    Or you can specify how many steps (n) to rollback
    Or you can specify how many steps (n) to rollback. (Specify STEP=9999 to rollback everything)
    ```sh
    rake db:rollback step=n
    rake db:rollback STEP=n
    ```

    Or you can specfiy exactly the version of migration to rollback to
    ```sh
    rake db:migrate:down version=847583457438957
    rake db:migrate:down VERSION=847583457438957
    ```

    Or to simply rerun last migration
  2. stevenyap revised this gist Jan 3, 2014. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion Rake Database.md
    Original file line number Diff line number Diff line change
    @@ -103,4 +103,11 @@ Or to simply rerun last migration
    rake db:migrate:redo
    ```

    **Tip: If you are changing the latest migration, you should rake db:rollback and then add your new code to the migration file and then run rake db:migrate again**
    **Tip: If you are changing the latest migration, you should rake db:rollback and then add your new code to the migration file and then run rake db:migrate again**

    ## Other database commands

    ```sh
    # View all migration logs
    rake db:migrate:status
    ```
  3. stevenyap revised this gist Oct 18, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Rake Database.md
    Original file line number Diff line number Diff line change
    @@ -83,6 +83,7 @@ rake db:seed
    ```

    ## Roll back migration
    Roll back the last migration
    ```sh
    rake db:rollback
    ```
    @@ -102,4 +103,4 @@ Or to simply rerun last migration
    rake db:migrate:redo
    ```

    **Tip: If you are changing the latest migration, you should rake db:rollback and then add your new code and then run rake db:migrate**
    **Tip: If you are changing the latest migration, you should rake db:rollback and then add your new code to the migration file and then run rake db:migrate again**
  4. stevenyap revised this gist Oct 18, 2013. 1 changed file with 25 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions Rake Database.md
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ end
    ## Run Migrate
    - You need to run migration before ROR actually creates it in the database
    - *You must run this everytime there is a new migration*

    ```sh
    rake db:migrate
    ```
    @@ -36,6 +37,7 @@ rake db:migrate
    - This should be ran after rake db:migrate
    - This duplicates your schema into the test database so that your test cases will work
    - Common error in test cases when you forget this step

    ```sh
    rake db:test:prepare
    ```
    @@ -47,11 +49,34 @@ rake db:drop

    ## Chaining commands
    - You can chain commands together for faster typing

    ```sh
    rake db:drop db:create db:migration db:test:prepare
    ```


    ## Seeding the database
    - Note that seeding of database should be idempotent (running it multiple times does not create duplicated records)
    - Add your ruby commands to add data in /db/seeds.rb like below:

    ```ruby
    puts "** Seeding Database: seeding ***\n\n"

    unless User.where(email: "admin@example.com").first
    User.create!(email: 'admin@example.com',
    password: '123123',
    password_confirmation: '123123')
    puts "-- Created admin@example.com with password 123123\n"
    end

    unless Company.where(name: "ABC").first
    Company.create!(name: "ABC")
    puts "-- Created company: ABC\n"
    end

    puts "\n** Seeding Database: completed ***"
    ```

    Run the command below to seed the data according to seeds.rb
    ```sh
    rake db:seed
  5. stevenyap revised this gist Oct 18, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Rake Database.md
    Original file line number Diff line number Diff line change
    @@ -27,15 +27,15 @@ end

    ## Run Migrate
    - You need to run migration before ROR actually creates it in the database
    - *You must run this everytime there is a new migration*
    - *You must run this everytime there is a new migration*
    ```sh
    rake db:migrate
    ```

    ## Duplicate to test database
    - This should be ran after rake db:migrate
    - This duplicates your schema into the test database so that your test cases will work
    - Common error in test cases when you forget this step
    - Common error in test cases when you forget this step
    ```sh
    rake db:test:prepare
    ```
  6. stevenyap revised this gist Oct 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rake Database.md
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ end

    ## Run Migrate
    - You need to run migration before ROR actually creates it in the database
    - *You must run this everytime there is a new migration*
    - *You must run this everytime there is a new migration*
    ```sh
    rake db:migrate
    ```
  7. stevenyap revised this gist Oct 18, 2013. 1 changed file with 0 additions and 21 deletions.
    21 changes: 0 additions & 21 deletions Rake Database.md
    Original file line number Diff line number Diff line change
    @@ -51,27 +51,6 @@ rake db:drop
    rake db:drop db:create db:migration db:test:prepare
    ```

    ## Seeding the database
    - Note that seeding of database should be idempotent (running it multiple times does not create duplicated records)
    - Add your ruby commands to add data in /db/seeds.rb like below:
    ```ruby
    puts "** Seeding Database: seeding ***\n\n"

    unless User.where(email: "admin@example.com").first
    User.create!(email: 'admin@example.com',
    password: '123123',
    password_confirmation: '123123')
    puts "-- Created admin@example.com with password 123123\n"
    end

    unless Company.where(name: "ABC").first
    Company.create!(name: "ABC")
    puts "-- Created company: ABC\n"
    end

    puts "\n** Seeding Database: completed ***"

    ```

    Run the command below to seed the data according to seeds.rb
    ```sh
  8. stevenyap created this gist Oct 18, 2013.
    101 changes: 101 additions & 0 deletions Rake Database.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    ## Create database
    ```sh
    rake db:create
    ```

    ## Create database table
    This will creates a migration file in /db/migrate without table definition.
    ```sh
    rails g migration create_<TABLE>
    ```

    Add your table definition in the file.
    ```ruby
    class CreatePorts < ActiveRecord::Migration
    def change
    create_table :ports do |t|
    t.string :name
    t.text :description
    t.string :city
    t.string :country

    t.timestamps
    end
    end
    end
    ```

    ## Run Migrate
    - You need to run migration before ROR actually creates it in the database
    - *You must run this everytime there is a new migration*
    ```sh
    rake db:migrate
    ```

    ## Duplicate to test database
    - This should be ran after rake db:migrate
    - This duplicates your schema into the test database so that your test cases will work
    - Common error in test cases when you forget this step
    ```sh
    rake db:test:prepare
    ```

    ## Drop database
    ```sh
    rake db:drop
    ```

    ## Chaining commands
    - You can chain commands together for faster typing
    ```sh
    rake db:drop db:create db:migration db:test:prepare
    ```

    ## Seeding the database
    - Note that seeding of database should be idempotent (running it multiple times does not create duplicated records)
    - Add your ruby commands to add data in /db/seeds.rb like below:
    ```ruby
    puts "** Seeding Database: seeding ***\n\n"

    unless User.where(email: "admin@example.com").first
    User.create!(email: 'admin@example.com',
    password: '123123',
    password_confirmation: '123123')
    puts "-- Created admin@example.com with password 123123\n"
    end

    unless Company.where(name: "ABC").first
    Company.create!(name: "ABC")
    puts "-- Created company: ABC\n"
    end

    puts "\n** Seeding Database: completed ***"

    ```

    Run the command below to seed the data according to seeds.rb
    ```sh
    rake db:seed
    ```

    ## Roll back migration
    ```sh
    rake db:rollback
    ```

    Or you can specify how many steps (n) to rollback
    ```sh
    rake db:rollback step=n
    ```

    Or you can specfiy exactly the version of migration to rollback to
    ```sh
    rake db:migrate:down version=847583457438957
    ```

    Or to simply rerun last migration
    ```sh
    rake db:migrate:redo
    ```

    **Tip: If you are changing the latest migration, you should rake db:rollback and then add your new code and then run rake db:migrate**