Restore the restoration state taken before a transaction began. Corresponds to a rollback.
(self, dirty_only: bool = False)
| 1107 | self._key_switches = weakref.WeakKeyDictionary() |
| 1108 | |
| 1109 | def _restore_snapshot(self, dirty_only: bool = False) -> None: |
| 1110 | """Restore the restoration state taken before a transaction began. |
| 1111 | |
| 1112 | Corresponds to a rollback. |
| 1113 | |
| 1114 | """ |
| 1115 | assert self._is_transaction_boundary |
| 1116 | |
| 1117 | to_expunge = set(self._new).union(self.session._new) |
| 1118 | self.session._expunge_states(to_expunge, to_transient=True) |
| 1119 | |
| 1120 | for s, (oldkey, newkey) in self._key_switches.items(): |
| 1121 | # we probably can do this conditionally based on |
| 1122 | # if we expunged or not, but safe_discard does that anyway |
| 1123 | self.session.identity_map.safe_discard(s) |
| 1124 | |
| 1125 | # restore the old key |
| 1126 | s.key = oldkey |
| 1127 | |
| 1128 | # now restore the object, but only if we didn't expunge |
| 1129 | if s not in to_expunge: |
| 1130 | self.session.identity_map.replace(s) |
| 1131 | |
| 1132 | for s in set(self._deleted).union(self.session._deleted): |
| 1133 | self.session._update_impl(s, revert_deletion=True) |
| 1134 | |
| 1135 | assert not self.session._deleted |
| 1136 | |
| 1137 | for s in self.session.identity_map.all_states(): |
| 1138 | if not dirty_only or s.modified or s in self._dirty: |
| 1139 | s._expire(s.dict, self.session.identity_map._modified) |
| 1140 | |
| 1141 | def _remove_snapshot(self) -> None: |
| 1142 | """Remove the restoration state taken before a transaction began. |
no test coverage detected