Sequel::Plugins::InlineSchema::

ClassMethods

module

Sequel plugin API – add these methods to model classes which load the plugin.

Public Instance Methods

anchor
after_create_table()

Called after the table is created.

# File lib/sequel/plugins/inline_schema.rb, line 134
def after_create_table
        # No-op
end
anchor
after_drop_table()

Called after the table is dropped.

# File lib/sequel/plugins/inline_schema.rb, line 160
def after_drop_table
        # No-op
end
anchor
before_create_table()

Called before the table is created.

# File lib/sequel/plugins/inline_schema.rb, line 128
def before_create_table
        # No-op
end
anchor
before_drop_table()

Called before the table is dropped.

# File lib/sequel/plugins/inline_schema.rb, line 154
def before_drop_table
        # No-op
end
anchor
create_table( *args, &block )

Creates table, using the column information from set_schema.

# File lib/sequel/plugins/inline_schema.rb, line 103
def create_table( *args, &block )
        self.set_schema( *args, &block ) if block
        self.before_create_table
        self.db.create_table( self.table_name, generator: self.schema )
        @db_schema = get_db_schema( true )
        self.after_create_table
        return self.columns
end
anchor
create_table!( *args, &block )

Drops the table if it exists and then runs create_table. Should probably not be used except in testing.

# File lib/sequel/plugins/inline_schema.rb, line 115
def create_table!( *args, &block )
        self.drop_table?
        return self.create_table( *args, &block )
end
anchor
create_table?( *args, &block )

Creates the table unless the table already exists

# File lib/sequel/plugins/inline_schema.rb, line 122
def create_table?( *args, &block )
        self.create_table( *args, &block ) unless self.table_exists?
end
anchor
drop_table()

Drops table. If the table doesn't exist, this will probably raise an error.

# File lib/sequel/plugins/inline_schema.rb, line 140
def drop_table
        self.before_drop_table
        self.db.drop_table( self.table_name )
        self.after_drop_table
end
anchor
drop_table?()

Drops table if it already exists, do nothing if it doesn't exist.

# File lib/sequel/plugins/inline_schema.rb, line 148
def drop_table?
        self.db.drop_table?( self.table_name )
end
anchor
schema()

Returns the table schema created with set_schema.

# File lib/sequel/plugins/inline_schema.rb, line 166
def schema
        if !@schema && @schema_block
                self.set_dataset( self.db[@schema_name] ) if @schema_name
                @schema = self.db.create_table_generator( &@schema_block )
                self.set_primary_key( @schema.primary_key_name ) if @schema.primary_key_name
        end
        return @schema || ( superclass.schema unless superclass == Sequel::Model )
end
anchor
set_schema( name=nil, require_table: false, &block )

Defines a table schema (see Schema::CreateTableGenerator for more information).

This will also set the dataset if you provide a name, as well as setting the primary key if you define one in the passed block.

Since this plugin allows you to declare the schema inline with the model class that acts as its interface, the table will not always exist when the class loads, so calling set_schema will call require_valid_table to false for you. You can disable this by passing require_table: true.

# File lib/sequel/plugins/inline_schema.rb, line 185
def set_schema( name=nil, require_table: false, &block )
        self.require_valid_table = require_table
        @schema = nil
        @schema_name = name
        @schema_block = block
end
anchor
table_exists?()

Returns true if table exists, false otherwise.

# File lib/sequel/plugins/inline_schema.rb, line 194
def table_exists?
        return self.db.table_exists?( self.table_name )
end
anchor
uninstalled_tables()

Return an Array of model table names that don't yet exist, in the order they need to be created to satisfy foreign key constraints.

# File lib/sequel/plugins/inline_schema.rb, line 213
def uninstalled_tables
        self.db.log_info "  searching for unbacked model classes..."

        self.tsort.find_all do |modelclass|
                next unless modelclass.name && modelclass.name != ''
                !modelclass.table_exists?
        end.uniq( &:table_name )
end

Protected Instance Methods

anchor
cancel_action( msg=nil )

Cancel the currently-running before_* hook. If a msg is given, use it when constructing the HookFailed exception.

# File lib/sequel/plugins/inline_schema.rb, line 244
def cancel_action( msg=nil )
        self.raise_hook_failure( msg )
end
anchor
raise_hook_failure( type=nil )

Raise an appropriate Sequel::HookFailure exception for the specified type.

# File lib/sequel/plugins/inline_schema.rb, line 228
def raise_hook_failure( type=nil )
        msg = case type
                when String
                        type
                when Symbol
                        "the #{type} hook failed"
                else
                        "a hook failed"
                end

        raise Sequel::HookFailed.new( msg, self )
end
anchor
tsort_each_child( model_class ) { |model_class| ... }

TSort API – yield each of the given model_class's dependent model classes.

# File lib/sequel/plugins/inline_schema.rb, line 257
def tsort_each_child( model_class ) # :yields: model_class
        # Include (non-anonymous) parents other than Model
        model_class.ancestors[1..-1].
                select {|cl| cl < self }.
                select( &:name ).
                each do |parentclass|
                        yield( parentclass )
                end

        # Include associated classes for which this model class's table has a
        # foreign key
        model_class.association_reflections.each do |name, config|
                next if config[:polymorphic]

                associated_class = config.associated_class

                yield( associated_class ) if config[:type] == :many_to_one
        end
end
anchor
tsort_each_node( &block )

TSort API – yield each model class.

# File lib/sequel/plugins/inline_schema.rb, line 250
def tsort_each_node( &block )
        self.descendents.select( &:name ).each( &block )
end