Code

Opened 8 years ago

Closed 7 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
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: UI/UX:

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) 8 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 Changed 8 years ago by Simon Willison

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 Changed 8 years ago by Pawel J. Sawicki (http://pjs.name)

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 Changed 8 years ago by adrian

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

comment:4 Changed 8 years ago by mtredinnick

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 Changed 8 years ago by Pawel J. Sawicki (http://pjs.name)

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 Changed 8 years ago by anonymous

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

Changed 8 years ago by Pawel J. Sawicki (http://pjs.name)

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

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

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

comment:8 Changed 7 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.