Example #1
0
def test_schema_editor_add_default_partition(method, key):
    model = define_fake_partitioned_model(
        {
            "name": models.TextField(),
            "timestamp": models.DateTimeField()
        },
        {
            "method": method,
            "key": key
        },
    )

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

    schema_editor.add_default_partition(model,
                                        name="mypartition",
                                        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_default():
    """Tests whether creating a partitioned model and adding a default
    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_default_partition(model, "default")

    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 + "_default"

    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