Class: HasHelpers::OrganizationMerge

Inherits:
Object
  • Object
show all
Defined in:
app/lib/has_helpers/organization_merge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(into: nil, from: nil) ⇒ OrganizationMerge

Returns a new instance of OrganizationMerge.



7
8
9
10
11
12
# File 'app/lib/has_helpers/organization_merge.rb', line 7

def initialize(into: nil, from: nil)
  @merge_into_org = into
  @merge_from_org = from
  @success = false
  post_initialize
end

Instance Attribute Details

#merge_from_orgObject (readonly)

Returns the value of attribute merge_from_org.



5
6
7
# File 'app/lib/has_helpers/organization_merge.rb', line 5

def merge_from_org
  @merge_from_org
end

#merge_into_orgObject (readonly)

Returns the value of attribute merge_into_org.



5
6
7
# File 'app/lib/has_helpers/organization_merge.rb', line 5

def merge_into_org
  @merge_into_org
end

Instance Method Details

#included_tablesObject



21
22
23
# File 'app/lib/has_helpers/organization_merge.rb', line 21

def included_tables
  @included_tables ||= []
end

#merge_orgs(into: nil, from: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/lib/has_helpers/organization_merge.rb', line 25

def merge_orgs(into: nil, from: nil)
  @merge_into_org ||= into
  @merge_from_org ||= from

  ::ActiveRecord::Base.transaction do
    pre_merge_function
    @success = true
  rescue => e
    ::HasHelpers::LogNotifier.notify("Org merge failed: #{e.inspect}")
    @success = false
  end

  if success?
    ::ActiveRecord::Base.transaction do
      disable_triggers
      start_of_merge_transaction_function

      current_num = 0
      tables_to_merge = included_tables.any? ? included_tables : connection.tables
      num_tables = tables_to_merge.length
      tables_to_merge.each do |table|
        current_num += 1
        Rails.logger.info "--------------------------------------------------"
        Rails.logger.info "#{table} (#{current_num}/#{num_tables})"
        next unless connection.column_exists?(table, :organization_id)

        Rails.logger.info "Updating import key for #{table}..."
        connection.execute(update_import_key_sql(table)) if connection.column_exists?(table, :import_key)
        Rails.logger.info "DONE\n"

        Rails.logger.info "Updating org id for #{table}..."
        connection.execute(update_org_id_sql(table))
        Rails.logger.info "DONE\n"
      end

      Rails.logger.info "--------------------------------------------------"
      Rails.logger.info "Committing transaction..."
    rescue => e
      ::HasHelpers::LogNotifier.notify("Org merge failed: #{e.inspect}")
      @success = false
    end
  end

  if success?
    ::ActiveRecord::Base.transaction do
      post_merge_function
    rescue => e
      ::HasHelpers::LogNotifier.notify("Org merge failed: #{e.inspect}")
      @success = false
    end
  end
end

#post_initializeObject



14
15
# File 'app/lib/has_helpers/organization_merge.rb', line 14

def post_initialize
end

#post_merge_functionObject

Used when something needs to happen after the merge. NOTE: THIS OCCURS IN A SEPARATE TRANSACTION AND THE ORG MERGE WILL NOT BE ROLLED BACK IF THIS FAILS!



87
88
# File 'app/lib/has_helpers/organization_merge.rb', line 87

def post_merge_function
end

#pre_merge_functionObject

Used when something needs to happen before the merge. NOTE: THIS OCCURS IN A SEPARATE TRANSACTION AND WILL NOT BE ROLLED BACK IF THE ORG MERGE FAILS!



81
82
# File 'app/lib/has_helpers/organization_merge.rb', line 81

def pre_merge_function
end

#start_of_merge_transaction_functionObject

Allows for custom methods and queries to run in the same transaction as the org merge.



91
92
# File 'app/lib/has_helpers/organization_merge.rb', line 91

def start_of_merge_transaction_function
end

#success?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'app/lib/has_helpers/organization_merge.rb', line 17

def success?
  @success
end