Example #1
0
def test_schema_editor_add_list_partition():
    """Tests whether adding a list partition works."""

    model = define_fake_partitioned_model(
        {"name": models.TextField()},
        {
            "method": PostgresPartitioningMethod.LIST,
            "key": ["name"]
        },
    )

    schema_editor = PostgresSchemaEditor(connection)
    schema_editor.create_partitioned_model(model)

    schema_editor.add_list_partition(model,
                                     name="mypartition",
                                     values=["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 test_schema_editor_create_delete_partitioned_model_list():
    """Tests whether creating a partitioned model and adding a range partition
    to it using the.

    :see:PostgresSchemaEditor works.
    """

    method = PostgresPartitioningMethod.LIST
    key = ["category"]

    model = define_fake_partitioning_model(
        {"name": models.TextField(), "category": models.TextField()},
        {"method": method, "key": key},
    )

    schema_editor = PostgresSchemaEditor(connection)
    schema_editor.create_partitioned_model(model)

    schema_editor.add_list_partition(model, "pt1", ["car", "boat"])

    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