Last active
May 18, 2023 09:24
-
-
Save hirokazumiyaji/dcfed5b8114e8469f864 to your computer and use it in GitHub Desktop.
django parallel migrate(per host)
This file contains hidden or 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
# coding: utf-8 | |
from __future__ import absolute_import, unicode_literals | |
from collections import defaultdict | |
import commands | |
from multiprocessing import Process | |
from django.conf import settings | |
from django.core.management.base import BaseCommand | |
class Command(BaseCommand): | |
def handle(self, *args, **options): | |
hosts = self._get_hosts() | |
processes = self._gen_processes(hosts) | |
print("start.") | |
self._start_processes(processes) | |
self._join_processes(processes) | |
print("end.") | |
def _get_hosts(self): | |
hosts = defaultdict(dict) | |
for name, database in settings.DATABASES.items(): | |
hosts[database['HOST']][name] = database | |
return hosts | |
def _gen_processes(self, hosts): | |
return [ | |
Process(target=execute, args=(database.keys(),)) | |
for database in hosts.values() | |
] | |
def _start_processes(self, processes): | |
for process in processes: | |
process.start() | |
def _join_processes(self, processes): | |
for process in processes: | |
process.join() | |
def execute(names): | |
for name in names: | |
print("migrate --database={}".format(name)) | |
_, output = commands.getstatusoutput("./manage.py migrate --database={}".format(name)) | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment