Module: ActiveRecordSchema::SchemaDiff

Extended by:
ActiveSupport::Concern
Included in:
Schema
Defined in:
lib/active_record_schema/schema_diff.rb

Instance Method Summary collapse

Instance Method Details

#_column_namesObject



43
44
45
# File 'lib/active_record_schema/schema_diff.rb', line 43

def _column_names
  _table_exists? ? _connection.columns(_table).map(&:name) : []
end

#_connectionObject



34
35
36
# File 'lib/active_record_schema/schema_diff.rb', line 34

def _connection
  ActiveRecord::Base.connection
end

#_diff_fields_addObject



22
23
24
# File 'lib/active_record_schema/schema_diff.rb', line 22

def _diff_fields_add
  model.schema.fields.values.delete_if {|field| _column_names.include?(field.name.to_s) }
end

#_diff_indexes_addObject



26
27
28
# File 'lib/active_record_schema/schema_diff.rb', line 26

def _diff_indexes_add
  model.schema.indexes.values.delete_if {|index| _index_exists?(index.name) }
end

#_diff_joins_addObject



30
31
32
# File 'lib/active_record_schema/schema_diff.rb', line 30

def _diff_joins_add
  model.schema.joins.values.delete_if {|join| _table_names.include?(join.table.to_s) }
end

#_index_exists?(columns) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/active_record_schema/schema_diff.rb', line 55

def _index_exists?(columns)
  _connection.index_exists?(_table, columns)      
end

#_tableObject



38
39
40
41
# File 'lib/active_record_schema/schema_diff.rb', line 38

def _table
  # prefix = model.parents.find {|p| p.respond_to?(:table_name_prefix)}.try(:table_name_prefix)
  model.table_name
end

#_table_exists?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/active_record_schema/schema_diff.rb', line 51

def _table_exists?
  _connection.table_exists?(_table)
end

#_table_namesObject



47
48
49
# File 'lib/active_record_schema/schema_diff.rb', line 47

def _table_names
  _connection.tables
end

#diff(*args) ⇒ Object



5
6
7
8
# File 'lib/active_record_schema/schema_diff.rb', line 5

def diff(*args)
  diff_method = "_diff_" << args.join('_')
  self.respond_to?(diff_method) ? self.send(diff_method) : []
end

#nothing_to_do?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/active_record_schema/schema_diff.rb', line 10

def nothing_to_do?
  table_exists? && diff(:fields, :add).empty? && diff(:indexes, :add).empty? && diff(:joins, :add).empty? 
end

#prefixed_table_nameObject



18
19
20
# File 'lib/active_record_schema/schema_diff.rb', line 18

def prefixed_table_name
  _table
end

#table_exists?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/active_record_schema/schema_diff.rb', line 14

def table_exists?
  _table_exists?
end