Class: OnlineMigrations::BackgroundMigrations::ReconcileRoomStatesForCompletedQuotes

Inherits:
OnlineMigrations::BackgroundMigration
  • Object
show all
Defined in:
lib/online_migrations/background_migrations/reconcile_room_states_for_completed_quotes.rb

Instance Method Summary collapse

Instance Method Details

#countObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/online_migrations/background_migrations/reconcile_room_states_for_completed_quotes.rb', line 38

def count
  # Count unique room IDs from the subquery
  RoomConfiguration
    .joins('INNER JOIN quotes_room_configurations qrc ON qrc.room_configuration_id = room_configurations.id')
    .joins('INNER JOIN quotes q ON q.id = qrc.quote_id')
    .joins('INNER JOIN opportunities opp ON opp.id = room_configurations.opportunity_id')
    .where(state: 'draft')
    .where('q.state = ? OR opp.state = ?', 'complete', 'won')
    .where('NOT EXISTS (SELECT 1 FROM orders_room_configurations orc 
                      INNER JOIN orders o ON o.id = orc.order_id 
                      WHERE orc.room_configuration_id = room_configurations.id 
                      AND o.state NOT IN (\'cancelled\', \'archived\'))')
    .distinct
    .count('room_configurations.id')
end

#process_batch(rooms) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/online_migrations/background_migrations/reconcile_room_states_for_completed_quotes.rb', line 25

def process_batch(rooms)
  # Bulk update all rooms in the batch to 'complete' state using update_all
  # This is equivalent to update_column but for multiple records - skips callbacks and validations
  # More efficient than processing each room individually
  room_ids = rooms.pluck(:id)
  return if room_ids.empty?

  RoomConfiguration.where(id: room_ids).update_all(
    state: 'complete',
    updated_at: Time.current
  )
end

#relationObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/online_migrations/background_migrations/reconcile_room_states_for_completed_quotes.rb', line 6

def relation
  # Use subquery to get unique room IDs, then select from RoomConfiguration
  # This avoids JSON column comparison issues with DISTINCT
  room_ids = RoomConfiguration
    .joins('INNER JOIN quotes_room_configurations qrc ON qrc.room_configuration_id = room_configurations.id')
    .joins('INNER JOIN quotes q ON q.id = qrc.quote_id')
    .joins('INNER JOIN opportunities opp ON opp.id = room_configurations.opportunity_id')
    .where(state: 'draft')
    .where('q.state = ? OR opp.state = ?', 'complete', 'won')
    .where('NOT EXISTS (SELECT 1 FROM orders_room_configurations orc 
                      INNER JOIN orders o ON o.id = orc.order_id 
                      WHERE orc.room_configuration_id = room_configurations.id 
                      AND o.state NOT IN (\'cancelled\', \'archived\'))')
    .group('room_configurations.id')
    .select('room_configurations.id')

  RoomConfiguration.where(id: room_ids)
end