Skip to content

Commit 9b9eb7a

Browse files
AnexenPhotonios
authored andcommitted
Do not create default partition for hash partioning
1 parent 2ffa621 commit 9b9eb7a

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

docs/source/table_partitioning.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Inherit your model from :class:`psqlextra.models.PostgresPartitionedModel` and d
4242

4343
* Use :attr:`psqlextra.types.PostgresPartitioningMethod.RANGE` to ``PARTITION BY RANGE``
4444
* Use :attr:`psqlextra.types.PostgresPartitioningMethod.LIST` to ``PARTITION BY LIST``
45+
* Use :attr:`psqlextra.types.PostgresPartitioningMethod.HASH` to ``PARTITION BY HASH``
4546

4647
.. code-block:: python
4748

psqlextra/backend/migrations/patched_autodetector.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
DeleteModel,
99
RemoveField,
1010
RenameField,
11-
RunSQL,
1211
)
1312
from django.db.migrations.autodetector import MigrationAutodetector
1413
from django.db.migrations.operations.base import Operation
@@ -19,6 +18,7 @@
1918
PostgresPartitionedModel,
2019
PostgresViewModel,
2120
)
21+
from psqlextra.types import PostgresPartitioningMethod
2222

2323
from . import operations
2424

@@ -140,11 +140,12 @@ def add_create_partitioned_model(
140140
partitioning_options = model._partitioning_meta.original_attrs
141141
_, args, kwargs = operation.deconstruct()
142142

143-
self.add(
144-
operations.PostgresAddDefaultPartition(
145-
model_name=model.__name__, name="default"
143+
if partitioning_options["method"] != PostgresPartitioningMethod.HASH:
144+
self.add(
145+
operations.PostgresAddDefaultPartition(
146+
model_name=model.__name__, name="default"
147+
)
146148
)
147-
)
148149

149150
self.add(
150151
operations.PostgresCreatePartitionedModel(

tests/test_make_migrations.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,21 @@ def test_make_migration_create_partitioned_model(fake_app, model_config):
5757
migration = make_migration(model._meta.app_label)
5858
ops = migration.operations
5959

60-
# should have one operation to create the partitioned model
61-
# and one more to add a default partition
62-
assert len(ops) == 2
63-
assert isinstance(ops[0], operations.PostgresCreatePartitionedModel)
64-
assert isinstance(ops[1], operations.PostgresAddDefaultPartition)
60+
if model_config["partitioning_options"]["method"] == PostgresPartitioningMethod.HASH:
61+
# should have one operation to create the partitioned model
62+
# and no default partition
63+
assert len(ops) == 1
64+
assert isinstance(ops[0], operations.PostgresCreatePartitionedModel)
65+
else:
66+
# should have one operation to create the partitioned model
67+
# and one more to add a default partition
68+
assert len(ops) == 2
69+
assert isinstance(ops[0], operations.PostgresCreatePartitionedModel)
70+
assert isinstance(ops[1], operations.PostgresAddDefaultPartition)
71+
72+
# make sure the default partition is named "default"
73+
assert ops[1].model_name == model.__name__
74+
assert ops[1].name == "default"
6575

6676
# make sure the base is set correctly
6777
assert len(ops[0].bases) == 1
@@ -70,10 +80,6 @@ def test_make_migration_create_partitioned_model(fake_app, model_config):
7080
# make sure the partitioning options got copied correctly
7181
assert ops[0].partitioning_options == model_config["partitioning_options"]
7282

73-
# make sure the default partition is named "default"
74-
assert ops[1].model_name == model.__name__
75-
assert ops[1].name == "default"
76-
7783

7884
@postgres_patched_migrations()
7985
def test_make_migration_create_view_model(fake_app):

0 commit comments

Comments
 (0)