Migration Flashcards
Basic migration structure to create products table with name and description columns. Aslo, what command can be used?
rails generate migration CreateProducts name:string description:text
class CreateProducts < ActiveRecord::Migration[5.0] def change create_table :products do |t| t.string :name t.text :description
t.timestamps end end end
what command should be used to create a migration class to add part number column with an index and price to parts table.
rails generate migration AddPartNumberToParts part_number:string:index price:decimal
will generate
class AddPartNumberToProducts < ActiveRecord::Migration[5.0]
def change
add_column :products, :part_number, :string
add_column :products, :price, :decimal
add_index :products, :part_number
end
end
command to add user reference to products table.
rails generate migration AddUserRefToProducts user:references
class AddUserRefToProducts < ActiveRecord::Migration[5.0]
def change
add_reference :products, :user, index: true, foreign_key: true
end
end
method to create a new table
create_table :products do |t|
t.string :name
t.text :description
end
method to create a joining table for the has many and belongs to association
create_join_table :products, :categories do |t|
t.index :product_id
t.index :category_id
end
this will generate categories_products table
command to create joining table customers_products
rails generate migration CreateJoinTableCustomerProduct customer product
class CreateJoinTableCustomerProduct < ActiveRecord::Migration[5.0]
def change
create_join_table :customers, :products do |t|
# t.index [:customer_id, :product_id]
# t.index [:product_id, :customer_id]
end
end
end
method used to change tables (add, remove and rename) columns
change_table :products do |t| t.remove :name, discription t.string :part_number t.index :part_number t.rename :upccode, :upc_code end
method to change description column to be text in the products table
change_column :products, :description, :text
method to add a foreign key for table articles from table authors
add_foreign_key :articles, :authors
will add author_id to articles table
method to remove a foreign key for table articles from table authors
remove_foreign_key :articles, :authors
will remove author_id from articles table
method to run sql query directly when helpers are not enough. user table products
Products.connection.execute(“your sql goes here”)
methods supported by change method
add_column add_foreign_key add_index add_reference add_timestamps change_column_default (must supply a :from and :to option) change_column_null create_join_table create_table disable_extension drop_join_table drop_table (must supply a block) enable_extension remove_column (must supply a type) remove_foreign_key (must supply a second table) remove_index remove_reference remove_timestamps rename_column rename_index rename_table
method and structure for migration that active record might not know how to reverse
reversible do |dir| dir.up do # add a CHECK constraint execute <
what is the old style migration instead of def change
class ExampleMigration < ActiveRecord::Migration[5.0] def up create_table :distributors do |t| t.string :zipcode end
# add a CHECK constraint execute <
how to run migration
rake db:migrate