Merge lp:~allenap/maas/purge-commit-within-atomic-block into lp:~maas-committers/maas/trunk

Proposed by Gavin Panella
Status: Merged
Approved by: Gavin Panella
Approved revision: no longer in the source branch.
Merged at revision: 4358
Proposed branch: lp:~allenap/maas/purge-commit-within-atomic-block
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 187 lines (+1/-117)
3 files modified
src/maasserver/utils/orm.py (+0/-41)
src/maasserver/utils/tests/test_orm.py (+1/-60)
src/maasserver/websockets/handlers/device.py (+0/-16)
To merge this branch: bzr merge lp:~allenap/maas/purge-commit-within-atomic-block
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+273624@code.launchpad.net

Commit message

Purge the last remnants of commit_within_atomic_block().

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/utils/orm.py'
2--- src/maasserver/utils/orm.py 2015-09-24 16:22:12 +0000
3+++ src/maasserver/utils/orm.py 2015-10-06 23:57:19 +0000
4@@ -13,7 +13,6 @@
5
6 __metaclass__ = type
7 __all__ = [
8- 'commit_within_atomic_block',
9 'disable_all_database_connections',
10 'enable_all_database_connections',
11 'ExclusivelyConnected',
12@@ -59,7 +58,6 @@
13 from django.db.transaction import TransactionManagementError
14 from django.db.utils import OperationalError
15 from maasserver.utils.async import DeferredHooks
16-from provisioningserver.utils import warn_deprecated
17 from provisioningserver.utils.backoff import (
18 exponential_growth,
19 full_jitter,
20@@ -514,45 +512,6 @@
21 "Savepoints cannot be created outside of a transaction.")
22
23
24-def commit_within_atomic_block(using="default"):
25- """Exits an atomic block then immediately re-enters a new one.
26-
27- This relies on the fact that an atomic block commits when exiting the
28- outer-most context.
29-
30- :deprecated: Absolutely don't use this.
31- """
32- warn_deprecated("use post-commit hooks instead")
33- with outside_atomic_block(using):
34- pass # We just want to exit and enter.
35-
36-
37-@contextmanager
38-def outside_atomic_block(using="default"):
39- """A context manager that guarantees to not contain an atomic block.
40-
41- On entry into this context, this will exit all nested and unnested atomic
42- blocks until it reaches clear air.
43-
44- On exit from this context, the same level of nesting will be
45- reestablished.
46- """
47- connection = transaction.get_connection(using)
48- atomic_context = transaction.atomic(using)
49- assert connection.in_atomic_block
50-
51- depth = 0
52- while connection.in_atomic_block:
53- atomic_context.__exit__(None, None, None)
54- depth = depth + 1
55- try:
56- yield
57- finally:
58- while depth > 0:
59- atomic_context.__enter__()
60- depth = depth - 1
61-
62-
63 def in_transaction(connection=connection):
64 """Is `connection` in the midst of a transaction?
65
66
67=== modified file 'src/maasserver/utils/tests/test_orm.py'
68--- src/maasserver/utils/tests/test_orm.py 2015-09-24 16:22:12 +0000
69+++ src/maasserver/utils/tests/test_orm.py 2015-10-06 23:57:19 +0000
70@@ -31,13 +31,9 @@
71 from django.db.transaction import TransactionManagementError
72 from django.db.utils import OperationalError
73 from maasserver.fields import MAC
74-from maasserver.testing.testcase import (
75- MAASTransactionServerTestCase,
76- SerializationFailureTestCase,
77-)
78+from maasserver.testing.testcase import SerializationFailureTestCase
79 from maasserver.utils import orm
80 from maasserver.utils.orm import (
81- commit_within_atomic_block,
82 disable_all_database_connections,
83 DisabledDatabaseConnection,
84 enable_all_database_connections,
85@@ -52,7 +48,6 @@
86 macs_contain,
87 macs_do_not_contain,
88 make_serialization_failure,
89- outside_atomic_block,
90 post_commit,
91 post_commit_do,
92 post_commit_hooks,
93@@ -781,60 +776,6 @@
94 self.expectThat(connection.savepoint_ids, HasLength(0))
95
96
97-class TestOutsideAtomicBlock(MAASTransactionServerTestCase):
98- """Tests for `outside_atomic_block`."""
99-
100- def test__leaves_and_restores_atomic_block(self):
101- self.assertFalse(connection.in_atomic_block)
102- with transaction.atomic():
103- self.assertTrue(connection.in_atomic_block)
104- with outside_atomic_block():
105- self.assertFalse(connection.in_atomic_block)
106- self.assertTrue(connection.in_atomic_block)
107- self.assertFalse(connection.in_atomic_block)
108-
109- def test__leaves_and_restores_multiple_levels_of_atomic_blocks(self):
110- self.assertFalse(connection.in_atomic_block)
111- with transaction.atomic():
112- with transaction.atomic():
113- with transaction.atomic():
114- with transaction.atomic():
115- with outside_atomic_block():
116- # It leaves the multiple levels of atomic blocks,
117- # but puts the same number of levels back in place
118- # on exit.
119- self.assertFalse(connection.in_atomic_block)
120- self.assertTrue(connection.in_atomic_block)
121- self.assertTrue(connection.in_atomic_block)
122- self.assertTrue(connection.in_atomic_block)
123- self.assertTrue(connection.in_atomic_block)
124- self.assertFalse(connection.in_atomic_block)
125-
126- def test__restores_atomic_block_even_on_error(self):
127- with transaction.atomic():
128- exception_type = factory.make_exception_type()
129- try:
130- with outside_atomic_block():
131- raise exception_type()
132- except exception_type:
133- self.assertTrue(connection.in_atomic_block)
134-
135-
136-class TestCommitWithinAtomicBlock(MAASTransactionServerTestCase):
137- """Tests for `commit_within_atomic_block`."""
138-
139- def test__relies_on_outside_atomic_block(self):
140- outside_atomic_block = self.patch(orm, "outside_atomic_block")
141- with transaction.atomic():
142- commit_within_atomic_block()
143- self.expectThat(outside_atomic_block, MockCalledOnceWith("default"))
144- context_manager = outside_atomic_block.return_value
145- self.expectThat(
146- context_manager.__enter__, MockCalledOnceWith())
147- self.expectThat(
148- context_manager.__exit__, MockCalledOnceWith(None, None, None))
149-
150-
151 class TestInTransaction(DjangoTransactionTestCase):
152 """Tests for `in_transaction`."""
153
154
155=== modified file 'src/maasserver/websockets/handlers/device.py'
156--- src/maasserver/websockets/handlers/device.py 2015-09-24 16:22:12 +0000
157+++ src/maasserver/websockets/handlers/device.py 2015-10-06 23:57:19 +0000
158@@ -31,7 +31,6 @@
159 from maasserver.models.staticipaddress import StaticIPAddress
160 from maasserver.models.subnet import Subnet
161 from maasserver.node_action import compile_node_actions
162-from maasserver.utils.orm import commit_within_atomic_block
163 from maasserver.websockets.base import (
164 HandlerDoesNotExistError,
165 HandlerError,
166@@ -77,21 +76,6 @@
167 return None
168
169
170-def delete_device_and_static_ip_addresses(
171- device, external_static_ips, assigned_sticky_ips):
172- """Delete the created external and sticky ips and the created device.
173-
174- This function calls `commit_within_atomic_block` to force the transaction
175- to be saved.
176- """
177- for static_ip, _ in external_static_ips:
178- static_ip.deallocate()
179- for static_ip, _ in assigned_sticky_ips:
180- static_ip.deallocate()
181- device.delete()
182- commit_within_atomic_block()
183-
184-
185 def log_static_allocations(device, external_static_ips, assigned_sticky_ips):
186 """Log the allocation of the static ip address."""
187 all_ips = [