Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit 129f5dd

Browse files
monounoweb-flow
andauthored
Fix several issues
Co-authored-by: monouno <noreply@github.com>
1 parent 40ff81e commit 129f5dd

File tree

10 files changed

+261
-118
lines changed

10 files changed

+261
-118
lines changed

README.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@ django-pg-partitioning
1717
.. image:: https://codecov.io/gh/chaitin/django-pg-partitioning/branch/master/graph/badge.svg
1818
:target: https://codecov.io/gh/chaitin/django-pg-partitioning
1919

20+
⚠️
21+
----
22+
23+
目前已经有了更好用的 Django 插件(比如 django-postgres-extra)使得基于 Django 开发的项目能够方便地使用 PostgreSQL 数据库的高级功能,因此本项目不再维护。你依然可以 fork 本项目并进行二次开发,祝你生活愉快 :)
24+
25+
There are already better Django plugins (such as django-postgres-extra) that make it easy for Django-based projects to use the advanced features of PostgreSQL databases, so this project is no longer maintained. You can still fork this project and do secondary development, have a nice life :)
26+
27+
----
28+
2029
一个支持 PostgreSQL 11 原生表分区的 Django 扩展,使您可以在 Django 中创建分区表并管理它们。目前它支持两种分区类型:
2130

2231
- 时间范围分区(Time Range Partitioning):将时序数据分开存储到不同的时间范围分区表中,支持创建连续且不重叠的时间范围分区并进行归档管理。
2332
- 列表分区(List Partitioning):根据分区字段的确定值将数据分开存储到不同的分区表中。
2433

25-
----
26-
2734
A Django extension that supports PostgreSQL 11 native table partitioning, allowing you to create partitioned tables in Django
2835
and manage them. Currently it supports the following two partition types:
2936

dev/postgres_init.sh

100644100755
File mode changed.

pg_partitioning/decorators.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Type
33

44
from django.db import models
5+
56
from pg_partitioning.manager import ListPartitionManager, TimeRangePartitionManager
67

78
logger = logging.getLogger(__name__)
@@ -75,8 +76,5 @@ class MyLog(models.Model):
7576

7677
def __call__(self, model: Type[models.Model]):
7778
super().__call__(model)
78-
support_field_types = [item.get_internal_type() for item in [models.TextField(), models.BooleanField(), models.IntegerField()]]
79-
if model._meta.get_field(self.partition_key).get_internal_type() not in support_field_types:
80-
raise NotImplementedError("The partition_key does not support this field type.")
8179
model.partitioning = ListPartitionManager(model, self.partition_key, self.options)
8280
return model

pg_partitioning/manager.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from django.db import IntegrityError, models
99
from django.db.models import Q
1010
from django.utils import timezone
11+
1112
from pg_partitioning.shortcuts import double_quote, execute_sql, generate_set_indexes_tablespace_sql, single_quote
1213

