Class: XMigra::DeclarativeSupport::Table::Delta

Inherits:
Object
  • Object
show all
Defined in:
lib/xmigra/declarative_support/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old_state, new_state) ⇒ Delta

Returns a new instance of Delta.



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# 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

Instance Attribute Details

#altered_column_pairsObject (readonly)

Returns the value of attribute altered_column_pairs.



557
558
559
# File 'lib/xmigra/declarative_support/table.rb', line 557

def altered_column_pairs
  @altered_column_pairs
end

#constraints_to_dropObject (readonly)

Returns the value of attribute constraints_to_drop.



557
558
559
# File 'lib/xmigra/declarative_support/table.rb', line 557

def constraints_to_drop
  @constraints_to_drop
end

#new_columnsObject (readonly)

Returns the value of attribute new_columns.



557
558
559
# File 'lib/xmigra/declarative_support/table.rb', line 557

def new_columns
  @new_columns
end

#new_constraint_sql_clausesObject (readonly)

Returns the value of attribute new_constraint_sql_clauses.



557
558
559
# File 'lib/xmigra/declarative_support/table.rb', line 557

def new_constraint_sql_clauses
  @new_constraint_sql_clauses
end

#removed_columnsObject (readonly)

Returns the value of attribute removed_columns.



557
558
559
# File 'lib/xmigra/declarative_support/table.rb', line 557

def removed_columns
  @removed_columns
end