Migrations Flashcards
How to create a simple migration?
rails g migration sample
How to generate a migration with a table creation?
# 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 to generate a migration to create table with a column field?
rails g migration create_table_names name:string
creates
def change create_table :table_names do |t| t.string :name end end
How to generate a migration to remove a column?
# you need to use Remove and From rails g migration RemoveColumnFromTableName sample
creates
def change
remove_column :table_names, :sample, :string
end
How to generate a new column with an index?
rails g migration AddSampleToTableName sample:string:index
How do rails know which migration should be run or not?
Rails create a row with a timestamp in schema_migrations table after each running migration.
How to run and rollback migration?
rails db:migrate
rails db:rollback
How to use up/down inside migrations?
# up runs on db:migrate # down runs on db:rollback
How to create a table inside a migration?
create_table :hello
How to create a table with a field?
create_table :hello_world do |t|
t.column :sample, :string
end
How to create a table with a field?
create_table :hello_world do |t|
t.column :sample, :string
end
What are the differences between :decimal and :float?
: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 to create a table with a field and index?
create_table :hello_world do |t|
t.column :sample, :string
t.index :sample
end
How to add belongs_to association on migration?
# 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 to add belongs_to association on migration? ( 3 ways)
# 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 to add a new column to the table?
def change add_column :table_names, :my_column, :integer end
How to add additional fields to the new column? (default, null, limit, index)
def change add_column :table_names, :my_column, :integer, default: 1, limit: 10, index: true, null: false end
How to show actual status of migration?
rake db:migrate:status
What are the differences between db:migrate and db:schema:load?
- 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
How to remove a column from the table?
def change
remove_column :cats, :name
end
How to drop a table?
def change
drop_table :cats
end
How to change column type?
def change change_column :dogs, :name, :text end
How to rename column?
def change rename_column :dogs, :name, :name2 end
How to add polymorphic migration? And what field this connection generates?
create_table :cats do |t|
t.references :owner, polymorphic: true
end
# owner_id # owner_type
How to create a migration to create a join table for 2 tables?
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
Is the version number in ‘schema.rb’ used for anything? Should we carry about it?
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.
How to add index/column directly in rails c ?
ActiveRecord::Migration.add_index :ahoy_messages, :mailer
or
ActiveRecord::Migration.remove_index :ahoy_messages, :mailer
How to check all available indexes on the table?
ActiveRecord::Base.connection.indexes(:ahoy_messages)