Skip to content

Commit

Permalink
Add skip_runtime_checks so that you can lint django/run parts of it…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
JBKahn authored Jan 27, 2020
1 parent 689fb7f commit f400a6a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
3 changes: 1 addition & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ jobs:
- checkout
- python/load-cache:
key: python-cache-1-11
- python/install-deps:
local: false
- python/install-deps
- python/save-cache:
key: python-cache-1-11
- run:
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

5.2.0 (January 27th 2020)
------------------

- Add `skip_runtime_checks` to `model_config` to allow for use of linting tools where sharded databases aren't setup.

5.1.0 (December 10th 2019)
------------------

Expand Down
6 changes: 3 additions & 3 deletions django_sharding_library/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def configure(cls):
return configure


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

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

postgres_shard_id_fields = list(filter(lambda field: issubclass(type(field), BasePostgresShardGeneratedIDField), cls._meta.fields))
if postgres_shard_id_fields:
if postgres_shard_id_fields and skip_runtime_checks is False:
database_dicts = [settings.DATABASES[database]] if database else [db_settings for db, db_settings in
iteritems(settings.DATABASES) if
db_settings["SHARD_GROUP"] == shard_group]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

version = '5.1.0'
version = '5.2.0'


def get_requirements(file_path):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ def test_cannot_place_database_on_non_existant_db(self):
class ShardedTestModelIDsTwo(TableStrategyModel):
pass

@model_config(database='i_do_not_exist', skip_runtime_checks=True)
class ShardedTestModelIDsTwo(TableStrategyModel):
pass

def test_puts_database_name_on_model_stored_on_another_database(self):
@model_config(database='app_shard_002')
class ShardedTestModelIDsThree(TableStrategyModel):
Expand Down

0 comments on commit f400a6a

Please sign in to comment.