Skip to content

ActiveRecord meets DataMapper, with MiniRecord you are be able to write schema inside your models.

Notifications You must be signed in to change notification settings

tyabe/mini_record

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

MiniRecord is a micro extension for our ActiveRecord gem. With MiniRecord you can add the ability to create columns outside the default schema.rb, directly in your model in a similar way that should know in others projects like DataMapper, MongoMapper or MongoID.

My inspiration come from this handy project.

Features

  • Define columns/properties inside your model
  • Perform migrations automatically
  • Auto upgrade your schema, so if you know what you are doing you don't lose your existing data!
  • Add, Remove, Change Columns; Add, Remove, Change indexes

Instructions

What you need is to move/remove your db/schema.rb. This avoid conflicts.

Add to your Gemfile:

gem 'mini_record'

That's all!

Examples

Remember that inside properties you can use all migrations methods, see documentation

class Post < ActiveRecord::Base
  col :title_en, :title_jp
  col :description_en, :description_jp, :as => :text
  col :permalink, :index => true, :limit => 50
  col :comments_count, :as => :integer
  col :category, :as => :references, :index => true
end
Post.auto_upgrade!

If you don't like col there are also few aliases: key, field, property, attribute

Instead of :as => :my_type you can use :type => :my_type

Option :as or :type if not provided is :string by default, you can use all ActiveRecord types:

:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time,
:date, :binary, :boolean, :references, :belongs_to, :timestamp

You can provide others ActiveRecord options like:

:limit, :default, :null, :precision, :scale

# example
class Foo < ActiveRecord::Base
  col :title, :default => "MyTitle" # :as => :string is by default
  col :price, :as => :decimal, :scale => 8, :precision => 2
end

See ActiveRecord::TableDefinition for more details.

Finally, when you execute MyModel.auto_upgrade!, missing columns, indexes and tables will be created on the fly. Indexes and columns present in the db but not in your model schema will be deleted* also in your db.

Single Table Inheritance

MiniRecord as ActiveRecord support STI plus some goodness, see our specs for more details.

ActiveRecord Relations

MiniRecord has built-in support of belongs_to, belongs_to polymorphic and habtm relations. Just declaring these in your model will generate the necessary id columns, indexes and join tables

belongs_to

class Address < ActiveRecord::Base
  belongs_to :person
end

Will result in a person_id column (you can override with the foreign_key option) which is indexed

belongs_to with foreign key in database

class Address < ActiveRecord::Base
  belongs_to :person
  index :person_id, :foreign => true
end

The same as in the previous case, but foreign key will be added to the database with help of foreigner gem.

To remove the key please use :foreign => false If you simple remove the index, the foreign key will not be removed.

belongs_to (polymorphic)

class Address < ActiveRecord::Base
  belongs_to :addressable, :polymorphic => true
end

Will result in addressable id and type columns with composite indexes add_index(:addresses), [:addressable_id, :addressable_type]

habtm

class Address < ActiveRecord::Base
  has_and_belongs_to_many :people
end

Will generate a "addresses_people" join table and index the id columns

Adding a new column

Super easy, open your model and just add it:

class Post < ActiveRecord::Base
  col :title
  col :body, :as => :text # <<- this
  col :permalink, :index => true
  col :comments_count, :as => :integer
  col :category, :as => :references, :index => true
end
Post.auto_upgrade!

So now when you invoke MyModel.auto_upgrade! you should see a SQL query like ALTER TABLE that mean that your existing records are happy and safe.

Removing a column

It's exactly the same, but the column will be really deleted without affect other columns.

Rename columns

Simply add a rename_col declaration and mini_record will do a connection.rename_column in the next auto_upgrade! but only if the db has the old column and not the new column. You still need to have a col declaration for the new column name so subsequent MyModel.auto_upgrade! will not remove the column. You are free to leave the rename_col declaration in place or you can remove it once the new column exists in the db.

Moving from:

class Vehicle < ActiveRecord::Base
  col :color

To:

class Vehicle < ActiveRecord::Base
  rename_col :color, :new_name => :body_color
  col :body_color
end

Then perhaps later:

class Vehicle < ActiveRecord::Base
  rename_col :color, :new_name => :body_color
  rename_col :body_color, :new_name => :chassis_color
  col :chassis_color

Change columns

It's not possible for us know that the column name changed, if you modify the name in a col declaration, but we can know if you changed the type so if you change t.string :name to t.text :name we are be able to perform an ALTER TABLE

Add/Remove indexes

In the same ways we manage columns MiniRecord will detect new indexes and indexes that needs to be removed. So when you perform MyModel.auto_upgrade! a SQL command like:

PRAGMA index_info('index_people_on_name')
CREATE INDEX "index_people_on_surname" ON "people" ("surname")

Note that writing it in DSL way you have same options as add_index so you are be able to write:

class Fox < ActiveRecord::Base
  col :foo, :index => true
  col :foo, :index => :custom_name
  col :foo, :index => [:foo, :bar]
  col :foo, :index => { :column => [:branch_id, :party_id], :unique => true, :name => 'by_branch_party' }
end

That is the same of:

class Fox < ActiveRecord::Base
  col :foo
  add_index :foo
  add_index :custom_name
  add_index [:foo, :bar]
  add_index [:branch_id, :party_id], :unique => true, :name => 'by_branch_party'
end

Author

DAddYE, you can follow me on twitter @daddye or take a look at my site daddye.it

Copyright

Copyright (C) 2011 Davide D'Agostino - @daddye

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

ActiveRecord meets DataMapper, with MiniRecord you are be able to write schema inside your models.

Resources

Stars

Watchers

Forks

Packages

No packages published