Sequel::Plugins::
InlineSchema module
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:
-
create_table
-
create_table!
-
create_table?
-
table_exists?
-
drop_table
-
drop_table?
If you use it with an abstract base class, you can ask the base class which of its subclasses need their tables created:
-
uninstalled_tables
It can also define hooks for creating and dropping the table:
-
before_create_table
-
after_create_table
-
before_drop_table
-
after_drop_table
As with other Sequel model hooks, you can prevent the action from the before_* hooks by calling cancel_action.