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
Example #2
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_partitioned_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")

    table = db_introspection.get_partitioned_table(model._meta.db_table)
    assert table.name == model._meta.db_table
    assert table.method == method
    assert table.key == key
    assert table.partitions[0].full_name == model._meta.db_table + "_default"

    schema_editor.delete_partitioned_model(model)

    table = db_introspection.get_partitioned_table(model._meta.db_table)
    assert not table

    partitions = db_introspection.get_partitions(model._meta.db_table)
    assert len(partitions) == 0
Example #3
0
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_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_range_partition(model, "pt1", "2019-01-01", "2019-02-01")

    table = db_introspection.get_partitioned_table(model._meta.db_table)
    assert table.name == model._meta.db_table
    assert table.method == method
    assert table.key == key
    assert table.partitions[0].full_name == model._meta.db_table + "_pt1"

    schema_editor.delete_partitioned_model(model)

    table = db_introspection.get_partitioned_table(model._meta.db_table)
    assert not table

    partitions = db_introspection.get_partitions(model._meta.db_table)
    assert len(partitions) == 0