Skip to content

Commit

Permalink
2.0.0: Some initial support for deleting models and unpin libs (#67)
Browse files Browse the repository at this point in the history
* slightly better for now

* WIP

* update travil.yml

* precise

* update version and add tests for deleted models

* fixed and added test coverage

* update to 2.0.0
  • Loading branch information
JBKahn authored Sep 10, 2017
1 parent 834fa1b commit 3ce3faf
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 40 deletions.
23 changes: 8 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
sudo: false
language: python
python: 3.5
python: 3.6
cache: pip
dist: precise
services:
- mysql
- postgresql
Expand All @@ -21,23 +22,15 @@ env:
- TOX_ENV=py27-dj19
- TOX_ENV=py27-dj110
- TOX_ENV=py27-dj111
- TOX_ENV=py27-djdev
- TOX_ENV=py34-dj18
- TOX_ENV=py34-dj19
- TOX_ENV=py34-dj110
- TOX_ENV=py34-dj111
- TOX_ENV=py34-djdev
- TOX_ENV=py35-dj18
- TOX_ENV=py35-dj19
- TOX_ENV=py35-dj110
- TOX_ENV=py35-dj111
- TOX_ENV=py35-djdev
- TOX_ENV=py36-dj18
- TOX_ENV=py36-dj19
- TOX_ENV=py36-dj110
- TOX_ENV=py36-dj111
- TOX_ENV=py36-djdev
matrix:
fast_finish: true
allow_failures:
- env: TOX_ENV=py27-djdev
- env: TOX_ENV=py34-djdev
- env: TOX_ENV=py35-djdev
- env: TOX_ENV=py36-djdev

before_install:
- psql -c 'CREATE DATABASE "default";' -U postgres
Expand Down
12 changes: 12 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
Changelog
=========

2.0.0 (Sep 10th 2017)

### Small updates

- Unpin `dj-database-url` from a specific minor version to a specific major version.
- Initial support for deleting django models. I'm not going to update the docs just yet
as I don't entirely like this solution...something is better than nothing.
Check the `test_deleted_model_in_settings` tests to see this. The issue is that django
needs to know about a model after you've deleted the class so sharded settings on deleted
models need to be tracked somewhere.


1.2.0 (May 1st 2017)
------------------

Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include *.md
include requirements.txt
include requirements/*.txt
25 changes: 19 additions & 6 deletions django_sharding_library/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,25 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):
# is the app label of the app where the change is defined but to app with the model is
# passed in with the model name.
try:
app = apps.get_app_config(app_label)
model = app.get_model(model_name)
except LookupError:
app_label = model_name.split('.')[0]
app = apps.get_app_config(app_label)
model = app.get_model(model_name[len(app_label) + 1:])
if "." in model_name:
_app_label = model_name.split('.')[0]
app = apps.get_app_config(_app_label)
model = app.get_model(model_name[len(_app_label) + 1:])
else:
app = apps.get_app_config(app_label)
model = app.get_model(model_name)
except LookupError as exception:
deleted_model_settings = getattr(settings, 'DJANGO_SHARDING_SETTINGS', {}).get('DELETED_MODELS', {})
entry = "{}.{}".format(app_label, model_name) if "." not in model_name else model_name
if entry not in deleted_model_settings:
raise exception
deleted_model_data = deleted_model_settings[entry]
if deleted_model_data is None or ("shard_group" not in deleted_model_data and "database" not in deleted_model_data):
return db == "default"
elif "database" in deleted_model_data:
return db == deleted_model_data["database"]
else:
return settings.DATABASES[db]['SHARD_GROUP'] == deleted_model_data["shard_group"]

try:
return is_model_class_on_database(model=model, database=db)
Expand Down
1 change: 0 additions & 1 deletion requirements/_without_django.txt

This file was deleted.

3 changes: 1 addition & 2 deletions requirements/common.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
django>=1.8
-r _without_django.txt
dj-database-url>=0.4.1,<1.0.0
9 changes: 4 additions & 5 deletions requirements/development.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
psycopg2==2.6.2
mock==2.0.0
django_nose==1.4.4
mysqlclient==1.3.7
dj-database-url==0.4.1
psycopg2>=2.6.2
mock>=2.0.0
django_nose>=1.4.4
mysqlclient>=1.3.7
14 changes: 10 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from setuptools import setup, find_packages

version = '1.2.0'
version = '2.0.0'


def get_requirements(file_path):
with open(file_path) as f:
return [line for line in f if line and not line.startswith('-')]


setup(
Expand All @@ -13,8 +18,8 @@
url='https://github.com/JBKahn/django-sharding',
packages=find_packages(),
include_package_data=True,
install_requires=['Django>=1.8', 'dj-database-url==0.4.1'],
tests_require=['psycopg2==2.6.2', 'mysqlclient==1.3.7', 'mock==2.0.0', 'django_nose==1.4.4', 'tox==2.3.1'],
install_requires=get_requirements('requirements/common.txt') + ["django>=1.8,<2.0.0"],
tests_require=get_requirements('requirements/development.txt'),
license="BSD",
zip_safe=False,
keywords='django shard sharding library',
Expand All @@ -24,7 +29,8 @@
'Natural Language :: English',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5'
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6'
],
test_suite='runtests.run_tests',
)
38 changes: 37 additions & 1 deletion tests/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.test import TransactionTestCase
from django.test import TransactionTestCase, override_settings

from tests.models import TestModel, ShardedTestModelIDs, PostgresCustomAutoIDModel, PostgresShardUser
from django_sharding_library.exceptions import InvalidMigrationException
Expand Down Expand Up @@ -524,6 +524,42 @@ def test_lookup_fallback_if_migration_directory_not_the_same_as_the_model(self):
can_migrate_shard=False,
)

@override_settings(DJANGO_SHARDING_SETTINGS={"DELETED_MODELS": {"deleted.Whatever": {"database": "app_shard_002"}}})
def test_deleted_model_in_settings__specific_database(self):
self.assertFalse(self.sut.allow_migrate(model_name="deleted.Whatever", db='default', app_label='deleted', **{}))
self.assertTrue(self.sut.allow_migrate(model_name="deleted.Whatever", db='app_shard_002', app_label='deleted', **{}))

self.assertFalse(self.sut.allow_migrate(model_name="Whatever", db='default', app_label='deleted', **{}))
self.assertTrue(self.sut.allow_migrate(model_name="Whatever", db='app_shard_002', app_label='deleted', **{}))

@override_settings(DJANGO_SHARDING_SETTINGS={"DELETED_MODELS": {"deleted.Whatever": {"shard_group": "default"}}})
def test_deleted_model_in_settings__shard_group(self):
self.assertFalse(self.sut.allow_migrate(model_name="deleted.Whatever", db='default', app_label='deleted', **{}))
self.assertTrue(self.sut.allow_migrate(model_name="deleted.Whatever", db='app_shard_001', app_label='deleted', **{}))
self.assertTrue(self.sut.allow_migrate(model_name="deleted.Whatever", db='app_shard_002', app_label='deleted', **{}))
self.assertFalse(self.sut.allow_migrate(model_name="deleted.Whatever", db='app_shard_001_replica_001', app_label='deleted', **{}))
self.assertFalse(self.sut.allow_migrate(model_name="deleted.Whatever", db='app_shard_001_replica_002', app_label='deleted', **{}))

self.assertFalse(self.sut.allow_migrate(model_name="Whatever", db='default', app_label='deleted', **{}))
self.assertTrue(self.sut.allow_migrate(model_name="Whatever", db='app_shard_001', app_label='deleted', **{}))
self.assertTrue(self.sut.allow_migrate(model_name="Whatever", db='app_shard_002', app_label='deleted', **{}))
self.assertFalse(self.sut.allow_migrate(model_name="Whatever", db='app_shard_001_replica_001', app_label='deleted', **{}))
self.assertFalse(self.sut.allow_migrate(model_name="Whatever", db='app_shard_001_replica_002', app_label='deleted', **{}))

@override_settings(DJANGO_SHARDING_SETTINGS={"DELETED_MODELS": {"deleted.Whatever": None}})
def test_deleted_model_in_settings__unsharded(self):
self.assertTrue(self.sut.allow_migrate(model_name="deleted.Whatever", db='default', app_label='deleted', **{}))
self.assertFalse(self.sut.allow_migrate(model_name="deleted.Whatever", db='app_shard_001', app_label='deleted', **{}))
self.assertFalse(self.sut.allow_migrate(model_name="deleted.Whatever", db='app_shard_002', app_label='deleted', **{}))
self.assertFalse(self.sut.allow_migrate(model_name="deleted.Whatever", db='app_shard_001_replica_001', app_label='deleted', **{}))
self.assertFalse(self.sut.allow_migrate(model_name="deleted.Whatever", db='app_shard_001_replica_002', app_label='deleted', **{}))

self.assertTrue(self.sut.allow_migrate(model_name="Whatever", db='default', app_label='deleted', **{}))
self.assertFalse(self.sut.allow_migrate(model_name="Whatever", db='app_shard_001', app_label='deleted', **{}))
self.assertFalse(self.sut.allow_migrate(model_name="Whatever", db='app_shard_002', app_label='deleted', **{}))
self.assertFalse(self.sut.allow_migrate(model_name="Whatever", db='app_shard_001_replica_001', app_label='deleted', **{}))
self.assertFalse(self.sut.allow_migrate(model_name="Whatever", db='app_shard_001_replica_002', app_label='deleted', **{}))


class RouterForPostgresIDFieldTest(TransactionTestCase):

Expand Down
9 changes: 3 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
[tox]
envlist =
py27-dj{18,19,110,111,dev}
py34-dj{18,19,110,111,dev}
py35-dj{18,19,110,111,dev}
py36-dj{18,19,110,111,dev}

[testenv]
passenv=
Expand All @@ -16,12 +15,10 @@ passenv=
TRAVIS
basepython =
py27: python2.7
py34: python3.4
py35: python3.5
py36: python3.6
deps =
coverage
-r{toxinidir}/requirements/_without_django.txt
-r{toxinidir}/requirements/development.txt
-r{toxinidir}/requirements.txt
dj18: Django>=1.8,<1.9
dj19: Django>=1.9,<1.10
dj110: Django>=1.10,<1.11
Expand Down

0 comments on commit 3ce3faf

Please sign in to comment.