Skip to content

Commit f400a6a

Browse files
authored
Add skip_runtime_checks so that you can lint django/run parts of it without having access to sharded environments (#90)
* Update decorators.py * Update decorators.py * Update config.yml * Update test_decorators.py * Update CHANGES.md * Update setup.py
1 parent 689fb7f commit f400a6a

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

.circleci/config.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ jobs:
8282
- checkout
8383
- python/load-cache:
8484
key: python-cache-1-11
85-
- python/install-deps:
86-
local: false
85+
- python/install-deps
8786
- python/save-cache:
8887
key: python-cache-1-11
8988
- run:

CHANGES.md

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

4+
5.2.0 (January 27th 2020)
5+
------------------
6+
7+
- Add `skip_runtime_checks` to `model_config` to allow for use of linting tools where sharded databases aren't setup.
8+
49
5.1.0 (December 10th 2019)
510
------------------
611

django_sharding_library/decorators.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def configure(cls):
2020
return configure
2121

2222

23-
def model_config(shard_group=None, database=None):
23+
def model_config(shard_group=None, database=None, skip_runtime_checks=False):
2424
"""
2525
A decorator for marking a model as being either sharded or stored on a
2626
particular database. When sharding, it does some verification to ensure
@@ -34,14 +34,14 @@ def configure(cls):
3434
raise ShardedModelInitializationException('The model should be either sharded or stored on a database in the `model_config` decorator is used.')
3535

3636
if database:
37-
if database not in settings.DATABASES or settings.DATABASES[database].get('PRIMARY'):
37+
if (database not in settings.DATABASES or settings.DATABASES[database].get('PRIMARY')) and skip_runtime_checks is False:
3838
raise NonExistentDatabaseException(
3939
'Unable to place {} in {} as that is not an existing primary database in the system.'.format(cls._meta.model_name, database)
4040
)
4141
setattr(cls, 'django_sharding__database', database)
4242

4343
postgres_shard_id_fields = list(filter(lambda field: issubclass(type(field), BasePostgresShardGeneratedIDField), cls._meta.fields))
44-
if postgres_shard_id_fields:
44+
if postgres_shard_id_fields and skip_runtime_checks is False:
4545
database_dicts = [settings.DATABASES[database]] if database else [db_settings for db, db_settings in
4646
iteritems(settings.DATABASES) if
4747
db_settings["SHARD_GROUP"] == shard_group]

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, find_packages
22

3-
version = '5.1.0'
3+
version = '5.2.0'
44

55

66
def get_requirements(file_path):

tests/test_decorators.py

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def test_cannot_place_database_on_non_existant_db(self):
9595
class ShardedTestModelIDsTwo(TableStrategyModel):
9696
pass
9797

98+
@model_config(database='i_do_not_exist', skip_runtime_checks=True)
99+
class ShardedTestModelIDsTwo(TableStrategyModel):
100+
pass
101+
98102
def test_puts_database_name_on_model_stored_on_another_database(self):
99103
@model_config(database='app_shard_002')
100104
class ShardedTestModelIDsThree(TableStrategyModel):

0 commit comments

Comments
 (0)