Class: MotionRecord::Schema::TableDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/motion_record/schema/table_definition.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ TableDefinition

Returns a new instance of TableDefinition.



4
5
6
7
8
9
10
11
12
# File 'lib/motion_record/schema/table_definition.rb', line 4

def initialize(name, options={})
  @name = name
  @columns = []
  @index_definitions = []

  unless options.has_key?(:id) && !options[:id]
    add_default_primary_column
  end
end

Instance Method Details

#executeObject



14
15
16
17
18
19
20
21
# File 'lib/motion_record/schema/table_definition.rb', line 14

def execute
  # Create table
  column_sql = @columns.map(&:to_sql_definition).join(", ")
  MotionRecord::Base.connection.execute "CREATE TABLE #{@name} (#{column_sql})"

  # Create table's indexes
  @index_definitions.each(&:execute)
end

#float(column_name, options = {}) ⇒ Object



31
32
33
# File 'lib/motion_record/schema/table_definition.rb', line 31

def float(column_name, options={})
  @columns << ColumnDefinition.new(:float, column_name, options)
end

#index(columns, options = {}) ⇒ Object



35
36
37
# File 'lib/motion_record/schema/table_definition.rb', line 35

def index(columns, options={})
  @index_definitions << IndexDefinition.new(@name, columns, options)
end

#integer(column_name, options = {}) ⇒ Object



27
28
29
# File 'lib/motion_record/schema/table_definition.rb', line 27

def integer(column_name, options={})
  @columns << ColumnDefinition.new(:integer, column_name, options)
end

#text(column_name, options = {}) ⇒ Object



23
24
25
# File 'lib/motion_record/schema/table_definition.rb', line 23

def text(column_name, options={})
  @columns << ColumnDefinition.new(:text, column_name, options)
end

#timestampsObject

Add :created_at and :updated_at columns to the table



40
41
42
43
# File 'lib/motion_record/schema/table_definition.rb', line 40

def timestamps
  self.integer(:created_at)
  self.integer(:updated_at)
end