Projet

Général

Profil

0001-multitenant-show-ETA-on-migrate_schemas-68034.patch

Thomas Noël, 08 août 2022 17:51

Télécharger (4,02 ko)

Voir les différences:

Subject: [PATCH] multitenant: show ETA on migrate_schemas (#68034)

 .../management/commands/migrate_schemas.py    | 11 +++++++++-
 tests_multitenant/test_create_tenant.py       | 20 +++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)
hobo/multitenant/management/commands/migrate_schemas.py
21 21
from django.db import connection
22 22
from django.db.migrations.loader import MigrationLoader
23 23
from django.db.migrations.recorder import MigrationRecorder
24
from django.utils.timezone import localtime
24 25
from tenant_schemas.postgresql_backend.base import FakeTenant
25 26
from tenant_schemas.utils import get_public_schema_name, schema_exists
26 27

  
......
59 60
            all_migrations = set(
60 61
                [(app, migration) for app, migration in loader.disk_migrations if app in app_labels]
61 62
            )
62
            for tenant in TenantMiddleware.get_tenants():
63
            tenants = list(TenantMiddleware.get_tenants())
64
            len_tenants = len(tenants)
65
            start_datetime = localtime()
66
            if int(self.options.get('verbosity', 1)) >= 1 and len(tenants) > 1:
67
                self._notice('=== Start migrate_schemas for %d tenants' % len(tenants))
68
            for step, tenant in enumerate(tenants):
63 69
                connection.set_tenant(tenant, include_public=False)
64 70
                applied_migrations = self.get_applied_migrations(app_labels)
65 71
                if options.get('fake') or options.get('migration_name') or options.get('app_label'):
......
71 77
                        self._notice("=== Skipping migrations of tenant %s" % tenant.domain_url)
72 78
                    continue
73 79
                self.run_migrations(tenant, settings.TENANT_APPS)
80
                if int(self.options.get('verbosity', 1)) >= 1:
81
                    eta = start_datetime + len_tenants * (localtime() - start_datetime) / (step + 1)
82
                    self._notice('=== migrate_schemas ETA: %s' % eta)
74 83

  
75 84
    def get_applied_migrations(self, app_labels):
76 85
        applied_migrations = []
tests_multitenant/test_create_tenant.py
141 141
    with pytest.raises(CommandError) as exc_info:
142 142
        call_command('create_tenant', '--legacy-hostname', 'host1.com', 'host2.com')
143 143
    assert str(exc_info.value) == 'tenant already exists'
144

  
145

  
146
def test_migrate_schemas_eta(db, capsys):
147
    call_command('create_tenant', 'host1.com')
148
    call_command('create_tenant', 'host2.com')
149
    # all skipped, no ETA is displayed
150
    call_command('migrate_schemas', verbosity=1)
151
    captured = capsys.readouterr()
152
    assert 'Start migrate_schemas for 2 tenants' in captured.out
153
    assert 'Skipping migrations of tenant host1.com' in captured.out
154
    assert 'Skipping migrations of tenant host2.com' in captured.out
155
    assert 'migrate_schemas ETA: 2' not in captured.out
156
    # force re-migration and re-migrate, ETA is displayed
157
    call_command('migrate_schemas', 'common', '0001_initial', verbosity=1)
158
    call_command('migrate_schemas', verbosity=1)
159
    captured = capsys.readouterr()
160
    assert 'Start migrate_schemas for 2 tenants' in captured.out
161
    assert 'Running migrate for tenant host1.com' in captured.out
162
    assert 'Running migrate for tenant host2.com' in captured.out
163
    assert 'migrate_schemas ETA: 2' in captured.out
144
-