tenants: add command showmigrations_schemas (#37683)

This commit is contained in:
Lauréline Guérin 2019-11-15 11:14:55 +01:00
parent 71dfae0ced
commit b6237a23df
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,54 @@
# hobo - portal to configure and deploy applications
# Copyright (C) 2019 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.core.management.commands.showmigrations import Command as ShowMigrationsCommand
from django.db import connection
from django.conf import settings
from tenant_schemas.utils import schema_exists
from hobo.multitenant.middleware import TenantMiddleware
from hobo.multitenant.management.commands import SyncCommon
class ShowMigrationsSchemasCommand(SyncCommon):
help = "Show database schema migrations."
def add_arguments(self, parser):
super(ShowMigrationsSchemasCommand, self).add_arguments(parser)
command = ShowMigrationsCommand()
command.add_arguments(parser)
def handle(self, *args, **options):
super(ShowMigrationsSchemasCommand, self).handle(*args, **options)
if self.schema_name:
if not schema_exists(self.schema_name):
raise RuntimeError('Schema "{}" does not exist'.format(
self.schema_name))
else:
self.run_showmigrations(self.schema_name, settings.TENANT_APPS)
else:
for tenant in TenantMiddleware.get_tenants():
self.run_showmigrations(tenant.schema_name, settings.TENANT_APPS)
def run_showmigrations(self, schema_name, included_apps):
self._notice("=== Show migrations for schema %s" % schema_name)
connection.set_schema(schema_name, include_public=False)
command = ShowMigrationsCommand()
command.execute(*self.args, **self.options)
connection.set_schema_to_public()
Command = ShowMigrationsSchemasCommand

View File

@ -0,0 +1,32 @@
from django.core.management import call_command
import pytest
from test_create_tenant import schema_exists
@pytest.fixture(autouse=True)
def configuration(settings, tmpdir):
settings.TENANT_BASE = str(tmpdir.mkdir('tenants'))
def test_showmigrations_schemas(db, capsys):
assert not schema_exists('www_example_com')
call_command('create_tenant', 'www.example.com')
call_command('showmigrations_schemas', 'common')
captured = capsys.readouterr()
assert '[X] 0001_initial' in captured.out
assert '[X] 0002_auto_20160105_1702' in captured.out
call_command('migrate_schemas', 'common', '0001_initial')
call_command('showmigrations_schemas')
captured = capsys.readouterr()
assert '[X] 0001_initial' in captured.out
assert '[ ] 0002_auto_20160105_1702' in captured.out
call_command('migrate_schemas', 'common')
call_command('showmigrations_schemas')
captured = capsys.readouterr()
assert '[X] 0001_initial' in captured.out
assert '[X] 0002_auto_20160105_1702' in captured.out