Opened 18 years ago

Closed 17 years ago

#2452 closed enhancement (fixed)

[PATCH] Allow to use comments in the "initial sql data" files.

Reported by: Pawel J. Sawicki (http://pjs.name) Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: minor Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In order to allow comments to be placed in the initial sql data files (http://www.djangoproject.com/documentation/model_api/#providing-initial-sql-data) the following patch may be used:

$ svn diff management.py
Index: management.py
===================================================================
--- management.py       (revision 3496)
+++ management.py       (working copy)
@@ -499,7 +499,9 @@
                     print "Installing initial data for %s model" % model._meta.object_name
                     try:
                         for sql in initial_sql:
-                            cursor.execute(sql)
+                               # Allow to use comments in the initial sql data files.
+                               if not sql.strip().startswith('--'):
+                                   cursor.execute(sql)
                     except Exception, e:
                         sys.stderr.write("Failed to install initial SQL data for %s model: %s" % \
                                             (model._meta.object_name, e))

Attachments (1)

management.diff (628 bytes ) - added by Pawel J. Sawicki (http://pjs.name) 18 years ago.
Patch that correctly removes any comments presented within any initial sql data files.

Download all attachments as: .zip

Change History (9)

comment:1 by Simon Willison, 18 years ago

I haven't tried this patch, but from looking at it it doesn't seem to take differences in SQL comments betweeen databases in to account. Should this be handled at by code in the individual database adapters?

comment:2 by Pawel J. Sawicki (http://pjs.name), 18 years ago

Isn't the '--' prefix an "ISO way" to make comments in the SQL language? I'm not a DB expert - just asking just out of curiosity.

comment:3 by Adrian Holovaty, 18 years ago

I believe the -- will take care of comments on all database backends we support.

comment:4 by Malcolm Tredinnick, 18 years ago

We unfortunately need to revisit the initial SQL parsing again and probably come up with something that is much more holistic. The current line-by-line stuff has no end of corner-cases it can't handle, no matter how we implement it. Tickets #2284 and #2444 are related to this.

comment:5 by Pawel J. Sawicki (http://pjs.name), 18 years ago

Ok - I get it. Nevertheless this is quite enough for me, for the time being.

I need to version the sql files and add $Id$ "svn:keywords" ("-- $Id$\n" line). Since the patch is a one-liner I'll stick with it until the initial sql parsing procedures are revisited.

comment:6 by anonymous, 18 years ago

wouldn't it just be easier to not try and parse these at all?
just read the files and dump them to stdout

by Pawel J. Sawicki (http://pjs.name), 18 years ago

Attachment: management.diff added

Patch that correctly removes any comments presented within any initial sql data files.

comment:7 by Pawel J. Sawicki (http://pjs.name), 18 years ago

I've fixed the patch since the previous one didn't work as it should (under some conditions).

comment:8 by Jacob, 17 years ago

Resolution: fixed
Status: newclosed

(In [4040]) Fixed #2452: comments can now be put within initial SQL files. Thanks, Pawel J. Sawicki.

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