【发布时间】:2015-09-20 11:15:13
【问题描述】:
我想在迁移结束时包括对许多原始 SQL 文件(函数、触发器...)的处理。
我想写我的own Special operation,但有人告诉我改用django.db.migrations.RunPython() 命令并将一些 django.db.migrations.RunSQL() 命令放在被调用的函数中。
由于 RunPython() 需要一个带有 2 个实例(一个应用程序和一个 SchemaEditor)的可调用对象,我严重怀疑(我查看了一点源代码)我可以用纯 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。 提前感谢您的回答。
【问题讨论】:
-
“我严重怀疑......我可以用纯 python 代码调用一个函数,它似乎只能执行 ORM 操作。”这句话真的把我扔了。纯 Python 与仅 ORM 操作是什么意思?所有的 Django 都是用 Python 编写的。什么是“不纯”的?
-
用词不当,但我的意思是我只在 runPython() 命令中看到了类似模型的实现,我想知道我是否可以在其中编写任何 python 代码?
-
大概。你试过了吗?什么会阻止你?甚至那些模型也只是 Python 代码。
-
我试过上面的代码,好像我做错了,如果我放打印语句,我没有得到任何输出,我的 django_migrations 表得到一个迁移条目运行代码,所以它不会失败。我不知道我是否需要 link
sqlparse包以及我应该使用 RunSQL 还是手动使用数据库连接。
标签: python django django-migrations