Class: Sequel::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/extensions/auto_migration.rb

Instance Method Summary collapse

Instance Method Details

#upgrade_table?(name, options = {}, &block) ⇒ Boolean

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.

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
# 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