Migranize is a Ruby gem that automatically generates Rails migration files by analyzing changes in your models, similar to Django's migration system.
- Automatically detect changes in your Rails models
- Generate migration files based on model changes
- Simple CLI commands for generating migrations
- Seamless integration with Rails' migration system
- Inspired by Django's migration workflow
Add this line to your application's Gemfile:
gem 'migranize'And then execute:
bundle installInitialize Migranize in your Rails project:
bundle exec migranize initThis will create a configuration file at config/migranize.rb.
Define your models using the field system provided by Migranize:
# app/models/user.rb
class User < ApplicationRecord
field :name, :string
field :email, :string
field :age, :integer
field :active, :boolean, default: true
has_many :posts
end
# app/models/post.rb
class Post < ApplicationRecord
field :title, :string
field :content, :text
field :published_at, :datetime
belongs_to :user
endAfter defining or changing your models, generate migrations with:
bundle exec migranize make_migrationsThis will analyze your models, detect changes, and create migration files in your db/migrate directory.
Once the migrations are generated, run them using Rails' standard command:
rails db:migrateYou can customize Migranize's behavior in the configuration file (config/migranize.rb):
Migranize.configure do |config|
# Namespaces to ignore when detecting model changes
config.ignore_namespaces = []
# Tables to ignore when generating migrations
config.ignore_tables = []
# Directory where migration files are stored
config.migrations_dir = "db/migrate"
# Directory where model files are stored
config.models_dir = "app/models"
endMigranize supports all standard ActiveRecord column types:
:string
:text
:integer
:float
:decimal
:datetime
:time
:date
:binary
:boolean
:json
:jsonb (PostgreSQL only)
:uuid (PostgreSQL only)You can specify various options when defining fields:
field :name, :string, null: false
field :price, :decimal, precision: 10, scale: 2
field :status, :string, default: "pending"
field :metadata, :json, null: trueclass Product < ApplicationRecord
field :name, :string
field :description, :text
field :price, :decimal, precision: 10, scale: 2
field :in_stock, :boolean, default: true
field :released_at, :datetime
endWhen you add or change fields in your model:
class Product < ApplicationRecord
field :name, :string
field :description, :text
field :price, :decimal, precision: 10, scale: 2
field :in_stock, :boolean, default: true
field :released_at, :datetime
field :category, :string # New field
field :tags, :json # New field
endRunning migranize make_migrations will generate a migration that adds these new fields to your table.
The gem is available as open source under the terms of the MIT License.