我希望在迁移结束时包括对许多原始SQL文件(函数、触发器等)的处理。在
由于RunPython()使用带有两个实例(一个应用程序和一个SchemaEditor)的callable,我严重怀疑(我稍微回顾了一下源代码)我是否可以用纯python代码调用函数,它似乎只能执行ORM操作。我是否应该在RunPython中使用execute_from_command_line()?或者这条路注定要失败?还是做得不好?在from __future__ import unicode_literals
from django.db import migrations
def load_sql(apps, schema_editor):
from os.path import normpath, dirname, isfile, join
from os import listdir
sql_folder_path = '/backoffice/sql/'
def load_raw_sql(folder_inside):
folder_path = join(sql_folder_path, folder_inside)
sql_files = [join(folder_path, f) for f in listdir(folder_path) if isfile(join(folder_path, f))]
for sql_file in sql_files:
with open(sql_file, 'r') as g:
migrations.RunSQL(g.read())
folders = ['functions', 'index', 'triggers']
for folder in folders:
load_raw_sql(folder)
class Migration(migrations.Migration):
dependencies = [
('app1', '0001_squashed_0018_auto_20150616_0708'),
]
operations = [
migrations.RunPython(load_sql),
]
我们使用的是PostGreSQL。
提前感谢您的回答。在