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

### Methods

create_table create_table! create_table? drop_table drop_table? schema set_schema table_exists?

### Public Instance methods

create_table(*args, &block)

Creates table, using the column information from set_schema.

[Source]

        # File lib/sequel/plugins/schema.rb, line 22
    22:         def create_table(*args, &block)
    23:           set_schema(*args, &block) if block
    24:           db.create_table(table_name, :generator=>@schema)
    25:           @db_schema = get_db_schema(true)
    26:           columns
    27:         end

create_table!(*args, &block)

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 31
    31:         def create_table!(*args, &block)
    32:           drop_table?
    33:           create_table(*args, &block)
    34:         end

create_table?(*args, &block)

Creates the table unless the table already exists

[Source]

        # File lib/sequel/plugins/schema.rb, line 37
    37:         def create_table?(*args, &block)
    38:           create_table(*args, &block) unless table_exists?
    39:         end

drop_table()

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

[Source]

        # File lib/sequel/plugins/schema.rb, line 42
    42:         def drop_table
    43:           db.drop_table(table_name)
    44:         end

drop_table?()

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

[Source]

        # File lib/sequel/plugins/schema.rb, line 47
    47:         def drop_table?
    48:           db.drop_table?(table_name)
    49:         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 54
    54:         def schema
    55:           @schema || (superclass.schema unless superclass == Model)
    56:         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 67
    67:         def set_schema(name = nil, &block)
    68:           set_dataset(db[name]) if name
    69:           @schema = db.create_table_generator(&block)
    70:           set_primary_key(@schema.primary_key_name) if @schema.primary_key_name
    71:         end

table_exists?()

Returns true if table exists, false otherwise.

[Source]

        # File lib/sequel/plugins/schema.rb, line 74
    74:         def table_exists?
    75:           db.table_exists?(table_name)
    76:         end

[Validate]