Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#12293 closed (wontfix)

mysql backend raises Warning exceptions for ignorable info notices

Reported by: wdoekes Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: mysql warnings note
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

For example, when I use the custom /sql/mymodel.mysql.sql with something like:

DROP VIEW IF EXISTS myview;

python-mysqldb raises a Database.Warning to warn us of the info that the table did not exist.

mysql> drop view if exists myview;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+-----------------------------+
| Level | Code | Message                     |
+-------+------+-----------------------------+
| Note  | 1051 | Unknown table 'mydb.myview' | 
+-------+------+-----------------------------+
1 row in set (0.00 sec)

This Warning gets propagated when DEBUG==True (because it's not filterwarning'd) and then the rest of my custom SQL does not get run.

I propose a check against 'Note' level warnings, ignoring those. See my attached patch.
(I created a third function to avoid more duplicate code. I don't know if this extra call decreases performancy any.)

Regards,
Walter Doekes
OSSO B.V.

Attachments (1)

django-ignore-mysql-notes.patch (1.9 KB) - added by wdoekes 6 years ago.
Ignore 'Note'-level warnings from mysqldb.

Download all attachments as: .zip

Change History (3)

Changed 6 years ago by wdoekes

Ignore 'Note'-level warnings from mysqldb.

comment:1 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

I disagree that Django should be throwing away data returned by the cursor. If you don't want notes to be reported by the cursor, might I suggest the use of the sql_notes variable.

comment:2 Changed 5 years ago by wdoekes

Okay, that works for me. Thanks :)

A possible implementation for others experiencing the same issue:

class suppressed_sql_notes(object):
    '''
    Pimp a cursor object to suppress SQL notes and re-enable the
    original configuration when done.

    Use this in a with statement, like so:
      with suppressed_sql_notes(connection.cursor()) as cursor:
        cursor.execute('STUFF THAT YIELDS HARMLESS NOTICES')

    See django bug #12293 marked as WONTFIX.
    '''
    def __init__(self, cursor):
        self.cursor = cursor
    def __enter__(self):
        self.cursor.execute('''SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0;''')
        return self.cursor
    def __exit__(self, type, value, traceback):
        self.cursor.execute('''SET SQL_NOTES=@OLD_SQL_NOTES;''')

And those two SQL statements at the beginning and end of your custom SQL files.

Note: See TracTickets for help on using tickets.
Back to Top