What
is Migration & Its Advantage ?
Migrations
are a convenient way for you to alter your database in a structured
and organized manner.
Rails
Migration allows you to use Ruby to define changes to your database
schema.
Making
it possible to use a version control system to keep things
synchronized with the actual code.
Adv:-
Teams
of developers : If one person makes a schema change, the other
developers just need to update, and run "rake migrate".
Production
servers : Run "rake migrate" when you roll out a new
release to bring the database up to date as well.
Multiple
machines :
If you develop on both a desktop and a laptop, or in more than one
location, migrations can help you keep them all synchronized.
List
out what can Rails Migration do?
Rails
Migration can do following things
-
Create table
-
Drop table
-
Rename table
-
Add column
-
Rename column
-
Change column
-
Remove column and so on
What
are the different process and syntax of migration ?
$
rails g migration filename
only
migration file will generate
$
rails g model modelname
Model
file and migration file will generate
migration
file change
1/Creating
table name
class
CreateUsers < ActiveRecord::Migration
def
change
create_table
:users do |t|
t.string
:firstname
t.timestamps
null: true
end
end
end
2/
Add a Column to Table
class
AddColumnToOrganisation < ActiveRecord::Migration
def
change
add_column
:table_name, :column_name, :data_type
end
end
3/
Rename column name of a Table
rename_column
:table_name, :old_name, :new_name
4/
Change data type of a column
change_column
:table_name, :column_name, :new_datatype
Comments
Post a Comment