• Stars
    star
    265
  • Rank 154,577 (Top 4 %)
  • Language
    Ruby
  • Created about 13 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Build Status

MiniRecord is a micro extension for the ActiveRecord gem.

MiniRecord will allow you to create/edit/manage columns, directly in your model.

Features

  • Define columns/properties inside your model
  • Perform migrations automatically
  • Auto upgrade your schema
  • 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'

To optionally block any destructive actions on the database, create a file config/initializers/mini_record.rb and add:

MiniRecord.configure do |config|
  config.destructive = false
end

That's all!

Examples

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

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

Instead of field you can pick an alias: key, field, property, col

If the option :as is omitted, minirecord will assume it's a :string.

Remember that as for ActiveRecord you can choose different types:

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

You can also provide other options like:

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

# example
class Foo < ActiveRecord::Base
  field :title, default: "MyTitle" # as: :string is not necessary since is a default
  field :price, as: :decimal, scale: 8, precision: 2
end

See ActiveRecord::TableDefinition for more details.

Perform upgrades

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/definition will be deleted also from your db.

Single Table Inheritance

MiniRecord as ActiveRecord support STI:

  class Pet < ActiveRecord::Base; end
  class Dog < Pet; end
  class Cat < Pet; end
  ActiveRecord::Base.auto_upgrade!

When you perform ActiveRecord::Base.auto_upgrade!, just 1 table will be created with the type column (indexed as well).

ActiveRecord Relations

MiniRecord has built-in support of belongs_to, polymorphic associations as well with habtm relations.

You don't need to do anything in particular, is not even necessary define the field for them since they will be handled automatically.

belongs_to

class Address < ActiveRecord::Base
  belongs_to :person
end

Will result in a indexed person_id column. You can use a different one using the foreign_key option:

belongs_to :person, foreign_key: :person_pk

belongs_to with foreign key in database

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

This is the same example, but foreign key will be added to the database with help of foreigner gem.

In this case you have more control (if needed).

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 create an addressable_id and an addressable_type column 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" (aka: join table) with indexes on the id columns

Adding a new column

Super easy, open your model and just add it:

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

So now when you invoke MyModel.auto_upgrade! a diff between the old schema an the new one will detect changes and create the new column.

Removing a column

It's exactly the same as in the previous example.

Rename columns

Simply adding a rename_field 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.

This means that you still need to have a field declaration for the new column name so subsequent MyModel.auto_upgrade! will not remove the column.

You are free to leave the rename_field declaration in place or you can remove it once the new column exists in the db.

Moving from:

class Vehicle < ActiveRecord::Base
  field :color
end

To:

class Vehicle < ActiveRecord::Base
  rename_field :color, new_name: :body_color
  field :body_color
end

Then perhaps later:

class Vehicle < ActiveRecord::Base
  rename_field :color, new_name: :body_color
  rename_field :body_color, new_name: :chassis_color
  field :chassis_color
end

Change the type of columns

Where when you rename a column the task should be explicit changing the type is implicit.

This means that if you have

field :total, as: :integer

and later on you'll figure out that you wanted a float

field :total, as: :float

Will automatically change the type the the first time you'll invoke auto_upgrade.

Add/Remove indexes

In the same way 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")

A quick hint, sometimes index gets too verbose/long:

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

Here is where add_index comes handy, so you can rewrite the above in:

class Fox < ActiveRecord::Base
  field :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

Suppress default indexes for associations

If you do not need the default index for a belongs_to or has_and_belongs_to_many relationship, such as if you are using a composite index instead, you can suppress it from being created (or remove it) using suppress_index on the association:

class PhoneNumber < ActiveRecord::Base
  field :position
  belongs_to :person
  suppress_index :person
  add_index [:person_id, :position]
end

Passing options to Create Table

If you need to pass particular options to your CREATE TABLE statement, you can do so with create_table in the Model:

class Fox < ActiveRecord::Base
  create_table :options => 'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
  field :foo
end

Contributors

