Introduction
In today’s world, all websites have their databases, and they have to alter them continuously in a structured and organized manner. Users can achieve this with the help of SQL, but there are some constraints, like they have to tell other developers about the change and perform the changes by themselves. So is there any better way to perform these actions?
The answer is yes. We can perform all these operations efficiently and quickly with the help of migrations. As in migrations, we have the active record we keep the record of all the migrations that have been run, so we need to update the source code.
Migrations and its Features
Migrations in Rails allow users to use Ruby to define changes in the database's schema, keeping a synchronized actual code with the help of version controls.
Uses
- Production servers - To update the database, just run the “rake migrate” command whenever you roll out a new release.
- Multiple machines - This helps to keep all the code synchronized when you develop on more than one machine-like on a laptop and desktop.
- Teams of developers - As discussed earlier, other team members have to run the "rake migrate" command no matter how big the team is and who changes the schema.
Functions
- rename_table(old_name,new_name)
- rename_coloumn(table_name, new_column_name, column_name,)
- change_column(table_name, type, column_name, options)
- remove_index(table_name, column_name)
- add_index(table_name, index_type, column_name)
- drop_table(name)
- create_table(name, options)
- remove_coloumn(table_name, column_name)
Migration supports the following data types:
- string − for small data types such as a title.
- text − for textual data, such as the description.
- date and time − store either the date only or time only.
- integer − for whole numbers.
- binary − for storing data such as images, movies, or audio.
- float − for decimals.
- datetime and timestamp − store the date and time into a column.
- Boolean − for storing true or false values.
There are some valid column options the list is given below:
- null(: null=> false implies NOT NULL)
- limit(: limit=>”22”)
- default(: default=>” Something”)