lib/also_migrate/migrator.rb in also_migrate-0.3.4 vs lib/also_migrate/migrator.rb in also_migrate-0.3.5
- old
+ new
@@ -12,16 +12,12 @@
end
module InstanceMethods
def migrate_with_also_migrate
- if ::AlsoMigrate.classes
- ::AlsoMigrate.classes.uniq.each do |klass|
- if klass.respond_to?(:also_migrate_config)
- AlsoMigrate.create_tables(klass)
- end
- end
+ (::AlsoMigrate.configuration || []).each do |config|
+ AlsoMigrate.create_tables(config)
end
rescue Exception => e
puts "AlsoMigrate error: #{e.message}"
puts e.backtrace.join("\n")
ensure
@@ -33,61 +29,64 @@
def connection
ActiveRecord::Base.connection
end
- def create_tables(klass)
- config = klass.also_migrate_config
- return unless config
- old_table = klass.table_name
- 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[:subtract].collect(&:to_s)
- columns.collect! { |col| connection.quote_column_name(col) }
- 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
+ def create_tables(config)
+ [ config[:destination] ].flatten.compact.each do |new_table|
+ if !connection.table_exists?(new_table) && connection.table_exists?(config[:source])
+ columns = connection.columns(config[:source]).collect(&:name)
+ columns -= [ config[:subtract] ].flatten.compact.collect(&:to_s)
+ columns.collect! { |col| connection.quote_column_name(col) }
+ if config[:indexes]
+ engine =
+ if connection.class.to_s.include?('Mysql')
+ 'ENGINE=' + connection.select_one(<<-SQL)['Engine']
+ SHOW TABLE STATUS
+ WHERE Name = '#{config[:source]}'
+ SQL
+ end
+ connection.execute(<<-SQL)
+ CREATE TABLE #{new_table} #{engine}
+ AS SELECT #{columns.join(',')}
+ FROM #{config[:source]}
+ WHERE false;
+ SQL
+ [ config[:indexes] ].flatten.compact.each do |column|
+ connection.add_index(new_table, column)
+ end
+ else
+ if connection.class.to_s.include?('SQLite')
+ col_string = connection.columns(old_table).collect {|c|
+ "#{c.name} #{c.sql_type}"
+ }.join(', ')
connection.execute(<<-SQL)
- CREATE TABLE #{new_table} #{engine}
- AS SELECT #{columns.join(',')}
- FROM #{old_table}
- WHERE false;
+ CREATE TABLE #{new_table}
+ (#{col_string})
SQL
- indexes.each do |column|
- connection.add_index(new_table, column)
- end
else
connection.execute(<<-SQL)
CREATE TABLE #{new_table}
- LIKE #{old_table};
+ LIKE #{config[:source]};
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
+ if connection.table_exists?(new_table)
+ if config[:add] || config[:subtract]
+ columns = connection.columns(new_table).collect(&:name)
+ end
+ if config[:add]
+ config[:add].each do |column|
+ unless columns.include?(column[0])
+ connection.add_column(*([ new_table ] + column))
end
end
- if options[:subtract]
- options[:subtract].each do |column|
- if columns.include?(column)
- connection.remove_column(new_table, column)
- end
+ end
+ if config[:subtract]
+ [ config[:subtract] ].flatten.compact.each do |column|
+ if columns.include?(column)
+ connection.remove_column(new_table, column)
end
end
end
end
end
\ No newline at end of file