Skip to content

Commit

Permalink
Feature for client side binding in schema_editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo13 committed Jun 5, 2022
1 parent 3a57862 commit d074e9d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 deletions.
2 changes: 2 additions & 0 deletions django/db/backends/base/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ class BaseDatabaseFeatures:
# Can we roll back DDL in a transaction?
can_rollback_ddl = False

schema_editor_uses_clientside_param_binding = False

# Does it support operations requiring references rename in a transaction?
supports_atomic_references_rename = True

Expand Down
1 change: 1 addition & 0 deletions django/db/backends/postgresql/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
can_introspect_materialized_views = True
can_distinct_on_fields = True
can_rollback_ddl = True
schema_editor_uses_clientside_param_binding = True
supports_combined_alters = True
nulls_order_largest = True
supports_group_column_alias = True
Expand Down
27 changes: 4 additions & 23 deletions tests/schema/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
from unittest import skipIf, skipUnless

from django.db import connection
from django.test import TestCase


class SchemaLoggerTests(TestCase):
@skipIf(
connection.vendor == "postgresql", reason="postgresql will merge on the client"
)
def test_extra_args(self):
editor = connection.schema_editor(collect_sql=True)
sql = "SELECT * FROM foo WHERE id in (%s, %s)"
params = [42, 1337]
with self.assertLogs("django.db.backends.schema", "DEBUG") as cm:
editor.execute(sql, params)
if connection.features.schema_editor_uses_clientside_param_binding:
sql = "SELECT * FROM foo WHERE id in (42, 1337)"
params = None
self.assertEqual(cm.records[0].sql, sql)
self.assertEqual(cm.records[0].params, params)
self.assertEqual(
cm.records[0].getMessage(),
"SELECT * FROM foo WHERE id in (%s, %s); (params [42, 1337])",
)

@skipUnless(
connection.vendor == "postgresql", reason="postgres will merge on the client"
)
def test_extra_args_psycopg(self):
editor = connection.schema_editor(collect_sql=True)
sql = "SELECT * FROM foo WHERE id in (%s, %s)"
params = [42, 1337]
with self.assertLogs("django.db.backends.schema", "DEBUG") as cm:
editor.execute(sql, params)
merged = "SELECT * FROM foo WHERE id in (42, 1337)"
self.assertEqual(cm.records[0].sql, merged)
self.assertEqual(cm.records[0].params, None)
self.assertEqual(cm.records[0].getMessage(), "%s; (params None)" % merged)
self.assertEqual(cm.records[0].getMessage(), f"{sql}; (params {params})")

0 comments on commit d074e9d

Please sign in to comment.