Module: Mover::Base::Table

Defined in:
lib/mover/table.rb

Instance Method Summary collapse

Instance Method Details

#create_movable_table(type, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mover/table.rb', line 5

def create_movable_table(type, options={})
  movable_table = [ type, table_name ].join('_')
  columns =
    if options[:columns]
      options[:columns].collect { |c| "`#{c}`" }.join(', ')
    else
      '*'
    end
  engine = options[:engine]
  engine ||=
    if connection.class.to_s.include?('Mysql')
      "ENGINE=InnoDB"
    end
  if table_exists? and !connection.table_exists?(movable_table)
    # Create table
    connection.execute("CREATE TABLE \#{movable_table} \#{engine}\nAS SELECT \#{columns}\nFROM \#{table_name}\nWHERE false;\n")
    # Create indexes
    options[:indexes] ||= indexed_columns(table_name)
    options[:indexes].each do |column|
      connection.add_index(movable_table, column)
    end
  end
end

#drop_movable_table(*types) ⇒ Object



34
35
36
37
38
# File 'lib/mover/table.rb', line 34

def drop_movable_table(*types)
  types.each do |type|
    connection.execute("DROP TABLE IF EXISTS #{[ type, table_name ].join('_')}")
  end
end