class XMigra::DeclarativeSupport::Table::Delta

Attributes

altered_column_pairs[R]
constraints_to_drop[R]
new_columns[R]
new_constraint_sql_clauses[R]
removed_columns[R]

Public Class Methods

new(old_state, new_state) click to toggle source
# File lib/xmigra/declarative_support/table.rb, line 514
def initialize(old_state, new_state)
  @constraints_to_drop = []
  @new_constraint_sql_clauses = []
  
  # Look for constraints from old_state that are removed and gather
  # constraint creation SQL
  old_constraint_sql = old_state.constraints.each_value.inject({}) do |result, constr|
    if new_state.constraints.has_key? constr.name
      result[constr.name] = constr.creation_sql
    else
      @constraints_to_drop << constr.name
    end
    
    result
  end
  
  # Look for constraints that are new to or altered in new_state
  new_state.constraints.each_value do |constr|
    if old_constraint_sql.has_key? constr.name
      if old_constraint_sql[constr.name] != (crt_sql = constr.creation_sql)
        @constraints_to_drop << constr.name
        @new_constraint_sql_clauses << crt_sql
      end
    else
      new_constraint_sql_clauses << constr.creation_sql
    end
  end
  
  # Look for new and altered columns
  @new_columns = []
  @altered_column_pairs = []
  new_state.columns.each do |col|
    if !old_state.has_column? col.name
      @new_columns << col
    elsif new_state.column_alteration_occurs?(old_col = old_state.get_column(col.name), col)
      @altered_column_pairs << [old_col, col]
    end
  end
  
  # Look for removed columns
  @removed_columns = old_state.columns.reject {|col| new_state.has_column? col.name}
end