-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #454 from mcanaves/master
Added parallel migrations. Thank you @mcanaves
- Loading branch information
Showing
14 changed files
with
183 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import os | ||
|
||
from tenant_schemas.migration_executors.base import MigrationExecutor | ||
from tenant_schemas.migration_executors.parallel import ParallelExecutor | ||
from tenant_schemas.migration_executors.standard import StandardExecutor | ||
|
||
|
||
def get_executor(codename=None): | ||
codename = codename or os.environ.get('EXECUTOR', StandardExecutor.codename) | ||
|
||
for klass in MigrationExecutor.__subclasses__(): | ||
if klass.codename == codename: | ||
return klass | ||
|
||
raise NotImplementedError('No executor with codename %s' % codename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import sys | ||
|
||
from django.core.management.commands.migrate import Command as MigrateCommand | ||
from django.db import transaction | ||
|
||
from tenant_schemas.utils import get_public_schema_name | ||
|
||
|
||
def run_migrations(args, options, executor_codename, schema_name, allow_atomic=True): | ||
from django.core.management import color | ||
from django.core.management.base import OutputWrapper | ||
from django.db import connection | ||
|
||
style = color.color_style() | ||
|
||
def style_func(msg): | ||
return '[%s:%s] %s' % ( | ||
style.NOTICE(executor_codename), | ||
style.NOTICE(schema_name), | ||
msg | ||
) | ||
|
||
stdout = OutputWrapper(sys.stdout) | ||
stdout.style_func = style_func | ||
stderr = OutputWrapper(sys.stderr) | ||
stderr.style_func = style_func | ||
if int(options.get('verbosity', 1)) >= 1: | ||
stdout.write(style.NOTICE("=== Running migrate for schema %s" % schema_name)) | ||
|
||
connection.set_schema(schema_name) | ||
MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options) | ||
|
||
try: | ||
transaction.commit() | ||
connection.close() | ||
connection.connection = None | ||
except transaction.TransactionManagementError: | ||
if not allow_atomic: | ||
raise | ||
|
||
# We are in atomic transaction, don't close connections | ||
pass | ||
|
||
connection.set_schema_to_public() | ||
|
||
|
||
class MigrationExecutor(object): | ||
codename = None | ||
|
||
def __init__(self, args, options): | ||
self.args = args | ||
self.options = options | ||
|
||
def run_migrations(self, tenants): | ||
public_schema_name = get_public_schema_name() | ||
|
||
if public_schema_name in tenants: | ||
run_migrations(self.args, self.options, self.codename, public_schema_name) | ||
tenants.pop(tenants.index(public_schema_name)) | ||
|
||
self.run_tenant_migrations(tenants) | ||
|
||
def run_tenant_migrations(self, tenant): | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import functools | ||
import multiprocessing | ||
|
||
from django.conf import settings | ||
|
||
from tenant_schemas.migration_executors.base import MigrationExecutor, run_migrations | ||
|
||
|
||
class ParallelExecutor(MigrationExecutor): | ||
codename = 'parallel' | ||
|
||
def run_tenant_migrations(self, tenants): | ||
if tenants: | ||
processes = getattr(settings, 'TENANT_PARALLEL_MIGRATION_MAX_PROCESSES', 2) | ||
chunks = getattr(settings, 'TENANT_PARALLEL_MIGRATION_CHUNKS', 2) | ||
|
||
from django.db import connection | ||
|
||
connection.close() | ||
connection.connection = None | ||
|
||
run_migrations_p = functools.partial( | ||
run_migrations, | ||
self.args, | ||
self.options, | ||
self.codename, | ||
allow_atomic=False | ||
) | ||
p = multiprocessing.Pool(processes=processes) | ||
p.map(run_migrations_p, tenants, chunks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from tenant_schemas.migration_executors.base import MigrationExecutor, run_migrations | ||
|
||
|
||
class StandardExecutor(MigrationExecutor): | ||
codename = 'standard' | ||
|
||
def run_tenant_migrations(self, tenants): | ||
for schema_name in tenants: | ||
run_migrations(self.args, self.options, self.codename, schema_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters