I want to include the processing of many many raw SQL files (functions, triggers ...) at the end of my migrations.
I thought to write my own Special operation, but I've been told to use the django.db.migrations.RunPython() command instead and put some django.db.migrations.RunSQL() commands in the called function.
As RunPython() takes a callable with 2 instances (an App and a SchemaEditor) I seriously doubt (I reviewed the source a bit) that I can call a function with pure python code, it seems like it could only perform ORM manipulations. Should I use execute_from_command_line() inside the RunPython ? Or is that way doomed to failure ? Or doing things poorly ?
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),
]
We are using PostGreSQL. Thanks in advance for your answers.
sqlparsepackage and if i should use RunSQL or manually use a database connection.