#23426: migrations.RunSQL's function signature implies it won't do any parameter
substitution
-------------------------------------+-------------------------------------
     Reporter:  ris                  |                    Owner:
         Type:                       |  Markush2010
  Cleanup/optimization               |                   Status:  assigned
    Component:  Documentation        |                  Version:  1.7
     Severity:  Normal               |               Resolution:
     Keywords:  migrations sql       |             Triage Stage:  Accepted
  runsql params escape               |      Needs documentation:  0
    Has patch:  0                    |  Patch needs improvement:  0
  Needs tests:  0                    |                    UI/UX:  0
Easy pickings:  1                    |
-------------------------------------+-------------------------------------
Changes (by Markush2010):

 * cc: info+coding@… (added)
 * status:  new => assigned
 * owner:  nobody => Markush2010


Comment:

 A patch is rather simple to get either support or not support for
 parameters. I would consider the way it is implemented right now to be a
 bug.

 To allow using `'%'` as wildcards in SQL, the following works for me:

 {{{#!patch
 diff --git a/django/db/migrations/operations/special.py
 b/django/db/migrations/operations/special.py
 index fe5f00a..bfe4180 100644
 --- a/django/db/migrations/operations/special.py
 +++ b/django/db/migrations/operations/special.py
 @@ -66,14 +66,14 @@ class RunSQL(Operation):
      def database_forwards(self, app_label, schema_editor, from_state,
 to_state):
          statements =
 schema_editor.connection.ops.prepare_sql_script(self.sql)
          for statement in statements:
 -            schema_editor.execute(statement)
 +            schema_editor.execute(statement, params=None)

      def database_backwards(self, app_label, schema_editor, from_state,
 to_state):
          if self.reverse_sql is None:
              raise NotImplementedError("You cannot reverse this
 operation")
          statements =
 schema_editor.connection.ops.prepare_sql_script(self.reverse_sql)
          for statement in statements:
 -            schema_editor.execute(statement)
 +            schema_editor.execute(statement, params=None)

      def describe(self):
          return "Raw SQL operation"
 }}}

 Allowing params to be passed to `RunSQL` a few more changes need to be
 done:

 {{{#!patch
 diff --git a/django/db/migrations/operations/special.py
 b/django/db/migrations/operations/special.py
 index fe5f00a..f6e7ea9 100644
 --- a/django/db/migrations/operations/special.py
 +++ b/django/db/migrations/operations/special.py
 @@ -50,10 +50,12 @@ class RunSQL(Operation):
      by this SQL change, in case it's custom column/table
 creation/deletion.
      """

 -    def __init__(self, sql, reverse_sql=None, state_operations=None):
 +    def __init__(self, sql, reverse_sql=None, state_operations=None,
 params=None, reverse_params=None):
          self.sql = sql
          self.reverse_sql = reverse_sql
          self.state_operations = state_operations or []
 +        self.params = params
 +        self.reverse_params = reverse_params

      @property
      def reversible(self):
 @@ -66,14 +68,14 @@ class RunSQL(Operation):
      def database_forwards(self, app_label, schema_editor, from_state,
 to_state):
          statements =
 schema_editor.connection.ops.prepare_sql_script(self.sql)
          for statement in statements:
 -            schema_editor.execute(statement)
 +            schema_editor.execute(statement, params=self.params)

      def database_backwards(self, app_label, schema_editor, from_state,
 to_state):
          if self.reverse_sql is None:
              raise NotImplementedError("You cannot reverse this
 operation")
          statements =
 schema_editor.connection.ops.prepare_sql_script(self.reverse_sql)
          for statement in statements:
 -            schema_editor.execute(statement)
 +            schema_editor.execute(statement, params=self.reverse_params)

      def describe(self):
          return "Raw SQL operation"
 }}}

 Which way should I go and should it be backported to 1.7?

--
Ticket URL: <https://code.djangoproject.com/ticket/23426#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/061.ba134203180af1a14cbedcc2398225b6%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to