Migrations Flashcards

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

How to create a simple migration?

A

rails g migration sample

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

How to generate a migration with a table creation?

A
# create and table name are keywords there
rails g migration create_table_name

creates

def change
create_table :table_names do |t|
end
end

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

How to generate a migration to create table with a column field?

A

rails g migration create_table_names name:string

creates

  def change
    create_table :table_names do |t|
      t.string :name
    end
  end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to generate a migration to remove a column?

A
# you need to use Remove and From 
rails g migration RemoveColumnFromTableName sample

creates

def change
remove_column :table_names, :sample, :string
end

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

How to generate a new column with an index?

A

rails g migration AddSampleToTableName sample:string:index

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

How do rails know which migration should be run or not?

A

Rails create a row with a timestamp in schema_migrations table after each running migration.

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

How to run and rollback migration?

A

rails db:migrate

rails db:rollback

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

How to use up/down inside migrations?

A
# up runs on db:migrate
# down runs on db:rollback
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to create a table inside a migration?

A

create_table :hello

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

How to create a table with a field?

A

create_table :hello_world do |t|
t.column :sample, :string
end

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

How to create a table with a field?

A

create_table :hello_world do |t|
t.column :sample, :string
end

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

What are the differences between :decimal and :float?

A

:decimal - Stored with specified precision. Use for math that requires accuracy.

:float - Floating-point decimal number with fixed precision depending on platform. Do not use for math that requires accuracy due to rounding errors.

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

How to create a table with a field and index?

A

create_table :hello_world do |t|
t.column :sample, :string
t.index :sample
end

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

How to add belongs_to association on migration?

A
# references
    create_table :hello_world do |t|
      t.references :dog
    end
# belongs_to
    create_table :hello_world do |t|
      t.belongs_to :dog
    end
# add column with index 
    create_table :hello_world do |t|
      t.column :dog_id, :integer
      t.index :dog_id
    end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to add belongs_to association on migration? ( 3 ways)

A
# add column with index 
    create_table :hello_world do |t|
      t.column :dog_id, :integer
      t.index :dog_id
    end
# references
    create_table :hello_world do |t|
      t.references :dog
    end
# belongs_to
    create_table :hello_world do |t|
      t.belongs_to :dog
    end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to add a new column to the table?

A
def change
    add_column :table_names, :my_column, :integer
  end
17
Q

How to add additional fields to the new column? (default, null, limit, index)

A
def change
    add_column :table_names, :my_column, :integer, default: 1, limit: 10, index: true, null: false
  end
18
Q

How to show actual status of migration?

A

rake db:migrate:status

19
Q

What are the differences between db:migrate and db:schema:load?

A
  • rake db:migrate runs migrations that have not run yet
  • rake db:schema:load loads the schema.db file into database.

Advice: when you are adding a new migration to an existing app then you need to run rake db:migrate, but when you join to existing application (especially some old application), or when you drop your applications database and you need to create it again, always run rake db:schema:load to load schema

20
Q

How to remove a column from the table?

A

def change
remove_column :cats, :name
end

21
Q

How to drop a table?

A

def change
drop_table :cats
end

22
Q

How to change column type?

A
def change
    change_column :dogs, :name, :text
  end
23
Q

How to rename column?

A
def change
    rename_column :dogs, :name, :name2
  end
24
Q

How to add polymorphic migration? And what field this connection generates?

A

create_table :cats do |t|
t.references :owner, polymorphic: true
end

# owner_id
# owner_type
25
Q

How to create a migration to create a join table for 2 tables?

A
def change
    create_join_table :dogs, :users do |t|
      t.index [:dog_id, :user_id]
      t.index [:user_id, :dog_id]
    end
  end

the same

  def change
    create_table :dogs_users do |t|
      t.belongs_to :user
      t.belongs_to :dog
    end
  end
# creates the following
Table - dogs_users ( alphabetic order )
Fields - dog_id, user_id
Indexes
26
Q

Is the version number in ‘schema.rb’ used for anything? Should we carry about it?

A

RAILS AUTOMATICLY SET THE LATEST VERSION OF MIGRATION after rails db:migrate to schema.rb.

The real effect of the migration version is that all prior migrations (as found in the db/migrate directory) are assumed to have been run.

1) Shema version 2020, old migration 2019 what will happen after rake db:migrate?

Migration will be run, shema version has the latest migration version ( 2020 )

2) Shema version 2020, old migration 2019 what will happen after rake db:schema:load?

Migration won’t be added to the schema.rb. And db:migrate will not run this migration anymore ( schema_migration table already contains all migrations). If migration version will be after scheme version db:migrate will work ok.

3) Shema version 2020, new migration 2021 (but alredy in schema ) what will happen after rake db:schema:load and db:migrate?
- Error that this table already exist
- The problem occurs if someone creates a migration with a timestamp prior to your schema.rb’s :version. If you db:migrate, you’ll apply their migration, your schema.rb will be updated (but retain the same, higher :version), and everything is fine. But if you should happen to db:schema:load (or db:reset) instead, you’ll not only be missing their migration, butassume_migrated_upto_version will mark their migration as having been applied.

27
Q

How to add index/column directly in rails c ?

A

ActiveRecord::Migration.add_index :ahoy_messages, :mailer

or

ActiveRecord::Migration.remove_index :ahoy_messages, :mailer

28
Q

How to check all available indexes on the table?

A

ActiveRecord::Base.connection.indexes(:ahoy_messages)