Coder Social home page Coder Social logo

sequel-annotate's Introduction

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 by default, 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

If you want to put the schema comment at the beginning of the file you can use the :position option:

Sequel::Annotate.annotate(Dir['models/*.rb'], position: :before)

or

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

If your models are nested in a namespace:

module ModelNamespace
  class Item < Sequel::Model
  end
end

Then you can use the :namespace option to set the namespace to use:

Sequel::Annotate.annotate(Dir['models/*.rb'], namespace: 'ModelNamespace')

Rake Task

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

desc "Update model annotations"
task :annotate do
  # Load the models first
  Dir['models/*.rb'].each{|f| require_relative f}

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

Running Tests

Tests of sequel-annotate itself can be run with rake:

rake

Set the SEQUEL_ANNOTATE_SPEC_POSTGRES_URL environment variable or appropriate libpq environment variables to specify which PostgreSQL database to connect to.

License

MIT

Author

Jeremy Evans <[email protected]>

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

sequel-annotate's People

Contributors

kennym avatar jeremyevans avatar ktaragorn avatar vosechu avatar

Watchers

Gencer W. Genç avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.