class Miguel::Schema::Table::Context

Helper class used to evaluate the add_table block. Also implements the timestamping helper.

Public Class Methods

new( table ) click to toggle source

Create new context for given table.

# File lib/miguel/schema.rb, line 294
def initialize( table )
  @table = table
end

Public Instance Methods

method_missing( name, *args ) click to toggle source

Send anything unrecognized as new definition to our table.

# File lib/miguel/schema.rb, line 299
def method_missing( name, *args )
  @table.add_definition( name, *args )
end
timestamps() click to toggle source

Create the default timestamp fields.

# File lib/miguel/schema.rb, line 310
def timestamps
  opts = @table.schema.opts
  if opts[ :mysql_timestamps ]
    # Unfortunately, MySQL allows only either automatic create timestamp
    # (DEFAULT CURRENT_TIMESTAMP) or automatic update timestamp (DEFAULT
    # CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP), but not both - one
    # has to be updated manually anyway. So we choose to have the update timestamp
    # automatically updated, and let the create one to be set manually.
    # Also, Sequel doesn't currently honor :on_update for column definitions,
    # so we have to use default literal to make it work. Sigh.
    timestamp :create_time, :null => false, :default => ZERO_TIME
    timestamp :update_time, :null => false, :default => Sequel.lit( 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP' )
  else
    Time :create_time
    Time :update_time
  end
end