diff --git a/packages/django-google-spanner/django_spanner/schema.py b/packages/django-google-spanner/django_spanner/schema.py index 689e3e12256f..b945319c9a28 100644 --- a/packages/django-google-spanner/django_spanner/schema.py +++ b/packages/django-google-spanner/django_spanner/schema.py @@ -3,15 +3,63 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file or at # https://developers.google.com/open-source/licenses/bsd +import datetime +import math import os +from decimal import Decimal +from typing import Any +from uuid import UUID import django from django.db import NotSupportedError from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.models.fields import NOT_PROVIDED +from django.utils.duration import duration_microseconds +from google.cloud.spanner_dbapi.types import DateStr, TimestampStr +from google.cloud.spanner_v1 import JsonObject from django_spanner._opentelemetry_tracing import trace_call +# GoogleSQL uses backslash escapes inside single-quoted string literals ('...') +# and rejects raw newlines and unescaped ASCII control characters (0x00-0x1F and +# 0x7F). Unlike ANSI SQL, doubling quotes ('') does not escape a quote in +# GoogleSQL; instead, values like r"x\') OR (TRUE) --" would treat \' as an +# escaped quote and close the literal early if backslashes are left unescaped. +# +# This translation table is used by _quote_string_literal() and +# DatabaseSchemaEditor.quote_value() (and by extension prepare_default(), +# CheckConstraint DDL, GeneratedField AS (...) expressions, db_default +# DEFAULT (...) expressions, and collect_sql / sqlmigrate output) to escape \, ', +# and ASCII control characters in one pass. +_STRING_ESCAPE_TABLE = str.maketrans( + { + **{chr(code): f"\\x{code:02x}" for code in (*range(0x20), 0x7F)}, + "\\": r"\\", + "'": r"\'", + "\a": r"\a", + "\b": r"\b", + "\f": r"\f", + "\n": r"\n", + "\r": r"\r", + "\t": r"\t", + "\v": r"\v", + } +) + + +def _quote_string_literal(value: str) -> str: + r"""Escape and wrap ``value`` as a single-quoted GoogleSQL string literal. + + Uses ``str.translate`` directly so ``str`` subclasses (such as Django's + ``SafeString``) cannot override translation behavior. + + Example: + ``_quote_string_literal(r"x\') OR (TRUE) --")`` returns + ``r"'x\\\') OR (TRUE) --'"``; ``_quote_string_literal("a\nb")`` returns + ``r"'a\nb'"``. + """ + return f"'{str.translate(value, _STRING_ESCAPE_TABLE)}'" + class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): """ @@ -395,39 +443,24 @@ def column_sql(self, model, field, include_default=False, exclude_not_null=False # Handle GeneratedField if getattr(field, "generated", False): generated_sql, generated_params = field.generated_sql(self.connection) - quoted_params = [] - for p in generated_params: - if isinstance(p, str): - quoted_params.append( - "'%s'" % p.replace("\\", "\\\\").replace("'", "\\'") - ) - elif isinstance(p, bool): - quoted_params.append("TRUE" if p else "FALSE") - elif p is None: - quoted_params.append("NULL") - else: - quoted_params.append(str(p)) - sql += " AS (%s) STORED" % (generated_sql % tuple(quoted_params)) + if generated_params: + generated_sql = generated_sql % tuple( + self.quote_value(parameter) for parameter in generated_params + ) + sql += " AS (%s) STORED" % generated_sql # Handle db_default db_default = getattr(field, "db_default", None) if db_default is not None and db_default is not NOT_PROVIDED: default_sql, default_params = self.db_default_sql(field) if default_sql: - # Spanner DDL doesn't support parameters, so we must inline them. - quoted_params = [] - for p in default_params: - if isinstance(p, str): - quoted_params.append( - "'%s'" % p.replace("\\", "\\\\").replace("'", "\\'") - ) - elif isinstance(p, bool): - quoted_params.append("TRUE" if p else "FALSE") - elif p is None: - quoted_params.append("NULL") - else: - quoted_params.append(str(p)) - sql += " DEFAULT (%s)" % (default_sql % tuple(quoted_params)) + # db_default_sql() already substitutes params when + # requires_literal_defaults is True; only format remaining params. + if default_params: + default_sql = default_sql % tuple( + self.quote_value(parameter) for parameter in default_params + ) + sql += " DEFAULT (%s)" % default_sql # Return the sql return sql, params @@ -459,15 +492,98 @@ def add_index(self, model, index): ): super().add_index(model, index) - def quote_value(self, value): - # A more complete implementation isn't currently required. - if isinstance(value, str): - return "'%s'" % value.replace("'", "''") + def quote_value(self, value: Any) -> str: + r"""Return a GoogleSQL-escaped literal representation of ``value``. + + Used by Django when inlining literal values into DDL or collected SQL + (for example ``CheckConstraint`` expressions, ``db_default`` and + ``GeneratedField`` definitions, and ``collect_sql`` / ``sqlmigrate``). + Percent signs (``%``) are intentionally not doubled to ``%%`` because + callers emit the returned literal directly without another ``%`` pass. + + Example: + ``quote_value("it's")`` -> ``r"'it\'s'"``; + ``quote_value(Decimal("12.34"))`` -> ``"NUMERIC '12.34'"``; + ``quote_value(b"\x00'")`` -> ``r"b'\x00\x27'"``. + """ + if value is None: + return "NULL" + # Check bool before int because bool is a subclass of int in Python. if isinstance(value, bool): return "TRUE" if value else "FALSE" - return str(value) + # Use base-type methods/descriptors (int.__repr__, float.__repr__, + # Decimal.*, datetime.*.*, UUID.hex.fget) so subclasses overriding + # __str__/__repr__/isoformat/hex/etc. cannot inject arbitrary SQL. + if isinstance(value, int): + return int.__repr__(value) + if isinstance(value, float): + if math.isnan(value): + return "CAST('NaN' AS FLOAT64)" + if math.isinf(value): + return ( + "CAST('-Infinity' AS FLOAT64)" + if value < 0 + else "CAST('Infinity' AS FLOAT64)" + ) + return float.__repr__(value) + if isinstance(value, Decimal): + if not Decimal.is_finite(value): + raise ValueError(f"Cannot quote non-finite Decimal value: {value!r}") + # Emit typed NUMERIC literals to avoid FLOAT64 precision loss/coercion. + return f"NUMERIC '{Decimal.__str__(value)}'" + # DateStr and TimestampStr subclass str, so check them before str. + if isinstance(value, DateStr): + return f"DATE {_quote_string_literal(value)}" + if isinstance(value, TimestampStr): + return f"TIMESTAMP {_quote_string_literal(value)}" + # Check datetime.datetime before datetime.date (datetime subclasses date). + if isinstance(value, datetime.datetime): + if datetime.datetime.utcoffset(value) is not None: + value = datetime.datetime.replace( + datetime.datetime.astimezone(value, datetime.timezone.utc), + tzinfo=None, + ) + formatted = datetime.datetime.isoformat(value, timespec="microseconds") + return f"TIMESTAMP '{formatted}Z'" + if isinstance(value, datetime.date): + return f"DATE '{datetime.date.isoformat(value)}'" + if isinstance(value, datetime.time): + if datetime.time.utcoffset(value) is not None: + raise ValueError(f"Cannot quote timezone-aware time value: {value!r}") + # Spanner has no TIME type; TimeField stores TIMESTAMP on 0001-01-01 UTC. + formatted = datetime.time.isoformat(value, timespec="microseconds") + return f"TIMESTAMP '0001-01-01T{formatted}Z'" + if isinstance(value, datetime.timedelta): + # DurationField maps to INT64 microseconds in django-spanner. + return str(int(duration_microseconds(value))) + if isinstance(value, UUID): + # UUIDField maps to STRING(32) storing 32-char lowercase hex. + return f"'{UUID.hex.fget(value)}'" + # JSONField adapts values via DatabaseOperations.adapt_json_value() into + # _JsonString (a JsonObject subclass); plain dicts raise below. + if isinstance(value, JsonObject): + serialized = value.serialize() + if serialized is None: + return "NULL" + if not isinstance(serialized, str): + raise ValueError( + "Cannot quote JsonObject with non-string serialization: " + f"{serialized!r}" + ) + return f"JSON {_quote_string_literal(serialized)}" + if isinstance(value, str): + return _quote_string_literal(value) + if isinstance(value, (bytes, bytearray, memoryview)): + # Hex-escape every byte inside b'...' so embedded quotes, %, ;, + # comments, control bytes, and non-UTF-8 bytes are always safe. + hex_escaped = "".join(f"\\x{byte:02x}" for byte in bytes(value)) + return f"b'{hex_escaped}'" + raise ValueError( + f"Cannot quote parameter value {value!r} of type {type(value).__name__}" + ) - def prepare_default(self, value): + def prepare_default(self, value: Any) -> str: + """Return a GoogleSQL literal suitable for a column DEFAULT clause.""" return self.quote_value(value) def _alter_field( diff --git a/packages/django-google-spanner/tests/unit/django_spanner/test_schema.py b/packages/django-google-spanner/tests/unit/django_spanner/test_schema.py index c8ccc88803b8..48e2d41f0c19 100644 --- a/packages/django-google-spanner/tests/unit/django_spanner/test_schema.py +++ b/packages/django-google-spanner/tests/unit/django_spanner/test_schema.py @@ -5,10 +5,18 @@ # https://developers.google.com/open-source/licenses/bsd +import datetime +import json +from decimal import Decimal +from enum import IntEnum +from typing import Any, Iterable from unittest import mock +from uuid import UUID +import sqlparse from django.db import NotSupportedError, connection, connections, models -from django.db.models import CheckConstraint, Index, Q +from django.db.models import CheckConstraint, F, Index, Q, Value +from django.db.models.expressions import RawSQL from django.db.models.fields import ( AutoField, IntegerField, @@ -16,10 +24,17 @@ PositiveIntegerField, PositiveSmallIntegerField, ) +from django.db.models.functions import Concat from django.test.utils import isolate_apps +from django.utils.safestring import mark_safe +from google.api_core.datetime_helpers import DatetimeWithNanoseconds from google.api_core.exceptions import InvalidArgument +from google.cloud.spanner_dbapi.parse_utils import classify_statement +from google.cloud.spanner_dbapi.types import DateStr, TimestampStr +from google.cloud.spanner_v1 import JsonObject from django_spanner import gen_rand_int64 +from django_spanner.operations import _JsonString from django_spanner.schema import DatabaseSchemaEditor from tests._helpers import HAS_OPENTELEMETRY_INSTALLED, StatusCode from tests.unit.django_spanner.simple_test import SpannerSimpleTestClass @@ -1316,3 +1331,476 @@ def run_alter(schema_editor: DatabaseSchemaEditor) -> None: new_field.model = Author schema_editor.alter_field(Author, old_field, new_field) self._assert_recorded_spans([]) + + def _assert_quote_value_cases( + self, + cases: Iterable[tuple[Any, str]], + check_prepare_default: bool = True, + ) -> None: + """Assert quote_value (and optionally prepare_default) across (input, expected) pairs.""" + schema_editor = DatabaseSchemaEditor(self.connection) + for raw_value, expected in cases: + with self.subTest(raw_value=raw_value): + self.assertEqual(schema_editor.quote_value(raw_value), expected) + if check_prepare_default: + self.assertEqual(schema_editor.prepare_default(raw_value), expected) + + def test_quote_value_strings_and_escapes(self) -> None: + """String literals use GoogleSQL backslash escaping and resist subclass overrides.""" + + class CustomStr(str): + def translate(self, table: Any) -> str: + return "') OR (TRUE) --" + + def __str__(self) -> str: + return "') OR (TRUE) --" + + self._assert_quote_value_cases( + ( + ("", "''"), + ("plain", "'plain'"), + ("it's", r"'it\'s'"), + (r"x\') OR (TRUE) --", r"'x\\\') OR (TRUE) --'"), + ("trailing\\", r"'trailing\\'"), + ("50% off", "'50% off'"), + ("café 🚀", "'café 🚀'"), + (mark_safe(r"x\') OR (TRUE) --"), r"'x\\\') OR (TRUE) --'"), + (CustomStr("safe'val\\"), r"'safe\'val\\'"), + ( + "\a\b\f\n\r\t\v\x00\x1f\x7f", + r"'\a\b\f\n\r\t\v\x00\x1f\x7f'", + ), + ) + ) + + def test_quote_value_control_characters(self) -> None: + """All ASCII control characters (0x00-0x1F and 0x7F) are escaped.""" + named_escapes = { + "\a": r"\a", + "\b": r"\b", + "\f": r"\f", + "\n": r"\n", + "\r": r"\r", + "\t": r"\t", + "\v": r"\v", + } + cases = [] + for code in (*range(0x20), 0x7F): + character = chr(code) + expected_escape = named_escapes.get(character, f"\\x{code:02x}") + cases.append((f"a{character}b", f"'a{expected_escape}b'")) + self._assert_quote_value_cases(cases) + + def test_quote_value_primitives_and_numeric_types(self) -> None: + """Primitives, floats (including non-finite), and Decimals quote cleanly.""" + + class SampleIntEnum(IntEnum): + FIRST = 7 + + class CustomInt(int): + def __int__(self) -> int: + return self + + def __repr__(self) -> str: + return "0 OR 1=1" + + def __str__(self) -> str: + return "0 OR 1=1" + + class CustomFloat(float): + def __float__(self) -> float: + return self + + def __repr__(self) -> str: + return "0 OR 1=1" + + def __str__(self) -> str: + return "0 OR 1=1" + + class CustomDecimal(Decimal): + def is_finite(self) -> bool: + return True + + def __str__(self) -> str: + return "0 OR 1=1" + + self._assert_quote_value_cases( + ( + (None, "NULL"), + (True, "TRUE"), + (False, "FALSE"), + (0, "0"), + (-42, "-42"), + (SampleIntEnum.FIRST, "7"), + (CustomInt(9), "9"), + (1.1, "1.1"), + (float("-0.0"), "-0.0"), + (CustomFloat(2.5), "2.5"), + (float("nan"), "CAST('NaN' AS FLOAT64)"), + (float("inf"), "CAST('Infinity' AS FLOAT64)"), + (float("-inf"), "CAST('-Infinity' AS FLOAT64)"), + (Decimal("12.34"), "NUMERIC '12.34'"), + (Decimal("-0.50"), "NUMERIC '-0.50'"), + (Decimal("-0"), "NUMERIC '-0'"), + (Decimal("1E+10"), "NUMERIC '1E+10'"), + (CustomDecimal("3.14"), "NUMERIC '3.14'"), + ) + ) + + schema_editor = DatabaseSchemaEditor(self.connection) + for bad_decimal in ( + Decimal("NaN"), + Decimal("sNaN"), + Decimal("Infinity"), + Decimal("-Infinity"), + CustomDecimal("NaN"), + ): + with self.subTest(bad_decimal=bad_decimal): + with self.assertRaisesRegex(ValueError, "non-finite Decimal"): + schema_editor.quote_value(bad_decimal) + + def test_quote_value_temporal_types(self) -> None: + """DateStr, TimestampStr, date, datetime, time, and timedelta quote cleanly.""" + + class NaiveTzInfo(datetime.tzinfo): + def utcoffset(self, dt: datetime.datetime | None) -> None: + return None + + def dst(self, dt: datetime.datetime | None) -> None: + return None + + def tzname(self, dt: datetime.datetime | None) -> str: + return "NAIVE" + + class CustomDateTime(datetime.datetime): + @property + def tzinfo(self) -> None: + return None + + def utcoffset(self) -> datetime.timedelta: + return datetime.timedelta(hours=5) + + def replace(self, *args: Any, **kwargs: Any) -> "CustomDateTime": + return self + + def isoformat(self, sep: str = "T", timespec: str = "auto") -> str: + return "') OR (TRUE) --" + + class CustomDate(datetime.date): + def isoformat(self) -> str: + return "') OR (TRUE) --" + + class CustomTime(datetime.time): + @property + def tzinfo(self) -> None: + return None + + def utcoffset(self) -> datetime.timedelta: + return datetime.timedelta(hours=5) + + def isoformat(self, timespec: str = "auto") -> str: + return "') OR (TRUE) --" + + offset_timezone = datetime.timezone(datetime.timedelta(hours=2)) + self._assert_quote_value_cases( + ( + (DateStr("2025-01-15"), "DATE '2025-01-15'"), + (DateStr(r"x\') OR (TRUE) --"), r"DATE 'x\\\') OR (TRUE) --'"), + ( + TimestampStr("2025-01-15T12:30:00.000000Z"), + "TIMESTAMP '2025-01-15T12:30:00.000000Z'", + ), + ( + TimestampStr(r"x\') OR (TRUE) --"), + r"TIMESTAMP 'x\\\') OR (TRUE) --'", + ), + (datetime.date(2025, 1, 15), "DATE '2025-01-15'"), + (CustomDate(2025, 1, 15), "DATE '2025-01-15'"), + ( + datetime.datetime(2025, 1, 15, 12, 30, 0), + "TIMESTAMP '2025-01-15T12:30:00.000000Z'", + ), + ( + CustomDateTime(2025, 1, 15, 12, 30, 0, tzinfo=NaiveTzInfo()), + "TIMESTAMP '2025-01-15T12:30:00.000000Z'", + ), + ( + CustomDateTime(2025, 1, 15, 14, 30, 0, tzinfo=offset_timezone), + "TIMESTAMP '2025-01-15T12:30:00.000000Z'", + ), + ( + datetime.datetime(2025, 1, 15, 14, 30, 0, tzinfo=offset_timezone), + "TIMESTAMP '2025-01-15T12:30:00.000000Z'", + ), + ( + DatetimeWithNanoseconds( + 2025, 1, 15, 12, 30, 0, nanosecond=123456789 + ), + "TIMESTAMP '2025-01-15T12:30:00.123456Z'", + ), + ( + DatetimeWithNanoseconds( + 2025, + 1, + 15, + 12, + 30, + 0, + nanosecond=123456789, + tzinfo=datetime.timezone.utc, + ), + "TIMESTAMP '2025-01-15T12:30:00.123456Z'", + ), + ( + datetime.time(3, 4, 5), + "TIMESTAMP '0001-01-01T03:04:05.000000Z'", + ), + ( + CustomTime(3, 4, 5, tzinfo=NaiveTzInfo()), + "TIMESTAMP '0001-01-01T03:04:05.000000Z'", + ), + (datetime.timedelta(seconds=5), "5000000"), + (datetime.timedelta(seconds=-5), "-5000000"), + ) + ) + + schema_editor = DatabaseSchemaEditor(self.connection) + for aware_time in ( + datetime.time(3, 4, 5, tzinfo=datetime.timezone.utc), + CustomTime(3, 4, 5, tzinfo=datetime.timezone.utc), + ): + with self.subTest(aware_time=aware_time): + with self.assertRaisesRegex(ValueError, "timezone-aware time"): + schema_editor.quote_value(aware_time) + + def test_quote_value_bytes_and_memoryview(self) -> None: + """Bytes-like values hex-escape all bytes into GoogleSQL b'...' literals.""" + self._assert_quote_value_cases( + ( + (b"", "b''"), + (b"\x00'\\", r"b'\x00\x27\x5c'"), + (b"\xff\x00", r"b'\xff\x00'"), + (b"%s", r"b'\x25\x73'"), + ( + b'"; DROP TABLE users; --', + r"b'\x22\x3b\x20\x44\x52\x4f\x50\x20\x54\x41\x42\x4c\x45\x20" + r"\x75\x73\x65\x72\x73\x3b\x20\x2d\x2d'", + ), + (bytearray(b"\xde\xad"), r"b'\xde\xad'"), + (memoryview(b"\xbe\xef"), r"b'\xbe\xef'"), + (memoryview(b"abcd").cast("I"), r"b'\x61\x62\x63\x64'"), + (memoryview(b"abcd").cast("B", (2, 2)), r"b'\x61\x62\x63\x64'"), + ) + ) + + def test_quote_value_json_and_uuid(self) -> None: + """UUID and JsonObject/_JsonString values quote into GoogleSQL literals.""" + + class CustomUUID(UUID): + @property + def hex(self) -> str: + return "') OR (TRUE) --" + + class InvalidJsonObject(JsonObject): + def serialize(self) -> int: + return 123 + + self._assert_quote_value_cases( + ( + ( + UUID("12345678-1234-5678-1234-567812345678"), + "'12345678123456781234567812345678'", + ), + ( + CustomUUID("12345678-1234-5678-1234-567812345678"), + "'12345678123456781234567812345678'", + ), + ( + _JsonString('{"a": "x\'y\\\\z"}'), + 'JSON \'{"a": "x\\\'y\\\\\\\\z"}\'', + ), + (_JsonString("null"), "JSON 'null'"), + (_JsonString(None), "NULL"), + (JsonObject(None), "NULL"), + (JsonObject({"k": "v"}), 'JSON \'{"k":"v"}\''), + (JsonObject([1, "a'b\\"]), "JSON '[1,\"a\\'b\\\\\\\\\"]'"), + (JsonObject("scalar'\\"), "JSON '\"scalar\\'\\\\\\\\\"'"), + ) + ) + + schema_editor = DatabaseSchemaEditor(self.connection) + with self.assertRaisesRegex( + ValueError, "JsonObject with non-string serialization" + ): + schema_editor.quote_value(InvalidJsonObject()) + + def test_quote_value_rejects_unknown_types(self) -> None: + """Unsupported types raise ValueError instead of falling back to str().""" + + class CustomObject: + def __str__(self) -> str: + return "') OR (TRUE) --" + + schema_editor = DatabaseSchemaEditor(self.connection) + for unknown_value in (CustomObject(), {"a": 1}, [1, 2], {1, 2}, complex(1, 2)): + with self.subTest(unknown_value=unknown_value): + with self.assertRaisesRegex(ValueError, "Cannot quote parameter value"): + schema_editor.quote_value(unknown_value) + + def test_quote_value_sqlparse_safety(self) -> None: + """Quoted DDL literals are never split or stripped by sqlparse or dbapi.""" + schema_editor = DatabaseSchemaEditor(self.connection) + payloads = ( + "'", + "''", + "'''", + '"""', + "\\", + r"\'", + r"\\'", + "trailing\\", + "val\r\n-- comment\n; DROP TABLE users", + "val\x00/* comment */", + "val'; DROP TABLE users; --", + "val'; DROP TABLE users; /*", + "val'; DROP TABLE users; #", + r"x\') OR (TRUE) --", + r"x\\') OR (TRUE) --", + '"; DROP TABLE users; --', + "%s", + ) + for payload in payloads: + literals = ( + schema_editor.quote_value(payload), + schema_editor.quote_value(payload.encode("utf-8")), + schema_editor.quote_value(_JsonString(json.dumps({"k": payload}))), + ) + for literal in literals: + sql = ( + f"CREATE TABLE t (c STRING(MAX) DEFAULT ({literal})) PRIMARY KEY(c)" + ) + with self.subTest(payload=payload, literal=literal): + self.assertEqual(sqlparse.split(sql), [sql]) + self.assertEqual(sqlparse.format(sql, strip_comments=True), sql) + self.assertEqual(classify_statement(sql).statement.sql, sql) + + @isolate_apps("tests") + def test_create_model_ddl_literal_escaping(self) -> None: + """create_model escapes CheckConstraint, db_default, and GeneratedField.""" + + class EscapedLiteralModel(models.Model): + text = models.CharField( + max_length=100, + db_default="50% off\n'test'\\", + ) + created_date = models.DateField(db_default=datetime.date(2025, 1, 2)) + amount = models.DecimalField( + max_digits=8, + decimal_places=2, + db_default=Decimal("12.34"), + ) + payload = models.JSONField(db_default={"key": "val'ue\\"}) + computed = models.GeneratedField( + expression=Concat(F("text"), Value("a'b\\c\nd%e")), + output_field=models.CharField(max_length=200), + db_persist=True, + ) + raw_computed = models.GeneratedField( + expression=RawSQL("CASE WHEN text LIKE '50%' THEN 1 ELSE 0 END", ()), + output_field=models.IntegerField(), + db_persist=True, + ) + + class Meta: + app_label = "tests" + db_table = "tests_escaped_literal" + constraints = [ + CheckConstraint( + condition=Q(text=r"x\') OR (TRUE) --") + & Q(text__contains="50%'\\") + & Q(created_date__gte=datetime.date(2020, 1, 1)), + name="safe_text_check", + ), + ] + + with DatabaseSchemaEditor(self.connection, collect_sql=True) as schema_editor: + schema_editor.create_model(EscapedLiteralModel) + + self.assertEqual( + schema_editor.collected_sql, + [ + "CREATE TABLE tests_escaped_literal (" + "id INT64 NOT NULL, " + r"text STRING(100) NOT NULL DEFAULT ('50% off\n\'test\'\\'), " + "created_date DATE NOT NULL DEFAULT (DATE '2025-01-02'), " + "amount NUMERIC NOT NULL DEFAULT (NUMERIC '12.34'), " + 'payload JSON NOT NULL DEFAULT (JSON \'{"key": "val\\\'ue\\\\\\\\"}\'), ' + "computed STRING(200) NOT NULL AS " + r"(CONCAT(IFNULL(text, ''), IFNULL('a\'b\\c\nd%e', ''))) STORED, " + "raw_computed INT64 NOT NULL AS " + "((CASE WHEN text LIKE '50%' THEN 1 ELSE 0 END)) STORED, " + "CONSTRAINT safe_text_check CHECK (" + r"(text = 'x\\\') OR (TRUE) --' AND " + r"REGEXP_CONTAINS(CAST(text AS STRING), '50%\'\\\\') AND " + "created_date >= DATE '2020-01-01')" + ")) PRIMARY KEY(id);" + ], + ) + self.assertEqual( + sqlparse.split(schema_editor.collected_sql[0]), + schema_editor.collected_sql, + ) + + @isolate_apps("tests") + def test_column_sql_formats_remaining_db_default_params(self) -> None: + """column_sql quotes remaining db_default_sql params and skips empty SQL.""" + + class DefaultParamModel(models.Model): + text = models.CharField(max_length=100, db_default="50% 'off'\\") + + class Meta: + app_label = "tests" + db_table = "tests_default_param" + + schema_editor = DatabaseSchemaEditor(self.connection) + field = DefaultParamModel._meta.get_field("text") + with mock.patch.object( + schema_editor, "db_default_sql", return_value=("%s", ["50% 'off'\\"]) + ): + sql, params = schema_editor.column_sql(DefaultParamModel, field) + + self.assertEqual(sql, r"STRING(100) NOT NULL DEFAULT ('50% \'off\'\\')") + self.assertEqual(params, []) + + with mock.patch.object(schema_editor, "db_default_sql", return_value=("", [])): + sql, params = schema_editor.column_sql(DefaultParamModel, field) + + self.assertEqual(sql, "STRING(100) NOT NULL") + self.assertEqual(params, []) + + @isolate_apps("tests") + def test_add_field_collect_sql_literal_escaping(self) -> None: + """add_field with collect_sql escapes backfill UPDATE literals.""" + + class BackfillModel(models.Model): + class Meta: + app_label = "tests" + db_table = "tests_backfill_literal" + + new_field = models.CharField(max_length=100, default=r"x\') OR (TRUE) --") + new_field.set_attributes_from_name("extra_text") + new_field.model = BackfillModel + + with DatabaseSchemaEditor(self.connection, collect_sql=True) as schema_editor: + schema_editor.add_field(BackfillModel, new_field) + + self.assertEqual( + schema_editor.collected_sql, + [ + "ALTER TABLE tests_backfill_literal ADD COLUMN extra_text STRING(100);", + "UPDATE tests_backfill_literal SET extra_text=" + r"'x\\\') OR (TRUE) --' WHERE 1=1;", + "ALTER TABLE tests_backfill_literal ALTER COLUMN " + "extra_text STRING(100) NOT NULL;", + ], + )