lib/migrant/migration_generator.rb in migrant-1.3.2 vs lib/migrant/migration_generator.rb in migrant-1.4.0
- old
+ new
@@ -34,11 +34,11 @@
@table_name = model.table_name
@columns = Hash[[:changed, :added, :deleted, :renamed, :transferred].collect { |a| [a,[]] }]
if model.table_exists?
# Structure ActiveRecord::Base's column information so we can compare it directly to the schema
- db_schema = Hash[*model.columns.collect {|c| [c.name.to_sym, Hash[*[:type, :limit].map { |type| [type, c.send(type)] }.flatten] ] }.flatten]
+ db_schema = Hash[*model.columns.collect {|c| [c.name.to_sym, Hash[*[:type, :limit, :default].map { |type| [type, c.send(type)] }.flatten] ] }.flatten]
model.schema.columns.to_a.sort { |a,b| a.to_s <=> b.to_s }.each do |field_name, data_type|
if data_type.dangerous_migration_from?(db_schema[field_name]) &&
ask_user("#{model}: '#{field_name}': Converting from ActiveRecord type #{db_schema[field_name][:type]} to #{data_type.column[:type]} could cause data loss. Continue?", %W{Yes No}, true) == "No"
log "Aborting dangerous action on #{field_name}."
elsif (options = data_type.structure_changes_from(db_schema[field_name]))
@@ -88,11 +88,11 @@
next if @columns[:changed].empty? && @columns[:added].empty? && @columns[:renamed].empty? && @columns[:transferred].empty? && @columns[:deleted].empty? && @indexes.empty? # Nothing to do for this table
# Example: changed_table_added_something_and_modified_something
@activity = 'changed_'+model.table_name+[['added', @columns[:added]], ['modified', @columns[:changed]], ['deleted', destroyed_columns],
- ['moved', @columns[:transferred]], ['renamed', @columns[:renamed]], ['indexed', @new_indexes]].reject { |v| v[1].empty? }.collect { |v| "_#{v[0]}_"+v[1].collect(&:first).join('_') }.join('_and')
+ ['moved', @columns[:transferred]], ['renamed', @columns[:renamed]], ['indexed', @new_indexes]].reject { |v| v[1].empty? }.collect { |v| "_#{v[0]}_"+v[1].collect(&:last).join('_') }.join('_and')
@activity = @activity.split('_')[0..2].join('_')+'_with_multiple_changes' if @activity.length >= 240 # Most filesystems will raise Errno::ENAMETOOLONG otherwise
render('change_migration')
else
@activity = "create_#{model.table_name}"
@@ -114,27 +114,33 @@
true
end
private
def add_column(name, options)
- @columns[:added] << [name, options]
+ @columns[:added] << [name, options, name]
end
def change_column(name, new_schema, old_schema)
- @columns[:changed] << [name, new_schema, old_schema]
+ if new_schema[:default] && new_schema[:default].respond_to?(:to_s) && new_schema[:default].to_s.length < 31
+ change_description = "#{name}_defaulted_to_#{new_schema[:default].to_s.underscore}"
+ else
+ change_description = name
+ end
+
+ @columns[:changed] << [name, new_schema, old_schema, change_description]
end
def delete_column(name, current_structure)
- @columns[:deleted] << [name, current_structure]
+ @columns[:deleted] << [name, current_structure, name]
end
def move_column(old_name, new_name, old_schema, new_schema)
if new_schema == old_schema
- @columns[:renamed] << [old_name, new_name]
+ @columns[:renamed] << [old_name, new_name, old_name]
@columns[:added].reject! { |a| a.first == new_name } # Don't add the column too
else
@possible_irreversible_migrations = true
- @columns[:transferred] << [old_name, new_name] # Still need to add the column, just transfer the data afterwards
+ @columns[:transferred] << [old_name, new_name, old_name] # Still need to add the column, just transfer the data afterwards
delete_column(old_name, old_schema)
end
end
def migrations_path