Sequel::Plugins::

InlineSchema

module
Extended With
TSort

A replacement for Sequel's old built in schema plugin. It allows you to define your schema directly in the model using Model.set_schema (which takes a block similar to Database#create_table), and use Model.create_table to create a table using the schema information.

Usage

There are several ways to use this plugin.

Add the schema methods to all model subclasses:

Sequel::Model.plugin :inline_schema

Add the schema methods to a particular class:

Album.plugin :inline_schema
Album.set_schema { ... }
Album.create_table?

Add the schema methods to an abstract base class:

# lib/acme/model.rb
require 'sequel'
require 'acme'

module ACME
    Model = Class.new( Sequel::Model )
    Model.def_Model( ACME )

    class Model
        plugin :inline_schema
    end
end

# lib/acme/product.rb
require 'acme/model'

class ACME::Product < ACME::Model( :products )

    set_schema do
        primary_key :id
        String :sku, null: false
        String :name, null: false
        ...
    end

end

Notable Model Methods

See Sequel::Plugins::InlineSchema::ClassMethods for documentation for the methods the plugin adds to your model class/es.

Of particular note:

A model class with an inline schema has several methods for creating/dropping its associated table:

If you use it with an abstract base class, you can ask the base class which of its subclasses need their tables created:

It can also define hooks for creating and dropping the table:

As with other Sequel model hooks, you can prevent the action from the before_* hooks by calling cancel_action.