Skip to content

Commit 7d710d8

Browse files
committed
Make AddListPartition and AddRangePartition reversible
Fixes #101
1 parent d9aa57b commit 7d710d8

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

psqlextra/backend/migrations/operations/add_list_partition.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
4343
if self.allow_migrate_model(schema_editor.connection.alias, model):
4444
schema_editor.add_list_partition(model, self.name, self.values)
4545

46+
def database_backwards(
47+
self, app_label, schema_editor, from_state, to_state
48+
):
49+
model = from_state.apps.get_model(app_label, self.model_name)
50+
if self.allow_migrate_model(schema_editor.connection.alias, model):
51+
schema_editor.delete_partition(model, self.name)
52+
4653
def deconstruct(self):
4754
name, args, kwargs = super().deconstruct()
4855
kwargs["values"] = self.values

psqlextra/backend/migrations/operations/add_range_partition.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
5353
model, self.name, self.from_values, self.to_values
5454
)
5555

56+
def database_backwards(
57+
self, app_label, schema_editor, from_state, to_state
58+
):
59+
model = from_state.apps.get_model(app_label, self.model_name)
60+
if self.allow_migrate_model(schema_editor.connection.alias, model):
61+
schema_editor.delete_partition(model, self.name)
62+
5663
def deconstruct(self):
5764
name, args, kwargs = super().deconstruct()
5865

tests/test_migration_operations.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,53 @@ def test_migration_operations_delete_partitioned_table(method, create_model):
126126
assert _partitioned_table_exists(create_operation)
127127

128128

129+
@pytest.mark.parametrize(
130+
"method,add_partition_operation",
131+
[
132+
(
133+
PostgresPartitioningMethod.LIST,
134+
operations.PostgresAddDefaultPartition(
135+
model_name="test", name="pt1"
136+
),
137+
),
138+
(
139+
PostgresPartitioningMethod.RANGE,
140+
operations.PostgresAddRangePartition(
141+
model_name="test",
142+
name="pt1",
143+
from_values="2019-01-01",
144+
to_values="2019-02-01",
145+
),
146+
),
147+
(
148+
PostgresPartitioningMethod.LIST,
149+
operations.PostgresAddListPartition(
150+
model_name="test", name="pt1", values=["car", "boat"]
151+
),
152+
),
153+
],
154+
)
155+
def test_migration_operations_add_partition(
156+
method, add_partition_operation, create_model
157+
):
158+
"""Tests whether adding partitions and then rolling them back works as
159+
expected."""
160+
161+
create_operation = create_model(method)
162+
state = migrations.state.ProjectState.from_apps(apps)
163+
164+
# migrate forwards
165+
apply_migration([create_operation, add_partition_operation], state)
166+
assert _partition_exists(create_operation, add_partition_operation)
167+
168+
# rollback
169+
apply_migration(
170+
[create_operation, add_partition_operation], state, backwards=True
171+
)
172+
173+
assert not _partition_exists(create_operation, add_partition_operation)
174+
175+
129176
@pytest.mark.parametrize(
130177
"method,add_partition_operation,delete_partition_operation",
131178
[

0 commit comments

Comments
 (0)