1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
import pytest
from django.apps import apps
from django.db import connection, migrations, models
from psqlextra.backend.migrations import operations
from psqlextra.manager import PostgresManager
from psqlextra.models import PostgresPartitionedModel
from psqlextra.types import PostgresPartitioningMethod
from . import db_introspection
from .migrations import apply_migration
def _partitioned_table_exists(op: operations.PostgresCreatePartitionedModel):
"""Checks whether the specified partitioned model operation was succesfully
applied."""
model_table_name = f"tests_{op.name}"
table = db_introspection.get_partitioned_table(model_table_name)
if not table:
return False
part_options = op.partitioning_options
return (
table.method == part_options["method"]
and table.key == part_options["key"]
)
def _partition_exists(model_op, op):
"""Checks whether the parttitioned model and partition operations were
succesfully applied."""
model_table_name = f"tests_{model_op.name}"
table = db_introspection.get_partitioned_table(model_table_name)
if not table:
return False
partition = next(
(
partition
for partition in table.partitions
if partition.full_name == f"{model_table_name}_{op.name}"
),
None,
)
return bool(partition)
@pytest.fixture
def create_model():
"""Factory for creating a :see:PostgresCreatePartitionedModel operation."""
def _create_model(method):
fields = [("name", models.TextField())]
key = []
if method == PostgresPartitioningMethod.RANGE:
key.append("timestamp")
fields.append(("timestamp", models.DateTimeField()))
elif method == PostgresPartitioningMethod.LIST:
key.append("category")
fields.append(("category", models.TextField()))
elif method == PostgresPartitioningMethod.HASH:
key.append("artist_id")
fields.append(("artist_id", models.IntegerField()))
else:
raise NotImplementedError
return operations.PostgresCreatePartitionedModel(
"test",
fields=fields,
bases=(PostgresPartitionedModel,),
managers=[("objects", PostgresManager())],
partitioning_options={"method": method, "key": key},
)
return _create_model
@pytest.mark.postgres_version(lt=110000)
@pytest.mark.parametrize("method", PostgresPartitioningMethod.all())
def test_migration_operations_create_partitioned_table(method, create_model):
"""Tests whether the see :PostgresCreatePartitionedModel operation works as
expected in a migration."""
create_operation = create_model(method)
state = migrations.state.ProjectState.from_apps(apps)
# migrate forwards, is the table there?
apply_migration([create_operation], state)
assert _partitioned_table_exists(create_operation)
# migrate backwards, is the table there?
apply_migration([create_operation], state=state, backwards=True)
assert not _partitioned_table_exists(create_operation)
@pytest.mark.postgres_version(lt=110000)
@pytest.mark.parametrize("method", PostgresPartitioningMethod.all())
def test_migration_operations_delete_partitioned_table(method, create_model):
"""Tests whether the see :PostgresDeletePartitionedModel operation works as
expected in a migration."""
create_operation = create_model(method)
delete_operation = operations.PostgresDeletePartitionedModel(
create_operation.name
)
state = migrations.state.ProjectState.from_apps(apps)
# migrate forwards, create model
apply_migration([create_operation], state)
assert _partitioned_table_exists(create_operation)
# record intermediate state, the state we'll
# migrate backwards to
intm_state = state.clone()
# migrate forwards, delete model
apply_migration([delete_operation], state)
assert not _partitioned_table_exists(create_operation)
# migrate backwards, undelete model
delete_operation.database_backwards(
"tests", connection.schema_editor(), state, intm_state
)
assert _partitioned_table_exists(create_operation)
@pytest.mark.postgres_version(lt=110000)
@pytest.mark.parametrize(
"method,add_partition_operation",
[
(
PostgresPartitioningMethod.LIST,
operations.PostgresAddDefaultPartition(
model_name="test", name="pt1"
),
),
(
PostgresPartitioningMethod.RANGE,
operations.PostgresAddRangePartition(
model_name="test",
name="pt1",
from_values="2019-01-01",
to_values="2019-02-01",
),
),
(
PostgresPartitioningMethod.LIST,
operations.PostgresAddListPartition(
model_name="test", name="pt1", values=["car", "boat"]
),
),
(
PostgresPartitioningMethod.HASH,
operations.PostgresAddHashPartition(
model_name="test", name="pt1", modulus=3, remainder=0
),
),
],
)
def test_migration_operations_add_partition(
method, add_partition_operation, create_model
):
"""Tests whether adding partitions and then rolling them back works as
expected."""
create_operation = create_model(method)
state = migrations.state.ProjectState.from_apps(apps)
# migrate forwards
apply_migration([create_operation, add_partition_operation], state)
assert _partition_exists(create_operation, add_partition_operation)
# rollback
apply_migration(
[create_operation, add_partition_operation], state, backwards=True
)
assert not _partition_exists(create_operation, add_partition_operation)
@pytest.mark.postgres_version(lt=110000)
@pytest.mark.parametrize(
"method,add_partition_operation,delete_partition_operation",
[
(
PostgresPartitioningMethod.LIST,
operations.PostgresAddDefaultPartition(
model_name="test", name="pt1"
),
operations.PostgresDeleteDefaultPartition(
model_name="test", name="pt1"
),
),
(
PostgresPartitioningMethod.RANGE,
operations.PostgresAddRangePartition(
model_name="test",
name="pt1",
from_values="2019-01-01",
to_values="2019-02-01",
),
operations.PostgresDeleteRangePartition(
model_name="test", name="pt1"
),
),
(
PostgresPartitioningMethod.LIST,
operations.PostgresAddListPartition(
model_name="test", name="pt1", values=["car", "boat"]
),
operations.PostgresDeleteListPartition(
model_name="test", name="pt1"
),
),
(
PostgresPartitioningMethod.HASH,
operations.PostgresAddHashPartition(
model_name="test", name="pt1", modulus=3, remainder=0
),
operations.PostgresDeleteHashPartition(
model_name="test", name="pt1"
),
),
],
)
def test_migration_operations_add_delete_partition(
method, add_partition_operation, delete_partition_operation, create_model
):
"""Tests whether adding partitions and then removing them works as
expected."""
create_operation = create_model(method)
state = migrations.state.ProjectState.from_apps(apps)
# migrate forwards, create model and partition
apply_migration([create_operation, add_partition_operation], state)
assert _partition_exists(create_operation, add_partition_operation)
# record intermediate state, the state we'll
# migrate backwards to
intm_state = state.clone()
# migrate forwards, delete the partition
apply_migration([delete_partition_operation], state)
assert not _partition_exists(create_operation, add_partition_operation)
# migrate backwards, undelete the partition
delete_partition_operation.database_backwards(
"tests", connection.schema_editor(), state, intm_state
)
assert _partition_exists(create_operation, add_partition_operation)
|