可以migrations.RunPython运行任意python代码吗?
Can migrations.RunPython run arbitrary python code?
我想在迁移结束时包括对许多原始 SQL 文件(函数、触发器...)的处理。
我想改写我的 own Special operation, but I've been told to use the django.db.migrations.RunPython() 命令,并将一些 django.db.migrations.RunSQL() 命令放在被调用的函数中。
由于 RunPython() 需要一个带有 2 个实例的可调用对象(一个 App 和一个 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。
预先感谢您的回答。
我认为很明显,一个名为 RunPython
的操作可以,呃,运行 python 代码,但简短的回答是肯定的,你可以做任何你想做的事想要进行 RunPython
操作。
要考虑的主要事情是您编写的代码需要在未来继续工作(或直到您压缩迁移),因为每次您 运行 migrate
或 makemigrations
, 之前的所有迁移都用于导出数据库的当前状态。
django.db.migration.RunPython 可以接受任何 python 代码。
这里的问题是我试图在使用 运行Python() 进行的调用中使用 运行SQL(),这在我的情况下似乎不起作用,因为我没有提供一个应用程序和一个模式编辑器。
多亏了光标,我设法 运行 我的一批 SQL 文件,并在我的可调用文件中写道:
from django.db import migrations
cursor = connections['default'].cursor()
sql = "FOO;"
cursor.execute(sql)
我想在迁移结束时包括对许多原始 SQL 文件(函数、触发器...)的处理。
我想改写我的 own Special operation, but I've been told to use the django.db.migrations.RunPython() 命令,并将一些 django.db.migrations.RunSQL() 命令放在被调用的函数中。
由于 RunPython() 需要一个带有 2 个实例的可调用对象(一个 App 和一个 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。 预先感谢您的回答。
我认为很明显,一个名为 RunPython
的操作可以,呃,运行 python 代码,但简短的回答是肯定的,你可以做任何你想做的事想要进行 RunPython
操作。
要考虑的主要事情是您编写的代码需要在未来继续工作(或直到您压缩迁移),因为每次您 运行 migrate
或 makemigrations
, 之前的所有迁移都用于导出数据库的当前状态。
django.db.migration.RunPython 可以接受任何 python 代码。 这里的问题是我试图在使用 运行Python() 进行的调用中使用 运行SQL(),这在我的情况下似乎不起作用,因为我没有提供一个应用程序和一个模式编辑器。 多亏了光标,我设法 运行 我的一批 SQL 文件,并在我的可调用文件中写道:
from django.db import migrations
cursor = connections['default'].cursor()
sql = "FOO;"
cursor.execute(sql)