From 92571b0d48e30e958f4c3fed43bf035894c09390 Mon Sep 17 00:00:00 2001
From: Malcolm Tredinnick <malcolm.tredinnick@gmail.com>
Date: Tue, 20 Jun 2006 08:00:44 +0000
Subject: [PATCH] Fixed #2119 -- fixed problems with splitting SQL statements
 into separate statements. Uses a patch from eaw@woudy.org and some
 contributions from jpellerin@gmail.com. Also fixes #2034 and #1935.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3178 bcc190cf-cafb-0310-a4f2-bffc1f526a37
---
 django/core/management.py                           | 13 ++++---------
 .../initial_sql_regress/sql/simple.sql              |  1 +
 2 files changed, 5 insertions(+), 9 deletions(-)

diff --git a/django/core/management.py b/django/core/management.py
index 684020f9d1..bf48b2bd89 100644
--- a/django/core/management.py
+++ b/django/core/management.py
@@ -333,14 +333,7 @@ def get_sql_initial_data_for_model(model):
 
     # Some backends can't execute more than one SQL statement at a time,
     # so split into separate statements.
-    sql_expr = re.compile(
-        r"""(           # each statement ...
-        [^\r\n;]        # starts with something other than a line ending or ';'
-        (?:             # then has one or more chunks of ...
-            (?:[^;'"]+) # not the end of a statement or start of a quote
-          | (?:'[^']*') # something in single quotes
-          | (?:"[^"]*") # something in double quotes
-        )+)""", re.VERBOSE)
+    statements = re.compile(r";[ \t]*$", re.M)
 
     # Find custom SQL, if it's available.
     sql_files = [os.path.join(app_dir, "%s.%s.sql" % (opts.object_name.lower(), settings.DATABASE_ENGINE)),
@@ -348,7 +341,9 @@ def get_sql_initial_data_for_model(model):
     for sql_file in sql_files:
         if os.path.exists(sql_file):
             fp = open(sql_file)
-            output.extend(sql_expr.findall(fp.read()))
+            for statement in statements.split(fp.read()):
+                if statement.strip():
+                    output.append(statement + ";")
             fp.close()
 
     return output
diff --git a/tests/regressiontests/initial_sql_regress/sql/simple.sql b/tests/regressiontests/initial_sql_regress/sql/simple.sql
index ac60c0c3d6..d9cd9893b5 100644
--- a/tests/regressiontests/initial_sql_regress/sql/simple.sql
+++ b/tests/regressiontests/initial_sql_regress/sql/simple.sql
@@ -2,4 +2,5 @@ INSERT INTO initial_sql_regress_simple (name) VALUES ('John');
 INSERT INTO initial_sql_regress_simple (name) VALUES ('Paul');
 INSERT INTO initial_sql_regress_simple (name) VALUES ('Ringo');
 INSERT INTO initial_sql_regress_simple (name) VALUES ('George');
+INSERT INTO initial_sql_regress_simple (name) VALUES ('Miles O''Brien');