class Sequel::Database

Public Instance Methods

upgrade_table?( name, options={}, &block ) click to toggle source

Upgrades the table if it exists, or passes to create_table.

DB.upgrade_table?(:accounts){column :title, String, :default => 'foobar'}
# SELECT NULL AS `nil` FROM `accounts` LIMIT 1 -- check existence
# DESCRIBE `accounts`                          -- or similar to get database schema
# ALTER TABLE `accounts` ADD COLUMN `title` varchar(255) DEFAULT 'foobar'

NOTE: this method ignores column definition if the column already is in the database schema. This behavior is to evade possible DESTRUCTIVE action.

# File lib/sequel/extensions/auto_migration.rb, line 33
def upgrade_table?( name, options={}, &block )
  return create_table(name, options, &block)  unless table_exists?(name)
  current_schema = Hash[schema name]
  create_table_generator(&block).columns.each do |column|
    if current_schema[column[:name]]
      next
    else
      add_column name, column.delete(:name), column.delete(:type), column
    end
  end
end