Skip to content

Commit

Permalink
Make AddListPartition and AddRangePartition reversible
Browse files Browse the repository at this point in the history
Fixes #101
  • Loading branch information
Photonios committed Oct 19, 2020
1 parent d9aa57b commit 7d710d8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
7 changes: 7 additions & 0 deletions psqlextra/backend/migrations/operations/add_list_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
if self.allow_migrate_model(schema_editor.connection.alias, model):
schema_editor.add_list_partition(model, self.name, self.values)

def database_backwards(
self, app_label, schema_editor, from_state, to_state
):
model = from_state.apps.get_model(app_label, self.model_name)
if self.allow_migrate_model(schema_editor.connection.alias, model):
schema_editor.delete_partition(model, self.name)

def deconstruct(self):
name, args, kwargs = super().deconstruct()
kwargs["values"] = self.values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
model, self.name, self.from_values, self.to_values
)

def database_backwards(
self, app_label, schema_editor, from_state, to_state
):
model = from_state.apps.get_model(app_label, self.model_name)
if self.allow_migrate_model(schema_editor.connection.alias, model):
schema_editor.delete_partition(model, self.name)

def deconstruct(self):
name, args, kwargs = super().deconstruct()

Expand Down
47 changes: 47 additions & 0 deletions tests/test_migration_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,53 @@ def test_migration_operations_delete_partitioned_table(method, create_model):
assert _partitioned_table_exists(create_operation)


@pytest.mark.parametrize(
"method,add_partition_operation",
[
(
PostgresPartitioningMethod.LIST,
operations.PostgresAddDefaultPartition(
model_name="test", name="pt1"
),
),
(
PostgresPartitioningMethod.RANGE,
operations.PostgresAddRangePartition(
model_name="test",
name="pt1",
from_values="2019-01-01",
to_values="2019-02-01",
),
),
(
PostgresPartitioningMethod.LIST,
operations.PostgresAddListPartition(
model_name="test", name="pt1", values=["car", "boat"]
),
),
],
)
def test_migration_operations_add_partition(
method, add_partition_operation, create_model
):
"""Tests whether adding partitions and then rolling them back works as
expected."""

create_operation = create_model(method)
state = migrations.state.ProjectState.from_apps(apps)

# migrate forwards
apply_migration([create_operation, add_partition_operation], state)
assert _partition_exists(create_operation, add_partition_operation)

# rollback
apply_migration(
[create_operation, add_partition_operation], state, backwards=True
)

assert not _partition_exists(create_operation, add_partition_operation)


@pytest.mark.parametrize(
"method,add_partition_operation,delete_partition_operation",
[
Expand Down

0 comments on commit 7d710d8

Please sign in to comment.