1314
from .constants import (
@@ -197,13 +198,15 @@ def create_partition(self, partition_name: str, value: Union[str, int, bool, Non
197198
tablespace(str): Partition tablespace name.
198199
"""
199200

200-
sql_sequence = [
201-
SQL_CREATE_LIST_PARTITION % {"parent": double_quote(self.model._meta.db_table), "child": double_quote(partition_name), "value": _db_value(value)}
202-
]
201+
create_partition_sql = SQL_CREATE_LIST_PARTITION % {
202+
"parent": double_quote(self.model._meta.db_table),
203+
"child": double_quote(partition_name),
204+
"value": _db_value(value),
205+
}
203206
if tablespace:
204-
sql_sequence[0] += SQL_APPEND_TABLESPACE % {"tablespace": tablespace}
205-
sql_sequence.extend(generate_set_indexes_tablespace_sql(partition_name, tablespace))
206-
execute_sql(sql_sequence)
207+
create_partition_sql += SQL_APPEND_TABLESPACE % {"tablespace": tablespace}
208+
execute_sql(create_partition_sql)
209+
execute_sql(generate_set_indexes_tablespace_sql(partition_name, tablespace))
207210

208211
def attach_partition(self, partition_name: str, value: Union[str, int, bool, None], tablespace: str = None) -> None:
209212
"""Attach partitions.
@@ -219,8 +222,7 @@ def attach_partition(self, partition_name: str, value: Union[str, int, bool, Non
219222
sql_sequence.append(SQL_SET_TABLE_TABLESPACE % {"name": double_quote(partition_name), "tablespace": tablespace})
220223
sql_sequence.extend(generate_set_indexes_tablespace_sql(partition_name, tablespace))
221224
sql_sequence.append(
222-
SQL_ATTACH_LIST_PARTITION
223-
% {"parent": double_quote(self.model._meta.db_table), "child": double_quote(partition_name), "value": single_quote(_db_value(value))}
225+
SQL_ATTACH_LIST_PARTITION % {"parent": double_quote(self.model._meta.db_table), "child": double_quote(partition_name), "value": _db_value(value)}
224226
)
225227
execute_sql(sql_sequence)
226228

pg_partitioning/models.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.apps import apps
22
from django.db import models, transaction
3+
34
from pg_partitioning.signals import post_attach_partition, post_create_partition, post_detach_partition
45

56
from .constants import (
@@ -75,23 +76,20 @@ def save(self, force_insert=False, force_update=False, using=None, update_fields
7576

7677
model = apps.get_model(self.config.model_label)
7778
if self._state.adding:
78-
sql_sequence = [
79-
SQL_CREATE_TIME_RANGE_PARTITION
80-
% {
81-
"parent": double_quote(model._meta.db_table),
82-
"child": double_quote(self.table_name),
83-
"date_start": single_quote(self.start.isoformat()),
84-
"date_end": single_quote(self.end.isoformat()),
85-
}
86-
]
79+
create_partition_sql = SQL_CREATE_TIME_RANGE_PARTITION % {
80+
"parent": double_quote(model._meta.db_table),
81+
"child": double_quote(self.table_name),
82+
"date_start": single_quote(self.start.isoformat()),
83+
"date_end": single_quote(self.end.isoformat()),
84+
}
8785

8886
if self.config.attach_tablespace:
89-
sql_sequence[0] += SQL_APPEND_TABLESPACE % {"tablespace": self.config.attach_tablespace}
90-
sql_sequence.extend(generate_set_indexes_tablespace_sql(self.table_name, self.config.attach_tablespace))
87+
create_partition_sql += SQL_APPEND_TABLESPACE % {"tablespace": self.config.attach_tablespace}
9188

9289
with transaction.atomic():
9390
super().save(force_insert, force_update, using, update_fields)
94-
execute_sql(sql_sequence)
91+
execute_sql(create_partition_sql)
92+
execute_sql(generate_set_indexes_tablespace_sql(self.table_name, self.config.attach_tablespace))
9593
post_create_partition.send(sender=model, partition_log=self)
9694
else:
9795
with transaction.atomic():

pg_partitioning/patch/schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from django.apps.config import MODELS_MODULE_NAME
55
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
6+
67
from pg_partitioning.manager import _PartitionManagerBase
78

89
logger = logging.getLogger(__name__)

pg_partitioning/shortcuts.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import List, Optional, Tuple, Union
33

44
from django.db import connection
5+
56
from pg_partitioning.constants import SQL_DROP_TABLE, SQL_GET_TABLE_INDEXES, SQL_SET_INDEX_TABLESPACE, SQL_SET_TABLE_TABLESPACE, SQL_TRUNCATE_TABLE
67

78
logger = logging.getLogger(__name__)
@@ -23,8 +24,12 @@ def double_quote(name: str) -> str:
2324
return '"%s"' % name
2425

2526

26-
def execute_sql(sql_sequence: Union[str, List[str], Tuple[str]], fetch: bool = False) -> Optional[Tuple]:
27+
def execute_sql(sql_sequence: Union[str, List[str], Tuple[str]], fetch: bool = False) -> Optional[List]:
2728
"""Execute SQL sequence and returning result."""
29+
if not sql_sequence:
30+
if fetch:
31+
return []
32+
return
2833

2934
sql_str = ""
3035
for statement in sql_sequence if isinstance(sql_sequence, (list, tuple)) else [sql_sequence]:

tests/models.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.contrib.postgres.indexes import BrinIndex
22
from django.db import models
33
from django.utils import timezone
4+
45
from pg_partitioning.constants import PeriodType
56
from pg_partitioning.decorators import ListPartitioning, TimeRangePartitioning
67

@@ -29,6 +30,18 @@ class Meta:
2930

3031

3132
@ListPartitioning(partition_key="category")
32-
class ListTable(models.Model):
33+
class ListTableText(models.Model):
3334
category = models.TextField(default="A", null=True, blank=True)
3435
timestamp = models.DateTimeField(default=timezone.now)
36+
37+
38+
@ListPartitioning(partition_key="category")
39+
class ListTableInt(models.Model):
40+
category = models.IntegerField(default=0, null=True)
41+
timestamp = models.DateTimeField(default=timezone.now)
42+
43+
44+
@ListPartitioning(partition_key="category")
45+
class ListTableBool(models.Model):
46+
category = models.NullBooleanField(default=False, null=True)
47+
timestamp = models.DateTimeField(default=timezone.now)

0 commit comments

Comments
 (0)