Skip to content

Commit 918212e

Browse files
authored
5.0.0 (#85)
* remove the magic * update changelog * ready * undo test change?
1 parent 0625897 commit 918212e

38 files changed

+376
-809
lines changed

.circleci/config.yml

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
jobs:
2+
django-2-0:
3+
executor:
4+
name: python/default
5+
tag: 3.8.0
6+
steps:
7+
- checkout
8+
- python/load-cache:
9+
key: python-cache-2-0
10+
- python/install-deps
11+
- python/save-cache:
12+
key: python-cache-2-0
13+
- run:
14+
command: pip install --user "Django>=2.0.0,<2.1.0"
15+
name: Install Django
16+
- run: mkdir /tmp/test-reports
17+
- run:
18+
command: |
19+
python -m pytest \
20+
-vv \
21+
--junitxml /tmp/test-reports/xunit.xml \
22+
--durations 10
23+
name: Test
24+
- store_test_results:
25+
path: /tmp/test-reports/
26+
27+
django-2-1:
28+
executor:
29+
name: python/default
30+
tag: 3.8.0
31+
steps:
32+
- checkout
33+
- python/load-cache:
34+
key: python-cache-2-1
35+
- python/install-deps
36+
- python/save-cache:
37+
key: python-cache-2-1
38+
- run:
39+
command: pip install --user "Django>=2.1.0,<2.2.0"
40+
name: Install Django
41+
- run: mkdir /tmp/test-reports
42+
- run:
43+
command: |
44+
python -m pytest \
45+
-vv \
46+
--junitxml /tmp/test-reports/xunit.xml \
47+
--durations 10
48+
name: Test
49+
- store_test_results:
50+
path: /tmp/test-reports/
51+
52+
django-2-2:
53+
executor:
54+
name: python/default
55+
tag: 3.8.0
56+
steps:
57+
- checkout
58+
- python/load-cache:
59+
key: python-cache-2-2
60+
- python/install-deps
61+
- python/save-cache:
62+
key: python-cache-2-2
63+
- run:
64+
command: pip install --user "Django>=2.2.0,<2.3.0"
65+
name: Install Django
66+
- run: mkdir /tmp/test-reports
67+
- run:
68+
command: |
69+
python -m pytest \
70+
-vv \
71+
--junitxml /tmp/test-reports/xunit.xml \
72+
--durations 10
73+
name: Test
74+
- store_test_results:
75+
path: /tmp/test-reports/
76+
77+
django-1-11:
78+
executor:
79+
name: python/default
80+
tag: 2.7.14
81+
steps:
82+
- checkout
83+
- python/load-cache:
84+
key: python-cache-1-11
85+
- python/install-deps:
86+
local: false
87+
- python/save-cache:
88+
key: python-cache-1-11
89+
- run:
90+
command: pip install --user "Django>=1.11,<1.12" "pytest==4.0.0"
91+
name: Install Django
92+
- run: mkdir /tmp/test-reports
93+
- run:
94+
command: |
95+
python -m pytest \
96+
-vv \
97+
--junitxml /tmp/test-reports/xunit.xml \
98+
--durations 10
99+
name: Test
100+
- store_test_results:
101+
path: /tmp/test-reports/
102+
103+
django-latest:
104+
executor:
105+
name: python/default
106+
tag: 3.8.0
107+
steps:
108+
- checkout
109+
- python/load-cache:
110+
key: python-cache-latest
111+
- python/install-deps
112+
- python/save-cache:
113+
key: python-cache-latest
114+
- run:
115+
command: pip install --user -U "Django>=3.0rc1"
116+
name: Install Django
117+
- run: mkdir /tmp/test-reports
118+
- run:
119+
command: |
120+
python -m pytest \
121+
-vv \
122+
--junitxml /tmp/test-reports/xunit.xml \
123+
--durations 10
124+
name: Test
125+
- store_test_results:
126+
path: /tmp/test-reports/
127+
128+
orbs:
129+
python: circleci/[email protected]
130+
version: 2.1
131+
workflows:
132+
main:
133+
jobs:
134+
- django-2-0
135+
- django-2-1
136+
- django-2-2
137+
- django-1-11
138+
- django-latest

.flake8

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
ignore = E501
3+
exclude = .git,__pycache__,.venv

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
*.py[cod]
22
__pycache__
3+
.mypy_cache/
4+
.DS_Store
35

46
# Packages
57
*.egg

.travis.yml

-50
This file was deleted.

CHANGES.md

+30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
Changelog
22
=========
33

4+
5.0.0 (November 30th 2019)
5+
------------------
6+
7+
This is a combination of maintenance and planning so that I can start down the
8+
road of fixing the documentation and being opinionated about how this works. As
9+
a first step, I'm removing the more magical parts of this library that route the
10+
lookups based on the params passed. It's not a feature I had ever planned to
11+
support but had accepted from an outside contributor. Without consistent support
12+
I've decided to remove it.
13+
14+
### Magic Removal
15+
16+
- Remove `sharded_by_field` from the `model_config` function.
17+
- Removal of `ShardQuerySet` and `ShardManager`.
18+
- Removal of `router.get_shard_for_id_field`, `router.get_shard_for_postgres_pk_field` and support in the router for the magic.
19+
20+
### Less Magical Additions
21+
22+
- Added `ShardLookupQuerySet` and `ShardLookupBaseModel` which help reduce the number of router lookups. WIll add more to docs in upcoming months.
23+
24+
### Limit Support
25+
26+
- Support Django 1.11 and up only.
27+
- Test on python3.8 and on newer versions of django.
28+
29+
### Replace dj_database_url
30+
31+
- Added requirement for `django-environ` and remove usage of `dj_database_url`.
32+
33+
434
4.0.0 (July 16th 2018)
535
------------------
636

conftest.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
def pytest_configure():
2+
from datetime import datetime
3+
import time
4+
5+
try:
6+
import django
7+
from django.conf import settings
8+
from django_sharding_library.settings_helpers import database_configs
9+
except ImportError:
10+
import traceback
11+
traceback.print_exc()
12+
raise ImportError("To fix this error, run: pip install -r requirements-test.txt")
13+
14+
DATABASES = database_configs(databases_dict={
15+
'unsharded_databases': [
16+
{
17+
'name': 'default',
18+
'environment_variable': 'DATABASE_URL',
19+
'default_database_url': 'sqlite://testing123'
20+
}
21+
],
22+
'sharded_databases': [
23+
{
24+
'name': 'app_shard_001',
25+
'environment_variable': 'SHARD_001_DATABASE_URL',
26+
'default_database_url': 'sqlite://testing124',
27+
'replicas': [
28+
{
29+
'name': 'app_shard_001_replica_001',
30+
'environment_variable': 'REPLICA_001_DATABASE_URL',
31+
'default_database_url': 'sqlite://testing125'
32+
},
33+
{
34+
'name': 'app_shard_001_replica_002',
35+
'environment_variable': 'REPLICA_002_DATABASE_URL',
36+
'default_database_url': 'sqlite://testing126'
37+
},
38+
]
39+
},
40+
{
41+
'name': 'app_shard_002',
42+
'environment_variable': 'SHARD_002_DATABASE_URL',
43+
'default_database_url': 'sqlite://testing127'
44+
},
45+
{
46+
'name': 'app_shard_003',
47+
'shard_group': 'postgres',
48+
'environment_variable': 'SHARD_003_DATABASE_URL',
49+
'default_database_url': 'sqlite://testing125'
50+
},
51+
{
52+
'name': 'app_shard_004',
53+
'shard_group': 'postgres',
54+
'environment_variable': 'SHARD_004_DATABASE_URL',
55+
'default_database_url': 'sqlite://testing125'
56+
},
57+
]
58+
})
59+
settings.configure(
60+
DEBUG=True,
61+
USE_TZ=True,
62+
DATABASES=DATABASES,
63+
DATABASE_ROUTERS=['django_sharding_library.router.ShardedRouter'],
64+
AUTH_USER_MODEL='tests.User',
65+
INSTALLED_APPS=[
66+
"django.contrib.auth",
67+
"django.contrib.contenttypes",
68+
"django.contrib.sites",
69+
"django_sharding",
70+
"django_nose",
71+
"tests",
72+
],
73+
SITE_ID=1,
74+
MIDDLEWARE_CLASSES=(),
75+
SHARD_EPOCH=int(time.mktime(datetime(2016, 1, 1).timetuple()) * 1000),
76+
)
77+
django.setup()

django_sharding_library/decorators.py

+1-40
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import django
21
from django.conf import settings
32
from django.apps import apps
43
from django_sharding_library.constants import Backends
54
from django.utils.six import iteritems
6-
from django.db.models import Manager
75

86
from django_sharding_library.exceptions import NonExistentDatabaseException, ShardedModelInitializationException
9-
from django_sharding_library.manager import ShardManager
107
from django_sharding_library.fields import ShardedIDFieldMixin, BasePostgresShardGeneratedIDField
118
from django_sharding_library.utils import register_migration_signal_for_model_receiver
129

@@ -22,7 +19,7 @@ def configure(cls):
2219
return configure
2320

2421

25-
def model_config(shard_group=None, database=None, sharded_by_field=None):
22+
def model_config(shard_group=None, database=None):
2623
"""
2724
A decorator for marking a model as being either sharded or stored on a
2825
particular database. When sharding, it does some verification to ensure
@@ -73,41 +70,5 @@ def configure(cls):
7370
setattr(cls, 'django_sharding__shard_group', shard_group)
7471
setattr(cls, 'django_sharding__is_sharded', True)
7572

76-
# If the sharded by field is set, we will make our custom manager the default manager.
77-
if sharded_by_field:
78-
try:
79-
if not isinstance(cls.objects, ShardManager):
80-
if type(cls.objects) == Manager:
81-
cls.add_to_class('objects', ShardManager())
82-
if django.VERSION < (1, 10):
83-
cls._base_manager = cls.objects
84-
else:
85-
raise ShardedModelInitializationException('You must use the default Django model manager or'
86-
' your custom manager must inherit from '
87-
'``ShardManager``')
88-
except AttributeError as e:
89-
if cls._meta.abstract:
90-
if django.VERSION < (1, 10):
91-
managers = [x[2] for x in cls._meta.abstract_managers]
92-
else:
93-
managers = cls._meta.managers
94-
95-
if not len(managers) > 0:
96-
cls.add_to_class('objects', ShardManager())
97-
elif not any([isinstance(x, ShardManager) for x in managers]):
98-
raise ShardedModelInitializationException(
99-
'Please either do not specify a manager in your '
100-
'abstract base class %s, or if you are using a '
101-
'custom manager, your custom manager must '
102-
'inherit from ``ShardManager``' % cls.__name__
103-
)
104-
else:
105-
# If it gets to this point, the error is a Django error and not a library one. Pass it through.
106-
raise e
107-
setattr(cls, 'django_sharding__sharded_by_field', sharded_by_field)
108-
if not callable(getattr(cls, 'get_shard_from_id', None)):
109-
raise ShardedModelInitializationException('You must define a get_shard_from_id method on the '
110-
'sharded model if you define a "sharded_by_field".')
111-
11273
return cls
11374
return configure

django_sharding_library/fields.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import django
21
from django.apps import apps
32
from django.conf import settings
43
from django.db.models import AutoField, CharField, ForeignKey, BigIntegerField, OneToOneField
@@ -130,10 +129,7 @@ def __init__(self, *args, **kwargs):
130129
if type(model_class) == str:
131130
app_label = model_class.split('.')[0]
132131
app = apps.get_app_config(app_label)
133-
if (1, 11) > django.VERSION:
134-
model_class = app.get_model(model_class[len(app_label) + 1:])
135-
else:
136-
model_class = app.get_model(model_class[len(app_label) + 1:], require_ready=False)
132+
model_class = app.get_model(model_class[len(app_label) + 1:], require_ready=False)
137133
setattr(self, 'django_sharding__shard_storage_table', model_class)
138134
return super(ShardForeignKeyStorageFieldMixin, self).__init__(*args, **kwargs)
139135

0 commit comments

Comments
 (0)