Migration Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Basic migration structure to create products table with name and description columns. Aslo, what command can be used?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what command should be used to create a migration class to add part number column with an index and price to parts table.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

command to add user reference to products table.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

method to create a new table

A

create_table :products do |t|
t.string :name
t.text :description
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

method to create a joining table for the has many and belongs to association

A

create_join_table :products, :categories do |t|
t.index :product_id
t.index :category_id
end

this will generate categories_products table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

command to create joining table customers_products

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

method used to change tables (add, remove and rename) columns

A
change_table :products do |t|
  t.remove :name, discription
  t.string :part_number
  t.index :part_number
  t.rename :upccode, :upc_code
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

method to change description column to be text in the products table

A

change_column :products, :description, :text

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

method to add a foreign key for table articles from table authors

A

add_foreign_key :articles, :authors

will add author_id to articles table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

method to remove a foreign key for table articles from table authors

A

remove_foreign_key :articles, :authors

will remove author_id from articles table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

method to run sql query directly when helpers are not enough. user table products

A

Products.connection.execute(“your sql goes here”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

methods supported by change method

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

method and structure for migration that active record might not know how to reverse

A
reversible do |dir|
      dir.up do
        # add a CHECK constraint
        execute <
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is the old style migration instead of def change

A
class ExampleMigration < ActiveRecord::Migration[5.0]
  def up
    create_table :distributors do |t|
      t.string :zipcode
    end
    # add a CHECK constraint
    execute <
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how to run migration

A

rake db:migrate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to run a specific version of migration

A

rake db:migrate VERSION=20080906120000

17
Q

how to run a specific version and function in a migration

A

rake db:migrate:up VERSION=20080906120000

18
Q

how to rollback migration

A

rake db:rollback

19
Q

hot to rollback the last 3 migrations

A

rake db:rollback STEP=3

20
Q

how to setup a db

A

rake db:setup

21
Q

how to reset a db

A

rake db:reset

just like rake db:drop then rake db:setup

22
Q

how to run migration against production

A

rake db:migrate RAILS_ENV=production