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

License

MIT

Author

Jeremy Evans [email protected]

Based on [annotate-sequel]https://github.com/kennym/annotate-sequel by Kenny Meyer.