Module: Foobara::Persistence::EntityBase::Transaction::Concerns::StateTransitions
- Defined in:
- foobara-0.2.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb
Instance Method Summary collapse
- #commit! ⇒ Object
- #commit_nested! ⇒ Object
- #currently_open? ⇒ Boolean
-
#each_table ⇒ Object
TODO: this belongs elsewhere.
- #flush! ⇒ Object
- #open! ⇒ Object
- #open_nested!(outer_tx) ⇒ Object
-
#revert! ⇒ Object
Should communicate somehow that this is only in-memory.
- #rollback!(because_of = nil) ⇒ Object
- #rollback_nested!(because_of = nil) ⇒ Object
Instance Method Details
#commit! ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 55 def commit! return commit_nested! if nested? state_machine.commit! do each_table(&:commit!) entity_attributes_crud_driver.commit_transaction(raw_tx) each_table(&:committed) each_table(&:transaction_closed) end rescue => e # :nocov: rollback!(e) raise # :nocov: end |
#commit_nested! ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 71 def commit_nested! state_machine.commit_nested! do each_table(&:commit!) entity_attributes_crud_driver.flush_transaction(raw_tx) each_table(&:transaction_closed) end rescue => e # :nocov: rollback!(e) raise # :nocov: end |
#currently_open? ⇒ Boolean
10 11 12 |
# File 'projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 10 def currently_open? state_machine.currently_open? end |
#each_table ⇒ Object
TODO: this belongs elsewhere
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 85 def each_table(&) @ordered_tables ||= if tables.size <= 1 tables.values else entity_class_to_table = {} entity_classes = [] tables.each_value do |table| entity_class = table.entity_class entity_classes << entity_class entity_class_to_table[entity_class] = table end ordered_entity_classes = EntityBase.order_entity_classes(entity_classes) ordered_entity_classes.map do |entity_class| entity_class_to_table[entity_class] end end @ordered_tables.each(&) end |
#flush! ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 27 def flush! state_machine.flush! do each_table(&:validate!) each_table(&:flush_created!) each_table(&:flush_updated_and_hard_deleted!) end entity_attributes_crud_driver.flush_transaction(raw_tx) rescue => e # :nocov: rollback!(e) raise # :nocov: end |
#open! ⇒ Object
14 15 16 17 18 |
# File 'projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 14 def open! state_machine.open! do self.raw_tx = entity_attributes_crud_driver.open_transaction end end |
#open_nested!(outer_tx) ⇒ Object
20 21 22 23 24 25 |
# File 'projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 20 def open_nested!(outer_tx) state_machine.open_nested! do self.is_nested = true self.raw_tx = outer_tx.raw_tx end end |
#revert! ⇒ Object
Should communicate somehow that this is only in-memory. TODO: support multiple-reverts for databases that support checkpoints
43 44 45 46 47 48 49 50 51 52 53 |
# File 'projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 43 def revert! state_machine.revert! do each_table(&:revert!) end entity_attributes_crud_driver.revert_transaction(raw_tx) rescue => e # :nocov: rollback!(e) raise # :nocov: end |
#rollback!(because_of = nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 108 def rollback!(because_of = nil) return rollback_nested!(because_of) if nested? state_machine.rollback! do # TODO: raise error if already flushed and if crud_driver doesn't support true transactions entity_attributes_crud_driver.rollback_transaction(raw_tx) each_table(&:rollback!) end each_table(&:transaction_closed) if !because_of && (self == entity_base.current_transaction) raise RolledBack, "intentionally rolled back" end rescue state_machine.error! if state_machine.currently_open? raise end |
#rollback_nested!(because_of = nil) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 127 def rollback_nested!(because_of = nil) state_machine.rollback_nested! do entity_attributes_crud_driver.revert_transaction(raw_tx) each_table(&:revert!) end each_table(&:transaction_closed) if !because_of && (self == entity_base.current_transaction) raise RolledBack, "intentionally rolled back" end rescue state_machine.error! if state_machine.currently_open? raise end |