2013-10-14 04:36:58 +02:00
|
|
|
|
---
|
2023-03-14 05:40:29 +01:00
|
|
|
|
title: Rails migrations
|
2015-11-24 06:02:17 +01:00
|
|
|
|
category: Rails
|
2013-10-14 04:36:58 +02:00
|
|
|
|
---
|
2012-03-16 12:45:06 +01:00
|
|
|
|
|
2012-10-11 10:13:19 +02:00
|
|
|
|
### Automatically make migrations
|
|
|
|
|
|
|
|
|
|
$ rails generate migration RemovePartNumberFromProducts part_number:string
|
2017-03-01 06:34:40 +01:00
|
|
|
|
$ rails generate migration remove_part_number_from_products part_number # rails assumes string if not type given - and you can use snake_case
|
2017-01-22 15:02:25 +01:00
|
|
|
|
|
2012-10-11 10:13:19 +02:00
|
|
|
|
$ rails generate migration AddNameToWidgets name:string
|
2017-03-01 06:34:40 +01:00
|
|
|
|
$ rails g migration add_name_to_widgets name:string # you can use the short cut 'g' instead of generate - they both do the same thing
|
2012-10-11 10:13:19 +02:00
|
|
|
|
|
2012-03-16 12:45:06 +01:00
|
|
|
|
### Run migrations
|
|
|
|
|
|
|
|
|
|
$ rake db:migrate
|
|
|
|
|
|
2015-01-16 05:02:16 +01:00
|
|
|
|
### Creating tables
|
2012-03-16 12:45:06 +01:00
|
|
|
|
|
|
|
|
|
create_table :users do |t|
|
|
|
|
|
t.string :name
|
|
|
|
|
t.text :description
|
|
|
|
|
|
|
|
|
|
t.primary_key :id
|
|
|
|
|
t.string :title
|
|
|
|
|
t.text :description
|
|
|
|
|
t.integer :games_count
|
|
|
|
|
t.float :lol
|
|
|
|
|
t.decimal :price
|
|
|
|
|
t.decimal :price, :precision => 2, :scale => 10
|
|
|
|
|
t.datetime :expiration
|
|
|
|
|
t.timestamp :time_in
|
|
|
|
|
t.time :time_in
|
|
|
|
|
t.date :expiry
|
|
|
|
|
t.binary :image_data
|
|
|
|
|
t.boolean :is_admin
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Options:
|
|
|
|
|
:null (boolean)
|
|
|
|
|
:limit (integer)
|
|
|
|
|
:default
|
|
|
|
|
|
|
|
|
|
### Operations
|
|
|
|
|
|
2015-01-16 05:02:16 +01:00
|
|
|
|
add_column :users, :first_name, :string
|
|
|
|
|
remove_column :users, :first_name, :string
|
|
|
|
|
|
|
|
|
|
change_column :users, :first_name, :text
|
|
|
|
|
|
|
|
|
|
change_column_default :users, :admin, nil
|
2017-02-26 23:25:14 +01:00
|
|
|
|
change_column_null :users, :email, false # adds NOT NULL constraint
|
2015-01-16 05:02:16 +01:00
|
|
|
|
|
2012-03-16 12:45:06 +01:00
|
|
|
|
create_table
|
|
|
|
|
change_table
|
|
|
|
|
drop_table
|
2015-01-16 05:02:16 +01:00
|
|
|
|
|
2012-03-16 12:45:06 +01:00
|
|
|
|
add_column
|
|
|
|
|
change_column
|
|
|
|
|
rename_column
|
|
|
|
|
remove_column
|
2015-01-16 05:02:16 +01:00
|
|
|
|
|
2012-03-16 12:45:06 +01:00
|
|
|
|
add_index
|
|
|
|
|
remove_index
|
|
|
|
|
|
2014-06-04 09:14:41 +02:00
|
|
|
|
### Use models
|
|
|
|
|
|
|
|
|
|
class AddFlagToProduct < ActiveRecord::Migration
|
|
|
|
|
class Product < ActiveRecord::Base
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def change
|
|
|
|
|
add_column :products, :flag, :boolean
|
|
|
|
|
Product.reset_column_information
|
|
|
|
|
reversible do |dir|
|
|
|
|
|
dir.up { Product.update_all flag: false }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-03-16 12:45:06 +01:00
|
|
|
|
### Associations
|
|
|
|
|
|
|
|
|
|
t.references :category # kinda same as t.integer :category_id
|
|
|
|
|
|
|
|
|
|
# Can have different types
|
|
|
|
|
t.references :category, polymorphic: true
|
|
|
|
|
|
2015-01-16 05:02:16 +01:00
|
|
|
|
### Auto-Add/remove columns
|
2012-03-16 12:45:06 +01:00
|
|
|
|
|
|
|
|
|
$ rails generate migration RemovePartNumberFromProducts part_number:string
|
|
|
|
|
|
|
|
|
|
### Indices
|
|
|
|
|
|
|
|
|
|
# Simple
|
|
|
|
|
add_index :suppliers, :name
|
|
|
|
|
|
|
|
|
|
# Unique
|
|
|
|
|
add_index :accounts, [:branch_id, :party_id], :unique => true
|
|
|
|
|
|
|
|
|
|
# Named (:name => ...)
|
|
|
|
|
add_index :accounts, [:branch_id, :party_id], :unique => true, :name => "by_branch_party"
|
|
|
|
|
|
|
|
|
|
# Length
|
|
|
|
|
add_index :accounts, :name, :name => ‘by_name’, :length => 10
|
|
|
|
|
add_index :accounts, [:name, :surname], :name => ‘by_name_surname’,
|
|
|
|
|
:length => {
|
|
|
|
|
:name => 10,
|
|
|
|
|
:surname => 15
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Sort order (no MySQL support)
|
|
|
|
|
add_index :accounts, [:branch_id, :party_id, :surname],
|
|
|
|
|
:order => {:branch_id => :desc, :part_id => :asc}
|
|
|
|
|
|
2016-04-25 09:55:29 +02:00
|
|
|
|
### In console
|
|
|
|
|
Use `ActiveRecord::Migration`.
|
|
|
|
|
|
|
|
|
|
ActiveRecord::Migration.add_index :posts, :slug
|
|
|
|
|
|
2012-03-16 12:45:06 +01:00
|
|
|
|
### References
|
|
|
|
|
|
2022-11-01 04:29:32 +01:00
|
|
|
|
* https://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_index
|