script: strip comments in SQL statements

Regular expression does not match correctly against statements that contain
comments at their start. So strip those comments first (and whitespaces, while
we are at it).

Change-Id: Iad9b544bf995374d76cab1e125658aae2f8511f4
Closes-Bug: #1410494
This commit is contained in:
Ihar Hrachyshka 2015-01-14 00:09:32 +01:00
parent b011e6cb39
commit b9caaae4fc
2 changed files with 11 additions and 0 deletions

View File

@ -281,6 +281,16 @@ class TestSqlScript(fixture.Pathed, fixture.DB):
for script_pattern in ( for script_pattern in (
"BEGIN TRANSACTION; %s; COMMIT;", "BEGIN TRANSACTION; %s; COMMIT;",
"BEGIN; %s; END TRANSACTION;", "BEGIN; %s; END TRANSACTION;",
"/* comment */BEGIN TRANSACTION; %s; /* comment */COMMIT;",
"/* comment */ BEGIN TRANSACTION; %s; /* comment */ COMMIT;",
"""
-- comment
BEGIN TRANSACTION;
%s;
-- comment
COMMIT;""",
): ):
test_statement = ("CREATE TABLE TEST1 (field1 int); " test_statement = ("CREATE TABLE TEST1 (field1 int); "

View File

@ -54,6 +54,7 @@ class SqlScript(base.BaseScript):
# not all drivers reliably handle multistatement queries or # not all drivers reliably handle multistatement queries or
# commands passed to .execute(), so split them and execute one # commands passed to .execute(), so split them and execute one
# by one # by one
text = sqlparse.format(text, strip_comments=True, strip_whitespace=True)
for statement in sqlparse.split(text): for statement in sqlparse.split(text):
if statement: if statement:
if re.match(ignored_regex, statement): if re.match(ignored_regex, statement):