Migrations are a convenient and easy way to make consistent changes to our database structure over time. This methodology is compatible with the Agile and XP methods used by Rails developers worldwide.
Migration is the subclass of ActiveRecord::Migration
, which overrides the two needed methods:
self.up
self.down
ActiveRecord
migrationsclass SampleMigration < ActiveRecord::Migration
def self.up
end
def self.down
end
end
We can perform the following operations in self.up
and self.down
methods:
In both practice and theory, the concept is straightforward:
self.up
method definition contains the actions we wish to migrate to the next version of the schema.self.down
method.Let’s look at the code below:
class SampleMigration < ActiveRecord::Migrationdef self.upcreate_table :people do |t|t.column :name, :stringendenddef self.downdrop_table :peopleendend
SampleMigration
subclass from the ActiveRecord::Migration
.self.up
method.SampleMigration
to create a table people
with a string column named name
.self.down
method.SampleMigration
to drop the people
table.Free Resources