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
            
Tweet Me | Link To Facebook
Monday, 21st November, 2011
gravatar
Many thx 4 sharing !
Monday, 21st November, 2011
gravatar
Is there a rake task that will add "col" statements to your models if you are adding MiniRecord to an existing project that already uses ActiveRecord?    This way we could easily try it out.
Wednesday, 30th November, 2011
gravatar
https://gist.github.com/1409071