Skip to content

Commit

Permalink
Merge pull request #342 from goodtune/issue-289
Browse files Browse the repository at this point in the history
Drop support for Django < 1.8
  • Loading branch information
bernardopires committed May 20, 2016
2 parents af786ad + cb8862d commit 07841b9
Show file tree
Hide file tree
Showing 26 changed files with 121 additions and 518 deletions.
3 changes: 1 addition & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ created ``Client`` inside an app named ``customers``, your
TENANT_MODEL = "customers.Client" # app.Model
Now run ``migrate_schemas`` (``sync_schemas`` if you're on Django 1.6 and older),
this will sync your apps to the ``public`` schema.
Now run ``migrate_schemas`` to sync your apps to the ``public`` schema.

::

Expand Down
39 changes: 3 additions & 36 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ Once you have defined your model, don't forget to create the migrations for it o

.. code-block:: bash
# Django >= 1.7
python manage.py makemigrations customers
Configure Tenant and Shared Applications
Expand Down Expand Up @@ -113,20 +112,16 @@ You also have to set where your tenant model is.
TENANT_MODEL = "customers.Client" # app.Model
Now run ``migrate_schemas --shared`` (``sync_schemas --shared`` if you're on Django 1.6 or older), this will create the shared apps on the ``public`` schema. Note: your database should be empty if this is the first time you're running this command.
Now run ``migrate_schemas --shared`` to create the shared apps on the ``public`` schema. Note: your database should be empty if this is the first time you're running this command.

.. code-block:: bash
# Django >= 1.7
python manage.py migrate_schemas --shared
# Django < 1.7
python manage.py sync_schemas --shared
.. warning::

Never use ``migrate`` or ``syncdb`` as it would sync *all* your apps to ``public``!
Never use ``migrate`` as it would sync *all* your apps to ``public``!

Lastly, you need to create a tenant whose schema is ``public`` and it's address is your domain URL. Please see the section on :doc:`use <use>`.

You can also specify extra schemas that should be visible to all queries using
Expand All @@ -145,34 +140,6 @@ globally.
available globally. This helps avoid issues caused by hiding the public
schema from queries.

South Migrations
================
If you're on Django 1.6 or older, this app supports `South <http://south.aeracode.org/>`_ so if you haven't configured it yet and would like to:

For Django 1.1 or below

.. code-block:: python
SOUTH_DATABASE_ADAPTER = 'south.db.postgresql_psycopg2'
For Django 1.2 or above

.. code-block:: python
SOUTH_DATABASE_ADAPTERS = {
'default': 'south.db.postgresql_psycopg2',
}
You can list ``south`` under ``TENANT_APPS`` and ``SHARED_APPS`` if you want.

We override ``south``'s ``syncdb`` and ``migrate`` command, so you'll need to change your ``INSTALLED_APPS`` to

.. code-block:: python
INSTALLED_APPS = SHARED_APPS + TENANT_APPS + ('tenant_schemas',)
This makes sure ``tenant_schemas`` is the last on the list and therefore always has precedence when running an overridden command.

Optional Settings
=================

Expand Down
25 changes: 2 additions & 23 deletions docs/use.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ To run only a particular schema, there is an optional argument called ``--schema

.. code-block:: bash
./manage.py sync_schemas --schema=customer1
./manage.py migrate_schemas --schema=customer1
migrate_schemas
~~~~~~~~~~~~~~~

If you're on Django 1.7 or newer, ``migrate_schemas`` is the most important command on this app. The way it works is that it calls Django's ``migrate`` in two different ways. First, it calls ``migrate`` for the ``public`` schema, only syncing the shared apps. Then it runs ``migrate`` for every tenant in the database, this time only syncing the tenant apps.
``migrate_schemas`` is the most important command on this app. The way it works is that it calls Django's ``migrate`` in two different ways. First, it calls ``migrate`` for the ``public`` schema, only syncing the shared apps. Then it runs ``migrate`` for every tenant in the database, this time only syncing the tenant apps.

.. warning::

Expand All @@ -87,27 +87,6 @@ The options given to ``migrate_schemas`` are also passed to every ``migrate``. H
./manage.py migrate_schemas --list
sync_schemas
~~~~~~~~~~~~

If you're on Django 1.6 or older, we also packed ``sync_schemas``. It will also respect the ``SHARED_APPS`` and ``TENANT_APPS`` settings, so if you're syncing the ``public`` schema it will only sync ``SHARED_APPS``. If you're syncing tenants, it will only migrate ``TENANT_APPS``.

.. warning::

You should never directly call ``syncdb``. We perform some magic in order to make ``syncdb`` only sync the appropriate apps.

The options given to ``sync_schemas`` are passed to every ``syncdb``. So if you use South, you may find this handy

.. code-block:: bash
./manage.py sync_schemas --migrate
You can also use the option ``--tenant`` to only sync tenant apps or ``--shared`` to only sync shared apps.

.. code-block:: bash
./manage.py sync_schemas --shared # will only sync the public schema
tenant_command
~~~~~~~~~~~~~~

Expand Down
18 changes: 7 additions & 11 deletions dts_test_project/dts_test_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Django settings for dts_test_project project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
Expand All @@ -14,7 +14,7 @@


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'cl1)b#c&xmm36z3e(quna-vb@ab#&gpjtdjtpyzh!qn%bc^xxn'
Expand Down Expand Up @@ -48,11 +48,7 @@

TEST_RUNNER = 'django.test.runner.DiscoverRunner'

import django
if django.VERSION >= (1, 7, 0):
INSTALLED_APPS = list(set(TENANT_APPS + SHARED_APPS))
else:
INSTALLED_APPS = TENANT_APPS + SHARED_APPS + ('tenant_schemas',)
INSTALLED_APPS = list(set(TENANT_APPS + SHARED_APPS))

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
Expand All @@ -69,7 +65,7 @@


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
'default': {
Expand Down Expand Up @@ -104,7 +100,7 @@
)

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

Expand All @@ -118,7 +114,7 @@


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

Expand Down
4 changes: 0 additions & 4 deletions examples/tenant_tutorial/templates/index_public.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ <h2>First Step: Sync your database</h2>
</ul><br>
<p>Just run the command below on your shell to sync <code>SHARED_APPS</code>. Make sure your environment
has <code>Django</code> and <code>django-tenant-schemas</code> available.</p>
{% if DJANGO17 %}
<pre>$ python manage.py migrate_schemas --shared</pre>
{% else %}
<pre>$ python manage.py sync_schemas --shared</pre>
{% endif %}
<p>When you're done refresh this page.</p>
{% elif no_public_tenant %}
<h2>Second Step: Create a public tenant</h2>
Expand Down
6 changes: 1 addition & 5 deletions examples/tenant_tutorial/tenant_tutorial/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,7 @@

TENANT_MODEL = "customers.Client" # app.Model

import django
if django.VERSION >= (1, 7, 0):
INSTALLED_APPS = list(set(TENANT_APPS + SHARED_APPS))
else:
INSTALLED_APPS = TENANT_APPS + SHARED_APPS + ('tenant_schemas',)
INSTALLED_APPS = list(set(TENANT_APPS + SHARED_APPS))

SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'

Expand Down
4 changes: 0 additions & 4 deletions examples/tenant_tutorial/tenant_tutorial/urls_public.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import django
from django.conf.urls import url
from tenant_tutorial.views import HomeView


urlpatterns = [
url(r'^$', HomeView.as_view()),
]

if django.VERSION < (1, 9, 0):
urlpatterns = django.conf.urls.patterns('', *urlpatterns)
7 changes: 1 addition & 6 deletions examples/tenant_tutorial/tenant_tutorial/urls_tenants.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import django
from django.conf.urls import url
from customers.views import TenantView

from django.conf.urls import url

urlpatterns = [
url(r'^$', TenantView.as_view()),
]

if django.VERSION < (1, 9, 0):
urlpatterns = django.conf.urls.patterns('', *urlpatterns)
7 changes: 2 additions & 5 deletions examples/tenant_tutorial/tenant_tutorial/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import django
from customers.models import Client
from django.conf import settings
from django.db import utils
from django.views.generic import TemplateView

from tenant_schemas.utils import remove_www
from customers.models import Client


class HomeView(TemplateView):
Expand All @@ -14,9 +14,6 @@ def get_context_data(self, **kwargs):

hostname_without_port = remove_www(self.request.get_host().split(':')[0])

if django.VERSION >= (1, 7, 0):
context['DJANGO17'] = True

try:
Client.objects.get(schema_name='public')
except utils.DatabaseError:
Expand Down
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
except ImportError:
from distutils.core import setup

__version__ = "1.4.0"

setup(
name='django-tenant-schemas',
version=get_git_version(),
Expand All @@ -20,7 +18,6 @@
'tenant_schemas.postgresql_backend',
'tenant_schemas.management',
'tenant_schemas.management.commands',
'tenant_schemas.management.commands.legacy',
'tenant_schemas.templatetags',
'tenant_schemas.test',
'tenant_schemas.tests',
Expand All @@ -36,7 +33,7 @@
'Programming Language :: Python',
],
install_requires=[
'Django >= 1.6.0',
'Django >= 1.8.0',
'psycopg2',
],
zip_safe=False,
Expand Down
7 changes: 1 addition & 6 deletions tenant_schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import django
import warnings
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from tenant_schemas.utils import get_public_schema_name, get_tenant_model

from tenant_schemas.utils import get_public_schema_name, get_tenant_model

recommended_config = """
Warning: You should put 'tenant_schemas' at the end of INSTALLED_APPS:
Expand All @@ -23,9 +21,6 @@
if not hasattr(settings, 'TENANT_MODEL'):
raise ImproperlyConfigured('TENANT_MODEL setting not set')

if django.VERSION < (1, 7, 0) and settings.INSTALLED_APPS[-1] != 'tenant_schemas':
warnings.warn(recommended_config, SyntaxWarning)

if 'tenant_schemas.routers.TenantSyncRouter' not in settings.DATABASE_ROUTERS:
raise ImproperlyConfigured("DATABASE_ROUTERS setting must contain "
"'tenant_schemas.routers.TenantSyncRouter'.")
Expand Down
42 changes: 2 additions & 40 deletions tenant_schemas/management/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import django
from optparse import make_option
from django.conf import settings
from django.core.management import call_command, get_commands, load_command_class
from django.core.management.base import BaseCommand, NoArgsCommand, CommandError
from django.db import connection, DEFAULT_DB_ALIAS
from django.core.management.base import BaseCommand, CommandError
from django.db import connection

try:
from django.utils.six.moves import input
Expand Down Expand Up @@ -32,16 +30,6 @@ def __new__(cls, *args, **kwargs):
else:
cmdclass = load_command_class(app_name, obj.COMMAND_NAME)

if django.VERSION < (1, 8, 0):
# inherit the options from the original command
obj.option_list = cmdclass.option_list
obj.option_list += (
make_option("-s", "--schema", dest="schema_name"),
)
obj.option_list += (
make_option("-p", "--skip-public", dest="skip_public", action="store_true", default=False),
)

# prepend the command's original help with the info about schemata iteration
obj.help = "Calls %s for all registered schemata. You can use regular %s options. " \
"Original help for %s: %s" % (obj.COMMAND_NAME, obj.COMMAND_NAME, obj.COMMAND_NAME,
Expand Down Expand Up @@ -83,12 +71,6 @@ def handle(self, *args, **options):


class InteractiveTenantOption(object):
def __init__(self, *args, **kwargs):
super(InteractiveTenantOption, self).__init__(*args, **kwargs)
if django.VERSION < (1, 8, 0):
self.option_list += (
make_option("-s", "--schema", dest="schema_name", help="specify tenant schema"),
)

def add_arguments(self, parser):
parser.add_argument("-s", "--schema", dest="schema_name", help="specify tenant schema")
Expand Down Expand Up @@ -143,28 +125,8 @@ def handle(self, *args, **options):


class SyncCommon(BaseCommand):
if django.VERSION < (1, 8, 0):
option_list = (
make_option('--tenant', action='store_true', dest='tenant', default=False,
help='Tells Django to populate only tenant applications.'),
make_option('--shared', action='store_true', dest='shared', default=False,
help='Tells Django to populate only shared applications.'),
make_option('--app_label', action='store', dest='app_label', nargs='?',
help='App label of an application to synchronize the state.'),
make_option('--migration_name', action='store', dest='migration_name', nargs='?',
help=('Database state will be brought to the state after that '
'migration. Use the name "zero" to unapply all migrations.')),
make_option("-s", "--schema", dest="schema_name"),
)

def __init__(self, stdout=None, stderr=None, no_color=False):
if django.VERSION >= (1, 8, 0):
super(SyncCommon, self).__init__(stdout, stderr, no_color)
else:
super(SyncCommon, self).__init__()

def add_arguments(self, parser):
# for django 1.8 and above
parser.add_argument('--tenant', action='store_true', dest='tenant', default=False,
help='Tells Django to populate only tenant applications.')
parser.add_argument('--shared', action='store_true', dest='shared', default=False,
Expand Down
3 changes: 0 additions & 3 deletions tenant_schemas/management/commands/legacy/__init__.py

This file was deleted.

0 comments on commit 07841b9

Please sign in to comment.