delete_partition.py 1.2 KB

1234567891011121314151617181920212223242526272829
  1. from .partition import PostgresPartitionOperation
  2. class PostgresDeletePartition(PostgresPartitionOperation):
  3. """Deletes a partition that's part of a :see:PartitionedPostgresModel."""
  4. def state_forwards(self, app_label, state):
  5. model = state.models[(app_label, self.model_name_lower)]
  6. model.delete_partition(self.name)
  7. state.reload_model(app_label, self.model_name_lower)
  8. def database_forwards(self, app_label, schema_editor, from_state, to_state):
  9. model = from_state.apps.get_model(app_label, self.model_name)
  10. if self.allow_migrate_model(schema_editor.connection.alias, model):
  11. schema_editor.delete_partition(model, self.name)
  12. def database_backwards(
  13. self, app_label, schema_editor, from_state, to_state
  14. ):
  15. model = to_state.apps.get_model(app_label, self.model_name)
  16. model_state = to_state.models[(app_label, self.model_name)]
  17. if self.allow_migrate_model(schema_editor.connection.alias, model):
  18. partition_state = model_state.partitions[self.name]
  19. schema_editor.add_default_partition(model, partition_state.name)
  20. def describe(self) -> str:
  21. return "Deletes partition %s on %s" % (self.name, self.model_name)