A special thanks to all who have contributed in this project:

  • Dmitriy Partsyrniy
  • Steven Garcia
  • Carlo Bertini
  • Nate Wiger
  • Dan Watson
  • Guy Boertje
  • virtax
  • Nagy Bence
  • Takeshi Yabe
  • blahutka
  • 4r2r

Author

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

Copyright

Copyright (C) 2011-2014 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.

More Repositories

1

foreverb

Small daemon framework for ruby, with logging, error handler, scheduling and much more.
Ruby
520
star
2

vips

Go Bindings for Vips (a super fast image processor)
Go
450
star
3

do

DO - IT! is a thin framework useful to manage remote servers through ssh.
Ruby
166
star
4

igo

Intermediate GoLang source representation
Go
142
star
5

leveldb

LevelDB for Ruby (embedded)
Ruby
126
star
6

lipsiadmin

Lipsiadmin is a new revolutionary admin for your projects. Lipsiadmin is based on Ext Js 3+. framework (with prototype adapter) and is ready for Rails 2.+. This admin is for newbie developper but also for experts, is not entirely written in javascript because the aim of developper wose build in a agile way web/site apps so we use extjs in a new intelligent way a mixin of โ€œoldโ€ html and new ajax functions, for example ext manage the layout of page, grids, tree and errors, but form are in html code.
Ruby
105
star
7

trez

A fast image resizer
Go
102
star
8

githubwatcher

Github Growl Watcher, watch any project and get instant growl notifications for: updates, new watchers, forks and issues
Ruby
88
star
9

lightbox

LightBox Helper is a small but beautifull helper that automatize the process for include LightBox on our pages.
JavaScript
39
star
10

ruby_nsq

Ruby client for the NSQ realtime message processing system
Ruby
28
star
11

gruby

Go lang to Ruby transpiler
Go
26
star
12

soda.vim

Reinterpreted Soda Theme for Vim Users
Vim Script
21
star
13

lipsiablog

Lightweight, simple, fast, easy blogging engine written in rails and Lipsiadmin
JavaScript
17
star
14

exception-notifier

Exception Notifier Plugin for Rails, that send email notification and generate error pages that you can use with your controller Layout.
Ruby
13
star
15

google_tasks

This gem provides access to google tasks apis
Ruby
10
star
16

model

Naive sql to struct for go
Go
8
star
17

node.nim

Work in progress node pattern on the awesome Nimrod
Nim
7
star
18

goroutine

Simple goroutine model for Ruby.
Ruby
6
star
19

fiddler

Wrapper for Fiddler (Ruby 2.0 FFI impl)
Ruby
6
star
20

ruvy

FFI Bindings to Libuv (experimental)
Ruby
6
star
21

eva

Effortless event driven micro framework that runs on Ruby in a different syntax
Ruby
6
star
22

golia

WebSite Link and Speed Checker
Ruby
5
star
23

documents

Document nubble FS
Ruby
4
star
24

tomorrow.vim

Dark light version of new Tomorrow Theme
Vim Script
4
star
25

.vim

My vim editor, plugins, syntax and more.
Vim Script
3
star
26

forever

MOVED HERE: https://github.com/DAddYE/foreverb
3
star
27

wintersmith-perian

Browserify, Coffee, Stylus, UglifyJS and CleanCSS
CoffeeScript
3
star
28

gist

Repo as a gist
Nim
3
star
29

loop

A tiny Ruby program used to periodically execute a command.
Ruby
2
star
30

snippets

Snipmate custom snippets
2
star
31

.do

My Servo recipes
Ruby
1
star
32

yarv

RubyVM (yarv) compiler/decompiler, written in go.
Go
1
star
33

rosetta

One language to rule them all.
Ruby
1
star
34

homebrew-alt

My homebrews
Ruby
1
star
35

mrb

FFI bindings for mruby
Ruby
1
star
36

uv

Ruby bindings for libuv
Ruby
1
star
37

lipsiadmin-I18n

Repository for collecting Locale data for Lipsiadmin
1
star
38

igo.vim

Vim syntax for igo
Vim Script
1
star
39

coding-style

Coding style
1
star