multitenant: hook MigrationRecorder::has_table for a speedup (#81814)
gitea/hobo/pipeline/head This commit looks good Details

This commit is contained in:
Pierre Ducroquet 2023-09-29 16:28:09 +02:00
parent 5c2b51f671
commit 2fbdd2015e
1 changed files with 8 additions and 0 deletions

View File

@ -19,6 +19,7 @@ from django.conf import settings
from django.core.management.commands.migrate import Command as MigrateCommand
from django.db import connection
from django.db.migrations.loader import MigrationLoader
from django.db.migrations.recorder import MigrationRecorder
from django.utils.timezone import localtime
from hobo.multitenant.management.commands import SyncCommon
@ -91,16 +92,23 @@ class MigrateSchemasCommand(SyncCommon):
def run_migrations(self, tenant, included_apps, step=1, steps=1):
if int(self.options.get('verbosity', 1)) >= 1:
self._notice('=== Running migrate for tenant %s (%s/%s)' % (tenant.domain_url, step, steps))
# Hook recorder has_table function for a nice speedup
original_has_table = MigrationRecorder.has_table
MigrationRecorder.has_table = lambda x: True
connection.set_tenant(tenant, include_public=False)
command = MigrateCommand()
command.requires_system_checks = False
command.requires_migrations_checks = False
command.execute(*self.args, **self.options)
connection.set_schema_to_public()
# restore recorder
MigrationRecorder.has_table = original_has_table
def run_migrations_on_schema(self, schema, included_apps):
if int(self.options.get('verbosity', 1)) >= 1:
self._notice('=== Running migrate for schema %s' % schema)
connection.set_schema(schema, include_public=False)
command = MigrateCommand()
command.requires_system_checks = False