[Rails] Migration

Tags
Rails
Engineering
Created
Oct 20, 2023 11:34 PM
Edited
Oct 20, 2023
Description
Basic migration

What is migration?

Migrations are a convenient way to alter your database schema over time in a consistent way.
  • We need to run migration make sure the schema on the machine is up-to-date with the code.

Migration file

# 20211109184904_create_table.rb
class CreateProducts < ActiveRecord::Migration[7.1]
  def change
    create_table :products do |t|
      t.string :name
      t.text :description

      t.timestamps
    end
  end
end

Do migration on a file

# ex: a migration file 20211109184904_create_table.rb
bundle exec rake db:migrate:up VERSION=20211109184904

Check migration status

bundle exec rails db:migrate:status

Source