Annotate your Sequel models
Ruby
Pull request Compare This branch is 5 commits ahead, 6 commits behind kennym:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib/sequel
spec
.gitignore
CHANGELOG
Gemfile
MIT-LICENSE
README.rdoc
Rakefile
sequel-annotate.gemspec

README.rdoc

sequel-annotate

sequel-annotate annotates Sequel models with schema information. By default, it includes information on columns, indexes, and foreign key constraints for the current table.

On PostgreSQL, this includes more advanced information, including check constraints, triggers, and foreign keys constraints for other tables that reference the current table.

Example

The schema comments are kept at the end of the file, using a format similar to:

class Item < Sequel::Model
end

# Table: items
# Columns:
#  id                    | integer               | PRIMARY KEY DEFAULT nextval('items_id_seq'::regclass)
#  category_id           | integer               | NOT NULL
#  manufacturer_name     | character varying(50) |
#  manufacturer_location | text                  |
#  in_stock              | boolean               | DEFAULT false
#  name                  | text                  | DEFAULT 'John'::text
#  price                 | double precision      | DEFAULT 0
# Indexes:
#  items_pkey        | PRIMARY KEY btree (id)
#  name              | UNIQUE btree (manufacturer_name, manufacturer_location)
#  manufacturer_name | btree (manufacturer_name)
# Check constraints:
#  pos_id | (id > 0)
# Foreign key constraints:
#  items_category_id_fkey       | (category_id) REFERENCES categories(id)
#  items_manufacturer_name_fkey | (manufacturer_name, manufacturer_location) REFERENCES manufacturers(name, location)
# Triggers:
#  valid_price | BEFORE INSERT OR UPDATE ON items FOR EACH ROW EXECUTE PROCEDURE valid_price()

Install

gem install sequel-annotate

Usage

After loading the models:

require 'sequel/annotate'
Sequel::Annotate.annotate(Dir['models/*.rb'])

That will append or replace the schema comment in each of the files. It's best to run this when the repository is clean, and then use your source control tools (e.g. git diff) to see the changes it makes.

In some cases, sequel-annotate may not be able to correctly guess the model for the file. In that case, you may need to create an instance manually:

sa = Sequel::Annotate.new(Item)
sa.annotate('models/item.rb')

If you want to get the schema comment for a model without appending it to a file:

sa.schema_comment

Rake Task

Here's an example rake task for sequel-annotate:

desc "Update model annotations"
task :annotate do
  require 'sequel/annotate'
  Sequel::Annotate.annotate(Dir['models/*.rb'])
end

License

MIT

Author

Jeremy Evans <code@jeremyevans.net>

Based on [annotate-sequel]{github.com/kennym/annotate-sequel} by Kenny Meyer.