Last active
October 18, 2025 14:39
-
-
Save stevenyap/7038932 to your computer and use it in GitHub Desktop.
Revisions
-
stevenyap revised this gist
Feb 20, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. (Specify STEP=9999 to rollback everything) ```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 -
stevenyap revised this gist
Jan 3, 2014 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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** ## Other database commands ```sh # View all migration logs rake db:migrate:status ``` -
stevenyap revised this gist
Oct 18, 2013 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 to the migration file and then run rake db:migrate again** -
stevenyap revised this gist
Oct 18, 2013 . 1 changed file with 25 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
stevenyap revised this gist
Oct 18, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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* ```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 ``` -
stevenyap revised this gist
Oct 18, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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* ```sh rake db:migrate ``` -
stevenyap revised this gist
Oct 18, 2013 . 1 changed file with 0 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` Run the command below to seed the data according to seeds.rb ```sh -
stevenyap created this gist
Oct 18, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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**