Migrate All Of Your Model Classes When Using MiniRecord
MiniRecord is a micro extension for ActiveRecord which allows you to define your schema directly in your model in a similar way to others projects like DataMapper or MongoMapper.
class Post < ActiveRecord::Base col :title col :description, :as => :text
col :permalink, :index => true, :limit => 50
col :comments_count, :as => :integer
col :category, :as => :references, :index => true
end
Once the columns are defined inside the model you call the auto_upgrade! class method to run the migration on a class by class basis.
Post.auto_upgrade!
Most (if not all) projects have multiple model classes, and it is common to make changes to more than one model at a time. When using standard rails migrations you have access to the rake db:migrate task to migrate all the changes made to the schema. Unfortunately out of the box the MiniRecord gem does not provide this. Introducing rake mini_record:migrate. Add this rake task to your Rails project and when called it will loop over all of your model classes auto upgrading (migrating) them one by one.
if defined?(Rails) && (Rails.env == 'development')
Rails.logger = Logger.new(STDOUT)
end
namespace :mini_record do
desc "Auto migration of database"
task :migrate => :environment do
Dir["app/models/*.rb"].each do |file_path|
basename = File.basename(file_path, File.extname(file_path))
clazz = basename.camelize.constantize
clazz.auto_upgrade! if clazz.ancestors.include?(ActiveRecord::Base)
end
end
end