add_default_partition.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from psqlextra.backend.migrations.state import PostgresPartitionState
  2. from .partition import PostgresPartitionOperation
  3. class PostgresAddDefaultPartition(PostgresPartitionOperation):
  4. """Adds a new default partition to a :see:PartitionedPostgresModel."""
  5. def state_forwards(self, app_label, state):
  6. model_state = state.models[(app_label, self.model_name_lower)]
  7. model_state.add_partition(
  8. PostgresPartitionState(
  9. app_label=app_label, model_name=self.model_name, name=self.name
  10. )
  11. )
  12. state.reload_model(app_label, self.model_name_lower)
  13. def database_forwards(self, app_label, schema_editor, from_state, to_state):
  14. model = to_state.apps.get_model(app_label, self.model_name)
  15. if self.allow_migrate_model(schema_editor.connection.alias, model):
  16. schema_editor.add_default_partition(model, self.name)
  17. def database_backwards(
  18. self, app_label, schema_editor, from_state, to_state
  19. ):
  20. model = from_state.apps.get_model(app_label, self.model_name)
  21. if self.allow_migrate_model(schema_editor.connection.alias, model):
  22. schema_editor.delete_partition(model, self.name)
  23. def describe(self) -> str:
  24. return "Creates default partition '%s' on %s" % (
  25. self.name,
  26. self.model_name,
  27. )