_db_create_field_sql has precision and scale arguments in wrong order

pukku - February 5, 2008 - 14:36
Project:Drupal
Version:7.x-dev
Component:mysql database
Category:bug report
Priority:critical
Assigned:Unassigned
Status:closed
Description

This also affects the PostgreSQL database.

In both includes/database.mysql-common.inc and includes/database.pgsql.inc, the function _db_create_field_sql has the precision and scale arguments in the wrong order for numeric fields. They should be precision then scale.

That is, the lines:

  elseif (isset($spec['precision']) && isset($spec['scale'])) {
    $sql .= '('. $spec['scale'] .', '. $spec['precision'] .')';
  }

should be

  elseif (isset($spec['precision']) && isset($spec['scale'])) {
    $sql .= '('. $spec['precision'] .', '. $spec['scale'] .')';
  }

I'm sorry I can't roll an official patch — I'm not set up for that right now.

#1

Arancaytar - February 5, 2008 - 14:51
Status:active» patch (code needs review)

Here you go. The MySQL docs say you're right, but I haven't tested one bit of this myself.

AttachmentSize
database-scale-precision-swap-218054-1.patch1.4 KB

#2

mfb - February 5, 2008 - 17:47

#3

pukku - February 6, 2008 - 22:33
Status:patch (code needs review)» patch (reviewed & tested by the community)

I don't know if I'm really allowed to make this ready to be committed, but the code looks right...

#4

mfb - February 6, 2008 - 22:53

I tested the patch with precision 5, scale 2. The table is now created (where before there was an error) and it all looks good in mysql:

+--------+--------------+------+-----+---------+-------+
| foobar | decimal(5,2) | YES  |     | NULL    |       |
+--------+--------------+------+-----+---------+-------+

#5

Gábor Hojtsy - February 7, 2008 - 09:31

Is this used anywhere in Drupal core? Where we would need an update function to correct previously miscreated fields?

#6

Arancaytar - February 7, 2008 - 10:04

It's impossible to miscreate a field this way - as mfb tested, MySQL returns an error if the precision is smaller than the scale.

I grepped for "precision" to make sure that we didn't "work around" the bug by switching precision and scale elsewhere, but didn't find anything.

#7

Gábor Hojtsy - February 7, 2008 - 10:17
Version:6.x-dev» 7.x-dev

Great, thanks, committed to 6.x. Still needs to be committed to 7.x.

#8

Dries - February 8, 2008 - 03:24
Status:patch (reviewed & tested by the community)» fixed

Committed to CVS HEAD as well -- thanks folks and good catch.

#9

Anonymous (not verified) - February 22, 2008 - 03:31
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.