def test_schema_editor_add_range_partition(): """Tests whether adding a range partition works.""" model = define_fake_partitioned_model( { "name": models.TextField(), "timestamp": models.DateTimeField() }, {"key": ["timestamp"]}, ) schema_editor = PostgresSchemaEditor(connection) schema_editor.create_partitioned_model(model) schema_editor.add_range_partition( model, name="mypartition", from_values="2019-1-1", to_values="2019-2-1", comment="test", ) table = db_introspection.get_partitioned_table(model._meta.db_table) assert len(table.partitions) == 1 assert table.partitions[0].name == "mypartition" assert ( table.partitions[0].full_name == f"{model._meta.db_table}_mypartition") assert table.partitions[0].comment == "test" schema_editor.delete_partition(model, "mypartition") table = db_introspection.get_partitioned_table(model._meta.db_table) assert len(table.partitions) == 0
def create( self, model: PostgresPartitionedModel, schema_editor: PostgresSchemaEditor, comment: Optional[str] = None, ) -> None: schema_editor.add_range_partition( model=model, name=self.name(), from_values=self.from_values, to_values=self.to_values, comment=comment, )
def test_schema_editor_create_delete_partitioned_model_range(): """Tests whether creating a partitioned model and adding a list partition to it using the. :see:PostgresSchemaEditor works. """ method = PostgresPartitioningMethod.RANGE key = ["timestamp"] model = define_fake_partitioning_model( {"name": models.TextField(), "timestamp": models.DateTimeField()}, {"method": method, "key": key}, ) schema_editor = PostgresSchemaEditor(connection) schema_editor.create_partitioned_model(model) schema_editor.add_range_partition(model, "pt1", "2019-01-01", "2019-02-01") with connection.cursor() as cursor: introspection = connection.introspection table = introspection.get_partitioned_table( cursor, model._meta.db_table ) assert table.name == model._meta.db_table assert table.method == method assert table.key == key assert table.partitions[0].name == model._meta.db_table + "_pt1" schema_editor.delete_partitioned_model(model) with connection.cursor() as cursor: introspection = connection.introspection table = introspection.get_partitioned_table( cursor, model._meta.db_table ) assert not table partitions = introspection.get_partitions(cursor, model._meta.db_table) assert len(partitions) == 0