这是有关 SQL 语句中的多重分隔符的信息https://stackoverflow.com/a/52292690/9521312
在您的情况下,您可以使用我的脚本(不完美,因为它是),它将带有自定义 DELIMITER 的 MySQL 语句转换为原始 SQL 语句。
在迁移文件中添加脚本执行
有两个使用脚本的例子:运行 sql 文件或运行原始 MySQL 语句
from anywhere import migrate_run_sql
operations = [
migrations.RunPython(migrate_run_sql.run_sql_file('contract_triggers.sql')),
migrations.RunPython(migrate_run_sql.run_sql(
"""
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON table
FOR EACH ROW
BEGIN
IF NEW.number <> 'anynumber' AND NEW.number <> 'anynumber'
THEN
SET NEW.number = 'anynumber';
END IF;
END$$
"""
))
]
脚本文件
# -*- coding: utf-8 -*-
from django.db import connection
import re
from StringIO import StringIO
from django.conf import settings
import os
# this function get raw MySQL statement
def run_sql(sql):
def load_data_from_sql(app, schema_editor):
f = StringIO(sql)
return _runsql(f)
return load_data_from_sql
# this function get sql file
def run_sql_file(filename):
def load_data_from_sql(app, schema_editor):
filepath = os.path.join(settings.PROJECT_PATH, '../deploy/mysql/', filename)
with open(filepath, 'rb') as f:
return _runsql(f)
return load_data_from_sql
# in this function content splits and checks line by line
def _runsql(f):
with connection.cursor() as c:
file_data = f.readlines()
statement = ''
delimiter = ';\n'
for line in file_data:
if re.findall('DELIMITER', line): # found delimiter
if re.findall('^\s*DELIMITER\s+(\S+)\s*$', line):
delimiter = re.findall('^\s*DELIMITER\s+(\S+)\s*$', line)[0] + '\n'
continue
else:
raise SyntaxError('Your usage of DELIMITER is not correct, go and fix it!')
statement += line // add lines while not met lines with current delimiter
if line.endswith(delimiter):
if delimiter != ';\n':
statement = statement.replace(';', '; --').replace(delimiter, ';') # found delimiter, add dash symbols (or any symbols you want) for converting MySQL statements with multiply delimiters in SQL statement
c.execute(statement) # execute current statement
statement = '' # begin collect next statement
希望它会有所帮助!