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

Defined in:
lib/sequel/lib/sequel/plugins/schema.rb

Instance Method Summary collapse

Instance Method Details

#create_tableObject

Creates table, using the column information from set_schema.


14
15
16
17
18
# File 'lib/sequel/lib/sequel/plugins/schema.rb', line 14

def create_table
  db.create_table(table_name, :generator=>@schema)
  @db_schema = get_db_schema(true)
  columns
end

#create_table!Object

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


22
23
24
25
# File 'lib/sequel/lib/sequel/plugins/schema.rb', line 22

def create_table!
  drop_table rescue nil
  create_table
end

#create_table?Boolean

Creates the table unless the table already exists


28
29
30
# File 'lib/sequel/lib/sequel/plugins/schema.rb', line 28

def create_table?
  create_table unless table_exists?
end

#drop_tableObject

Drops table.


33
34
35
# File 'lib/sequel/lib/sequel/plugins/schema.rb', line 33

def drop_table
  db.drop_table(table_name)
end

#schemaObject

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.


40
41
42
# File 'lib/sequel/lib/sequel/plugins/schema.rb', line 40

def schema
  @schema || (superclass.schema unless superclass == Model)
end

#set_schema(name = nil, &block) ⇒ Object

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.


53
54
55
56
57
# File 'lib/sequel/lib/sequel/plugins/schema.rb', line 53

def set_schema(name = nil, &block)
  set_dataset(db[name]) if name
  @schema = Sequel::Schema::Generator.new(db, &block)
  set_primary_key(@schema.primary_key_name) if @schema.primary_key_name
end

#table_exists?Boolean

Returns true if table exists, false otherwise.


60
61
62
# File 'lib/sequel/lib/sequel/plugins/schema.rb', line 60

def table_exists?
  db.table_exists?(table_name)
end