1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
from psqlextra.backend.migrations.state import PostgresListPartitionState
from .partition import PostgresPartitionOperation
class PostgresAddListPartition(PostgresPartitionOperation):
"""Adds a new list partition to a :see:PartitionedPostgresModel."""
def __init__(self, model_name, name, values):
"""Initializes new instance of :see:AddListPartition.
Arguments:
model_name:
The name of the :see:PartitionedPostgresModel.
name:
The name to give to the new partition table.
values:
Partition key values that should be
stored in this partition.
"""
super().__init__(model_name, name)
self.values = values
def state_forwards(self, app_label, state):
model = state.models[(app_label, self.model_name_lower)]
model.add_partition(
PostgresListPartitionState(
app_label=app_label,
model_name=self.model_name,
name=self.name,
values=self.values,
)
)
state.reload_model(app_label, self.model_name_lower)
def database_forwards(self, app_label, schema_editor, from_state, to_state):
model = to_state.apps.get_model(app_label, self.model_name)
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
return name, args, kwargs
def describe(self) -> str:
return "Creates list partition %s on %s" % (self.name, self.model_name)
|