All schema API operations should be reserved-word safe

Island Usurper - September 29, 2008 - 22:08
Project:Drupal
Version:6.x-dev
Component:database system
Category:bug report
Priority:critical
Assigned:Unassigned
Status:closed
Issue tags:Quick fix
Description

So, I admit that using reserved words for column names was a big mistake, but it was a typo, and I didn't know I had made it.

The gist of the bug is that db_change_field() uses backticks (`) to escape the new column name, but not the old column name. Since the column name is escaped when it is first created in drupal_install_schema(), it will gladly create a column that is a reserved word, but you can't change it after that.

Since using reserved words is an error, I'd like to change that column in an update function. But I can't.

Note that this issue is present in HEAD as well as D6.

#1

Island Usurper - October 1, 2008 - 20:09
Version:6.x-dev» 7.x-dev
Status:active» needs review

Here's the one-line patch to Drupal HEAD. I'll post the one for Drupal 6 after this one gets approved.

AttachmentSizeStatusTest resultOperations
quote_fields_7.diff909 bytesIdleFailed: Failed to apply patch.View details | Re-test

#2

earnie - October 2, 2008 - 12:50
Status:needs review» duplicate

#371: resolve ANSI SQL-92/99/2003 reserved words conflict in query statements

Its been an issue for a while.

#3

Damien Tournoud - October 2, 2008 - 12:54
Title:MySQL: can't update fields named with reserved words» All schema API operations should be reserved-word safe
Status:duplicate» active

@earnie, I disagree. This is a much narrower issue.

#4

earnie - October 2, 2008 - 13:15
Status:active» duplicate

No, it isn't. Take a look at the patch. You'll find similar patches in the issue I pointed to. And we can't quote with ` because ` is MySql only.

#5

Damien Tournoud - October 2, 2008 - 13:16
Status:duplicate» active

And we can't quote with ` because ` is MySql only.

That's great because we are in a MySQL context.

#6

Damien Tournoud - October 2, 2008 - 13:21

@earnie: it seems that we are taking the "don't use any of the reserved-words of any database engine and battle to change them if the list evolves" route. If we do, we have *no other choice* than to make *all* schema API operations reserved-word safe, because if we don't, we will not be able (as the original poster) to change the name of any column that uses a reserved word.

#7

Crell - October 2, 2008 - 15:53
Status:active» needs review

This is actually a prerequisite for fixing the use of reserved words, since we can't fix the problem otherwise. So this issue is valid, the other should be marked "postponed".

Visually the patch looks fine, but I need to try applying it and testing it first. I'll try to get to that soon.

#8

earnie - October 2, 2008 - 17:09

You convinced me.

#9

Crell - October 6, 2008 - 02:04
Status:needs review» needs work

Looks fine, applies fine. There's no Schema API unit tests yet, sadly, but this is trivial enough that I'm confident that it breaks nothing.

However, this only addresses MySQL. Can we give Postgres some love? (It is still a supported database, even if no one uses it.) Or should we push that off to a separate patch? (webchick/Dries, your call, given how lackluster Postgres interest is.)

#10

Island Usurper - October 7, 2008 - 13:39

I'm not sure that Postgres is affected the same way by the bug. None of its specific Schema API functions appear to do anything with escaping reserved words in column names. (How would Postgres do that? Double quotes?)

Would it actually be better to restore symmetry in the other direction, as in removing the backticks? From my point of view, I would have been better off if I hadn't been allowed to make the mistake of creating a field called "values". But I don't know how that would affect core, or indeed, the rest of contrib.

#11

Crell - October 7, 2008 - 14:54
Status:needs work» reviewed & tested by the community

hm. I don't know Postgres well enough to know either way, and we're still lacking good schema API code coverage on unit tests to check it. (And I am scheming to rewrite Schema API anyway).

Given that there are reserved word columns in core now, and we will need Schema API to support them in order to convert away from them, we can't simply ignore the problem.

I think I'm actually going to push this up to RTBC. Committers, if you really want Postgres checked/fixed as part of this patch, find me a Postgres person who can. :-) Otherwise let's get MySQL taken care of and fix Postgres (if needed) separately.

#12

Dries - October 8, 2008 - 11:09
Priority:normal» critical
Status:reviewed & tested by the community» active

I committed this to CVS HEAD. I'm marking this 'active' as the PostgreSQL case needs fixing too.

#13

Crell - October 8, 2008 - 15:05
Title:All schema API operations should be reserved-word safe» Postgres: All schema API operations should be reserved-word safe

Marking accordingly.

#14

linuxpoet - October 8, 2008 - 16:13

You shouldn't have a problem with PostgreSQL and reserved words in newer versions:

postgres=# \d type_test;
Table "public.type_test"
Column | Type | Modifiers
--------+------+-----------
type   | text |

type is a reserved word.

postgres=# select type from type_test;
type
------
(0 rows)

If you want to be doubly sure (and conform to standard) you could do this:

postgres=# select "type" from type_test;
type
------
(0 rows)

Oh and BTW don't be fooled, lots of people use Drupal + PostgreSQL.

#15

Crell - October 8, 2008 - 18:43

@linuxpoet: So the postgres driver is safe as-is, and doesn't need any more work? If so, let's mark this fixed. Or we can add " in the schema operations like we do ` for MySQL. Whichever you think is safer.

#16

nigel - October 20, 2008 - 02:52

Postgres will still complain if you use certain reserved words - GROUP comes to mind as an example:

postgres=# select group from mytable;
ERROR: syntax error at or near "group"
LINE 1: select group from mytable;
^
postgres=# select version();
version
----------------------------------------------------------------------------------
PostgreSQL 8.3.3 on i486-pc-linux-gnu, compiled by GCC cc (Debian 4.3.1-1) 4.3.1
(1 row)

#17

Crell - October 20, 2008 - 04:53

OK, so we need to quote fields in postgres, too. Is ' (apostrophe) the right character for that? If so, can you roll a quick patch a la the MySQL one above?

#18

earnie - October 20, 2008 - 11:55

No it would be the double quote " instead. The double quote character is the ANSI defined quoting character for tables and fields.

#19

brianV - June 7, 2009 - 14:38
Status:active» needs review

Patch rolled for Postgres following earnie's suggestion that the double quote is the proper escaping character.

I only wrapped them around the name of the existing field, and not the new field to stay in sync with the MySQL patch above.

AttachmentSizeStatusTest resultOperations
315047-postgres-make-changefield-reserved-word-safe.patch954 bytesIdleFailed: Failed to install HEAD.View details | Re-test

#20

System Message - June 13, 2009 - 13:05
Status:needs review» needs work

The last submitted patch failed testing.

#21

brianV - June 13, 2009 - 13:29
Status:needs work» needs review

Setting to 'needs review' - testbot was broken.

#22

System Message - June 24, 2009 - 01:25
Status:needs review» needs work

The last submitted patch failed testing.

#23

Shiny - July 16, 2009 - 02:23

tagging for next Postgres Code Sprint

#24

Josh Waihi - July 31, 2009 - 02:33
Status:needs work» reviewed & tested by the community
Issue tags:+Quick fix

adding double quotes around the renamed old field. Doesn't hurt. Testbot shouldn't fail, it doesn't test PostgreSQL (yet), this is a no brainer to comit

AttachmentSizeStatusTest resultOperations
315047-postgres-make-changefield-reserved-word-safe-2.patch947 bytesIdlePassed: 11853 passes, 0 fails, 0 exceptionsView details | Re-test

#25

webchick - August 4, 2009 - 06:32
Status:reviewed & tested by the community» fixed

Committed to HEAD. Thanks.

#26

Island Usurper - August 4, 2009 - 13:11
Title:Postgres: All schema API operations should be reserved-word safe» All schema API operations should be reserved-word safe
Version:7.x-dev» 6.x-dev
Status:fixed» needs review

Thanks, everyone, for looking into this. Here's an equivalent patch for Drupal 6, for both MySQL and Postgres.

AttachmentSizeStatusTest resultOperations
315047_quote_field_names-D6.patch1.19 KBIgnoredNoneNone

#27

Island Usurper - August 4, 2009 - 13:12

Untag pgsql specificity.

#28

brianV - August 4, 2009 - 15:49
Status:needs review» reviewed & tested by the community

Island Usurper's patch in #26 is good. RTBC.

#29

Gábor Hojtsy - September 14, 2009 - 10:32
Status:reviewed & tested by the community» fixed

Committed to Drupal 6 too, thanks.

#30

System Message - September 28, 2009 - 10:40
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.