Skip to content

fix(django-spanner): backslash-escape string literals in quote_value - #18234

Open
Samin061 wants to merge 1 commit into
googleapis:mainfrom
Samin061:django-spanner-quote-value-escape
Open

fix(django-spanner): backslash-escape string literals in quote_value#18234
Samin061 wants to merge 1 commit into
googleapis:mainfrom
Samin061:django-spanner-quote-value-escape

Conversation

@Samin061

Copy link
Copy Markdown
Contributor

quote_value inlines a string value into DDL by wrapping it in single quotes and doubling embedded quotes, but Cloud Spanner GoogleSQL does not treat '' as an escaped quote and the routine never escapes backslashes, so a value beginning with a backslash and a quote closes the literal and the trailing text is parsed as DDL. The Spanner backend sets requires_literal_defaults, so column and table comments and altered defaults reach this method. The db_default and generated-column inlining a few lines above already escape by backslashing the backslash and then the quote, so route quote_value through the same escaping.

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

@Samin061
Samin061 requested a review from a team as a code owner August 27, 2026 08:25

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the quote_value method in django_spanner/schema.py to properly escape backslashes and single quotes for GoogleSQL string literals, and adds corresponding unit tests. The feedback suggests also escaping literal newlines and carriage returns to prevent syntax errors in Spanner.

# GoogleSQL string literals use backslash escaping; '' quote
# doubling is not recognized, so escape the backslash first and
# then the quote (matching the db_default/generated inlining above).
return "'%s'" % value.replace("\\", "\\\\").replace("'", "\\'")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In GoogleSQL, single-quoted string literals cannot contain literal newlines (\n) or carriage returns (\r). If a string containing these characters is passed to quote_value, it will result in a syntax error in Spanner. To prevent this, we should also escape newlines and carriage returns.

Suggested change
return "'%s'" % value.replace("\\", "\\\\").replace("'", "\\'")
return "'%s'" % value.replace("\\", "\\\\").replace("'", "\\'").replace("\n", "\\n").replace("\r", "\\r")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Samin061 can you address this comment as well?

@sakthivelmanii sakthivelmanii added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Sep 4, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Sep 4, 2026
# GoogleSQL string literals use backslash escaping; '' quote
# doubling is not recognized, so escape the backslash first and
# then the quote (matching the db_default/generated inlining above).
return "'%s'" % value.replace("\\", "\\\\").replace("'", "\\'")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In GoogleSQL, single-quoted string literals cannot contain literal unescaped newlines (\n) or carriage returns (\r). If a multiline default or comment is passed to quote_value, it causes a syntax error in Spanner DDL. Please escape them using .replace("\n", "\\n").replace("\r", "\\r") (matching what you did in PR #18205).

Also add this test to verify:

def test_quote_value_escapes_newlines_and_carriage_returns(self):
    schema_editor = DatabaseSchemaEditor(self.connection)
    self.assertEqual(
        schema_editor.quote_value("line1\nline2"),
        "'line1\\nline2'",
    )
    self.assertEqual(
        schema_editor.quote_value("line1\r\nline2"),
        "'line1\\r\\nline2'",
    )

return "'%s'" % value.replace("\\", "\\\\").replace("'", "\\'")
if isinstance(value, bool):
return "TRUE" if value else "FALSE"
return str(value)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly part of this change, but still: quote_value falls through to str(value) when value is None, returning the literal string "None" instead of SQL "NULL". In GoogleSQL DDL, DEFAULT (None) causes a syntax/name-resolution error. Please handle None explicitly by returning "NULL".

Similar problems also occur for Date, Timestamp and Bytes.

Add these tests to verify:

def test_quote_value_handles_none(self):
    schema_editor = DatabaseSchemaEditor(self.connection)
    self.assertEqual(schema_editor.quote_value(None), "NULL")

import datetime
def test_quote_value_handles_date_and_datetime(self):
    schema_editor = DatabaseSchemaEditor(self.connection)
    self.assertEqual(
        schema_editor.quote_value(datetime.date(2026, 9, 4)),
        "'2026-09-04'",
    )
    self.assertEqual(
        schema_editor.quote_value(datetime.datetime(2026, 9, 4, 12, 0, 0)),
        "'2026-09-04 12:00:00'",
    )

schema_editor = DatabaseSchemaEditor(self.connection)
self.assertEqual(schema_editor.quote_value(value=1.1), "1.1")

def test_quote_value_escapes_string(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add:

def test_quote_value_booleans(self):
    schema_editor = DatabaseSchemaEditor(self.connection)
    self.assertEqual(schema_editor.quote_value(True), "TRUE")
    self.assertEqual(schema_editor.quote_value(False), "FALSE")

def test_prepare_default_delegates_to_quote_value(self):
    schema_editor = DatabaseSchemaEditor(self.connection)
    self.assertEqual(schema_editor.prepare_default("o'brien"), "'o\\'brien'")
    self.assertEqual(schema_editor.prepare_default(True), "TRUE")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants