Class: SQL::TableModifier

Inherits:
Object
  • Object
show all
Extended by:
DataMapper::Property::Lookup
Defined in:
lib/dm-migrations/sql/table_modifier.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (TableModifier) initialize(adapter, table_name, opts = {}, &block)



7
8
9
10
11
12
13
14
15
# File 'lib/dm-migrations/sql/table_modifier.rb', line 7

def initialize(adapter, table_name, opts = {}, &block)
  @adapter = adapter
  @table_name = table_name.to_s
  @opts = (opts)

  @statements = []

  self.instance_eval &block
end

Instance Attribute Details

- (Object) adapter

Returns the value of attribute adapter



5
6
7
# File 'lib/dm-migrations/sql/table_modifier.rb', line 5

def adapter
  @adapter
end

- (Object) opts

Returns the value of attribute opts



5
6
7
# File 'lib/dm-migrations/sql/table_modifier.rb', line 5

def opts
  @opts
end

- (Object) statements

Returns the value of attribute statements



5
6
7
# File 'lib/dm-migrations/sql/table_modifier.rb', line 5

def statements
  @statements
end

- (Object) table_name

Returns the value of attribute table_name



5
6
7
# File 'lib/dm-migrations/sql/table_modifier.rb', line 5

def table_name
  @table_name
end

Instance Method Details

- (Object) add_column(name, type, opts = {})



17
18
19
20
# File 'lib/dm-migrations/sql/table_modifier.rb', line 17

def add_column(name, type, opts = {})
  column = SQL::TableCreator::Column.new(@adapter, name, type, opts)
  @statements << "ALTER TABLE #{quoted_table_name} ADD COLUMN #{column.to_sql}"
end

- (Object) change_column(name, type, opts = {})



40
41
42
43
# File 'lib/dm-migrations/sql/table_modifier.rb', line 40

def change_column(name, type, opts = {})
  column = SQL::TableCreator::Column.new(@adapter, name, type, opts)
  @statements << @adapter.change_column_type_statement(table_name, column)
end

- (Object) drop_column(name) Also known as: drop_columns



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dm-migrations/sql/table_modifier.rb', line 22

def drop_column(name)
  # raise NotImplemented for SQLite3. Can't ALTER TABLE, need to copy table.
  # We'd have to inspect it, and we can't, since we aren't executing any queries yet.
  # TODO instead of building the SQL queries when executing the block, create AddColumn,
  # AlterColumn and DropColumn objects that get #to_sql'd
  if name.is_a?(Array)
    name.each{ |n| drop_column(n) }
  else
    @statements << "ALTER TABLE #{quoted_table_name} DROP COLUMN #{quote_column_name(name)}"
  end
end

- (Object) quote_column_name(name)



45
46
47
# File 'lib/dm-migrations/sql/table_modifier.rb', line 45

def quote_column_name(name)
  @adapter.send(:quote_name, name.to_s)
end

- (Object) quoted_table_name



49
50
51
# File 'lib/dm-migrations/sql/table_modifier.rb', line 49

def quoted_table_name
  @adapter.send(:quote_name, table_name)
end

- (Object) rename_column(name, new_name, opts = {})



35
36
37
38
# File 'lib/dm-migrations/sql/table_modifier.rb', line 35

def rename_column(name, new_name, opts = {})
  # raise NotImplemented for SQLite3
  @statements << @adapter.rename_column_type_statement(table_name, name, new_name)
end

- (Object) to_sql



53
54
55
# File 'lib/dm-migrations/sql/table_modifier.rb', line 53

def to_sql
  @statements.join(';')
end