Updated June 19, 2023
Introduction to Rails Migration
In Ruby Rails, the migration is a helper which facilitates the change or migration of a database in an application to a new schema. Usually, in Ruby, we handle data in SQL formally, but using Rails migration, we can define the database in DSL. Hence, we can use database-independent code; we benefit from moving the application to an entirely new platform. Additionally, we can fetch back the migrations and parallelly manage the application source code. Therefore, the Ruby Rails migration helper aids in the change of the database according to the user requirements.
What is Rails Migration?
We are familiar with using the ruby rails application. But sometimes, the user needs to change the data, which can be acquired using the helper “Rails Migration.” This helper facilitates the change of database within the rails application. We use SQL code for database management; here, we use Ruby code instead. This helper has several advantages as well. For instance, if we write code using SQL, the data will not be stored in the Active record model while changing the database to another application. Still, if you write the code using ruby rails, data will be stored in Active Records, and hence we gain the advantage of changing them to any application. Furthermore, migrations help us to change the database in a platform-independent way. This is the greatest advantage of using Ruby Rails migration over SQL.
The migration, an additive, helps create a new version of our existing database. One of the evolving applications is Ruby Rails; hence we can generate a new database using new migration and release an uncommon novel application. When it is good to use migration, in case we need to create a change in your database using ruby rails, it is essential to know that we should not change the database using Active Record; instead, using Migration is a good option. This migration helps us deploy the application containing the database into a new platform. These migrations are saved in the ruby itself. So we can able to view the changes in the ruby itself later. Another advantage of rails migration is the sharing changes, which help deploy the code by other users.
The other advantages of using rails are that we can write our database code, hence losing data independence. They are mostly labor-saving. Therefore, we can use DSL when we need to bring changes to the database by migrations.
Creating Rails Migration
To create a rails migration, the requirements are as follows:
- Ruby rails software
- Text editor
- Google Chrome browser
The operations we can perform using the migration are creating a table; we can add or change the column, drop a table, etc.; an example of creating a rails migration is as follows: Use the rails command prompt to generate a command. After opening the rails command prompt, go to start, then go to Railsinstaller and go to the command prompt, ad finally change into application directory to the parent directory.
The codes for migration using the default method are as follows:
- class CreateUsers < ActiveRecord::Migration[5.1]
- def change
- end
Let us see the changes that can be accommodated in rails migration. Adding a column to the existing column because it is one of the most common changes we make in an application. So, if we create any change in the existing database, we use migration. Hence, we use the Active records to make the changes in the database using the Ruby rails interface. We can use Rails’ basic data types. Examples are as follows: float, datetime, binary, and Boolean. Etc., We can even change a column, but it is irreversible. It is possible to add a new table which is also considered a change.
Let us see an example of using migration with SQL. But first, use MySQL to understand the migration flow.
# Mysql
default: &default
adapter: mysql2
username: root
password: password
host: 127.0.0.1
development:
<<: *default
database: todos_development
test:
<<: *default
database: todos_test
production:
<<: *default
database: todos_prod
Use the above sample code and open the working directory. Open database.yml.mysql in your favorite editor. In the above example, we see the database configuration file. This file helps MySQL to connect to a local host. The user in the example is “root,” and the password is “password.” So, we need to modify this according to our needs. So, copy the same file to the database.yml in the directory where you have the above file, and try to retain and save the original file. The next step is to run Rake. See the below code to follow. Beware, some warnings might occur.
rake db:create
Created database 'todos_development'
Created database 'todos_test'rake db:migrate
== 20181214203309 CreateTodos: migrating ======================================
-- create_table(:todos)
-> 0.0217s
== 20181214203309 CreateTodos: migrated (0.0218s) =============================
Now we need to run the migration, as shown above. So, here migration happened between SQLite3 and MySQL. So, as a recap, we changed the configuration file and ran the migration command. The rake command is useful in deploying the changes present in the application to a new backend.
Rails Migration File
When we create a migration, for example, as seen in the above content, the output will be a file with a name like 20190428200056_add_user_to_todos.rb.
So, please find the file name in your directory and open it using an editor. Rails will create a project file, where it creates migration. In the above example file name, the numerical is the version ID. This ID is the Rake used to deploy the application to MySQL.
Conclusion
So, we saw about migration and its uses as well. We must use the migration helper to change any database to another. Changes can be adding a database or column, etc.,
Recommended Articles
We hope that this EDUCBA information on “Rails Migration” was beneficial to you. You can view EDUCBA’s recommended articles for more information.