Module: Sequel::Plugins::Schema::ClassMethods

Module | Sequel::Plugins::Schema::ClassMethods
In: | lib/sequel/plugins/schema.rb

### Methods

create_table create_table! create_table? drop_table schema set_schema table_exists?

### Public Instance methods

create_table()

Creates table, using the column information from set_schema.

[Source]

        # File lib/sequel/plugins/schema.rb, line 14
    14:         def create_table
    15:           db.create_table(table_name, :generator=>@schema)
    16:           @db_schema = get_db_schema(true)
    17:           columns
    18:         end

create_table!()

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

[Source]

        # File lib/sequel/plugins/schema.rb, line 22
    22:         def create_table!
    23:           drop_table rescue nil
    24:           create_table
    25:         end

create_table?()

Creates the table unless the table already exists

[Source]

        # File lib/sequel/plugins/schema.rb, line 28
    28:         def create_table?
    29:           create_table unless table_exists?
    30:         end

drop_table()

Drops table.

[Source]

        # File lib/sequel/plugins/schema.rb, line 33
    33:         def drop_table
    34:           db.drop_table(table_name)
    35:         end

schema()

Returns table schema created with set_schema for direct descendant of Model. Does not retreive schema information from the database, see db_schema if you want that.

[Source]

        # File lib/sequel/plugins/schema.rb, line 40
    40:         def schema
    41:           @schema || (superclass.schema unless superclass == Model)
    42:         end

set_schema(name = nil, &block)

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

This is only needed if you want to use the create_table/create_table! methods. Will also set the dataset if you provide a name, as well as setting the primary key if you defined one in the passed block.

In general, it is a better idea to use migrations for production code, as migrations allow changes to existing schema. set_schema is mostly useful for test code or simple examples.

[Source]

        # File lib/sequel/plugins/schema.rb, line 53
    53:         def set_schema(name = nil, &block)
    54:           set_dataset(db[name]) if name
    55:           @schema = Sequel::Schema::Generator.new(db, &block)
    56:           set_primary_key(@schema.primary_key_name) if @schema.primary_key_name
    57:         end

table_exists?()

Returns true if table exists, false otherwise.

[Source]

        # File lib/sequel/plugins/schema.rb, line 60
    60:         def table_exists?
    61:           db.table_exists?(table_name)
    62:         end

[Validate]