5. Flow:
• Use Ruby code (Database independent);
• Run rake task;
• Other developer run rake task;
• Run rake task during deploy
Database migrations in Rails
7. Database migrations in Rails
• Database independent way;
• db/migrate - directory;
• YYYYMMDDHHMMSS_add_details_to_products.rb;
• Execute method allows you to execute arbitrary SQL;
• Migrations are wrapped in transaction(PostgreSQL or r SQLite3, not support
MySQL*);
* stackoverflow
8. Database migrations in Rails
rails g model Product name:string description:text
rails g scaffold Product name:string description:text
rails g migration AddShortToProducts short:text
rails g migration RemoveShortFromProducts short:text
To create migration files:
9. Database migrations in Rails
Add and Remove Columns:
• AddXXXtoYYY
• RemoveXXXFromYYY
rails g migration AddShortToProducts short:text
rails g migration RemoveShortFromProducts short:text
10. create_table
Methods for db structure changing
create_table :friends do |t|
t.string :first_name
t.string :last_name
t.string :email
t.timestamps
end
change_table
change_table :friends do |t|
t.remove :last_name, :first_name
t.string :phone_number
t.index :phone_number
end
add_column
change_column :friends, :last_name, :string
change_column
add_column :friends, :first_name, :string
add_column :friends, :last_name, :integer
15. Database migrations in Rails
rake db:migrate #up
rake db:rollback #down
rake db:migrate:status
rake db:version #current actual migration version
rake db:migrate VERSION=20151117195012 #migrate to version
rake db:migrate:up VERSION=20151117195012 #migrate specific
rake db:migrate:down VERSION=20151117195012 #migrate specific
rake db:migrate:redo VERSION=20151117195012 #migrate down and up
rake db:rollback STEP=3 #revert last 3 migrations
rake db:migrate RAILS_ENV=test #run in test environment
More commands:
20. db/schema.rb:
• “...schema dumps are the authoritative source for your database schema... ”
• “...two ways to dump the schema… This is set in config/application.rb by the config.
active_record.schema_format setting, which may be either :sql or :ruby”
Database migrations in Rails
rake db:schema:dump #called by db:migrate
rake db:schema:load
21. Output of migrations:
• suppress_messages - Takes a block as an argument and suppresses any output
generated by the block.
• say - Takes a message argument and outputs it as is. A second boolean argument
can be passed to specify whether to indent or not.
• say_with_time - Outputs text along with how long it took to run its block. If the
block returns an integer it assumes it is the number of rows affected.
Database migrations in Rails
22. Best practices
• “...developers should not be afraid to clear out the old migrations directory, dump a
new schema, and continue on from there...”;
• Never have data only migrations, or migration that change existing data;
• Keep the schema.rb (or structure.sql) under version control;
• Use rake db:schema:load instead of rake db:migrate to initialize an empty
database;
• When writing constructive migrations (adding tables or columns), use the change
method instead of up and down methods;
• Enforce default values in the migrations themselves instead of in the application
layer;
• Atomic changes;