[英]Django South: detecting if fake migrations were applied in production
我正在將South添加到具有許多安裝的現有應用程序中,但由於安全原因,我無法訪問生產環境。
我們只能提供Python安裝腳本,這些腳本將由通常不了解Django,South等的人員運行。
我知道對於現有安裝,任何未來的升級都必須從執行開始:
manage.py syncdb
manage.py migrate --all 0001 --fake
任何新安裝都將從以下開始:
manage.py syncdb
manage.py migrate -all
有沒有辦法檢測是否已經應用了南方初始遷移(例如,通過檢測是否存在south_migfationhistory表)以數據庫無關的方式(可能使用Django本身)?
我想做的是:
(pseudocode)
db = database.connect(dbname, user, password)
if db.table_existst('south_migrationhistory'):
execute 'manage.py syncdb'
execute 'manage.py migrate --all'
else:
execute 'manage.py syncdb'
execute 'manage.py migrate --all 0001 --fake'
execute 'manage.py migrate --all'
為了將來參考,這是我最終這樣做的方式:
if args.settings_dir not in sys.path:
sys.path.append(args.settings_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
#try to query db for existing objects, e.g. user groups
#in order to detect if we are upgrading an existing installation
from django.db.utils import DatabaseError
try:
from django.contrib.auth.models import Group
tmp = len(Group.objects.all()) #evaluate to force db query
updating = True
except DatabaseError as e:
updating = False
#try to query db for South migrations in order to detect if South was already applied
#in this installation (if not then accessing MigrationHistory will throw an excepion)
try:
from south.models import MigrationHistory
has_south = bool(len(MigrationHistory.objects.all()))
except ImportError as e:
print 'ERROR: Error importing South migration history: ' + str(e)
print 'Exiting'
exit(1)
except DatabaseError as e:
has_south = False
#syncdb to create south_migrationhistory table, portal models will not be synced
from django.core.management import execute_from_command_line
argv = ['manage.py', 'syncdb', '--settings=settings', '--pythonpath=' + args.settings_dir]
execute_from_command_line(argv)
#if we are updating existing installation and South wasn't already applied
#then initial migration has to be 'faked' in order to sync with existing tables
if updating and not has_south:
print 'INFO: Faking initial database migration...'
argv = ['manage.py', 'migrate', '--all', '0001', '--fake',
'--settings=settings', '--pythonpath=' + args.settings_dir]
execute_from_command_line(argv)
#run normal migrations
print 'INFO: Applying database migrations...'
argv = ['manage.py', 'migrate', '--all',
'--settings=settings', '--pythonpath=' + args.settings_dir]
execute_from_command_line(argv)
解決您的情況實際上是南方的優秀票:
http://south.aeracode.org/ticket/430
--autofake-first
單建議創建一個標志--autofake-first
,它將確定是否存在任何初始表,如果存在,將偽造第一次遷移,但正常運行其余的。 如果您剛剛將應用程序轉換為South,這是完美的,但您需要將此更改作為遠程或持續部署策略的一部分。
它已經開放了3年,雖然已經實施了一段時間,但它正在等待合並。 如果你想要這個功能,請隨意評論這個問題;-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.