미세 아니라 많은 사람들이 경험하거나이 꽤 특정 문제에 관심을 것 같다. 나는 여기 저기에 어떤 것을 시험해 보았고, 나는 또한 몇몇 점을 이해하는 데 도움이 된 사우스 메일 링리스트의 지원을 받았다.
나는 한국의 schemamigration를 통해 자동 생성 아주 일반적인 마이그레이션 파일이 있습니다
기본적으로,이 구현 된 솔루션은 다음과 같다. 그러나 테이블 이름을 add_column 및 delete_column에서 schema.table_name
으로 변경했습니다. 멀티 테넌트 미들웨어를 가져 와서 스키마를 제공합니다.
공용 스키마에 대해 스키마가 실행되지 않는 경우에만 마이그레이션이 적용됩니다. 실제로 독립형으로 실행되거나 데이터베이스와 스키마 kwargs로만 실행되는 것이 아니라 새로운 django 명령 인 마이그레이션 실행기에서 실행됩니다.
러너는 안타깝게도 매번 미들웨어를 통과하기 위해 외부로 마이그레이션을 호출해야합니다. 또 다른 트릭은 우리가 이주의 이전 상태를 가져야 만한다는 것입니다. 위조 각 입주자 이전 후 남쪽으로 이전 상태로 가짜입니다. 남쪽의 메일 링리스트에 게시 된
from subprocess import call
import os
from django.core.management.base import BaseCommand
from south.models import MigrationHistory
from myapp.models import MyModel
class Command(BaseCommand):
def handle(self, *args, **options):
#the only allowed arg is the prefix version and it should have a length of 4 (i.e. 0002)
applied = MigrationHistory.objects.filter(app_name='myapp').latest('applied')
current_version = applied.migration[:4]
call_args = ['python', os.path.join('bin', 'manage.py'), 'migrate', 'myorderbird.app.backups']
if len(args) == 1 and len(args[0]) == 4:
call_args.append(args[0])
obje_call_args = None
for obje in MyModel.objects.all():
if obje.schema_exists:
# fake the migration of the previous venue back to the current version
if obje_call_args:
obje_call_args = obje_call_args[:4] + [current_version, '--fake'] + obje_call_args[len(obje_call_args)-3:]
call(obje_call_args)
# migrate the venue in the loop
obje_call_args = list(call_args)
obje_call_args.extend(['--database={}'.format(obje.db), '--schema={}'.format(obje.schema)])
call(venue_call_args)