lib/also_migrate/migrator.rb in also_migrate-0.2.0 vs lib/also_migrate/migrator.rb in also_migrate-0.2.1
- old
+ new
@@ -42,21 +42,21 @@
config.each do |config|
options = config[:options]
config[:tables].each do |new_table|
if !connection.table_exists?(new_table) && connection.table_exists?(old_table)
columns = connection.columns(old_table).collect(&:name)
- columns -= options[:ignore].collect(&:to_s)
+ columns -= options[:subtract].collect(&:to_s)
columns.collect! { |col| connection.quote_column_name(col) }
- engine =
- if connection.class.to_s.include?('Mysql')
- 'ENGINE=' + connection.select_one(<<-SQL)['Engine']
- SHOW TABLE STATUS
- WHERE Name = '#{old_table}'
- SQL
- end
indexes = options[:indexes]
if indexes
+ engine =
+ if connection.class.to_s.include?('Mysql')
+ 'ENGINE=' + connection.select_one(<<-SQL)['Engine']
+ SHOW TABLE STATUS
+ WHERE Name = '#{old_table}'
+ SQL
+ end
connection.execute(<<-SQL)
CREATE TABLE #{new_table} #{engine}
AS SELECT #{columns.join(',')}
FROM #{old_table}
WHERE false;
@@ -67,9 +67,28 @@
else
connection.execute(<<-SQL)
CREATE TABLE #{new_table}
LIKE #{old_table};
SQL
+ end
+ end
+ if connection.table_exists?(new_table)
+ if options[:add] || options[:subtract]
+ columns = connection.columns(new_table).collect(&:name)
+ end
+ if options[:add]
+ options[:add].each do |column|
+ unless columns.include?(column[0])
+ connection.add_column(*([ new_table ] + column))
+ end
+ end
+ end
+ if options[:subtract]
+ options[:subtract].each do |column|
+ if columns.include?(column)
+ connection.remove_column(new_table, column)
+ end
+ end
end
end
end
end
end
\ No newline at end of file