pgsql driver does not handle unsigned numeric fields

bjaspan - May 8, 2008 - 00:47
Project:Drupal
Version:6.x-dev
Component:database system
Category:bug report
Priority:normal
Assigned:Unassigned
Status:postponed
Description

The D6 pgsql driver does not properly created unsigned numeric fields if the schema so specifies. I've attached a patch for D6. It applies to D7 as well. However, it also needs to be applied to the PDO changes in progress, so this issue should not be closed until it is in all three places.

AttachmentSizeStatusTest resultOperations
pgsql-numeric-unsigned.patch1.6 KBIdleFailed: Failed to apply patch.View details | Re-test

#1

Dries - May 8, 2008 - 05:56

I assume the changes to bootstrap.inc are "optional"? ;-)

Could you add a one-line code comment? CHECK($name >= 0) is new to me.

#2

bjaspan - May 8, 2008 - 15:52
Status:needs review» needs work

The change to bootstrap is an accident. :-)

#3

Damien Tournoud - May 8, 2008 - 20:42

Some discussion about this already occurred some years ago, in #60871.

#4

bjaspan - May 9, 2008 - 18:14
Status:needs work» needs review

@#3: Thanks for the pointer, I did not know about that issue.

New patch attached. Turns out D6 pgsql driver also does not support unsigned floats; also fixed.

AttachmentSizeStatusTest resultOperations
pgsql-numeric-unsigned-256001-4.patch1.93 KBIdleFailed: Failed to apply patch.View details | Re-test

#5

Dries - May 9, 2008 - 19:19
Version:7.x-dev» 6.x-dev

I've committed this patch to CVS HEAD. Would be better if it came with tests.

#6

bjaspan - May 10, 2008 - 00:39
Version:6.x-dev» 7.x-dev
Status:needs review» needs work

Okay, I'll mark it CNW pending tests.

#7

bjaspan - May 10, 2008 - 20:19
Status:needs work» needs review

Tests for signed/unsigned int, float, and numeric columns for HEAD. Remember to 'cvs add' this new file.

AttachmentSizeStatusTest resultOperations
pgsql-numeric-unsigned-256001-7.patch2.8 KBIdleFailed: Invalid PHP syntax.View details | Re-test

#8

Damien Tournoud - May 15, 2008 - 07:40

On 7.x, this patch causes a lot of exceptions during testing:

Unexpected PHP error [pg_query() [function.pg-query]: Query failed: ERROR: type "int_unsigned" already exists] severity [E_WARNING] in [/var/www/Drupal/Postgres-Head/includes/database.pgsql.inc line 138] [PHP] Exception
Unexpected PHP error [ERROR: type "int_unsigned" already exists query: CREATE DOMAIN int_unsigned integer CHECK (VALUE >= 0)] severity [E_USER_WARNING] in [/var/www/Drupal/Postgres-Head/includes/database.pgsql.inc line 159] [PHP] Exception
Unexpected PHP error [pg_query() [function.pg-query]: Query failed: ERROR: type "smallint_unsigned" already exists] severity [E_WARNING] in [/var/www/Drupal/Postgres-Head/includes/database.pgsql.inc line 138] [PHP] Exception
Unexpected PHP error [ERROR: type "smallint_unsigned" already exists query: CREATE DOMAIN smallint_unsigned smallint CHECK (VALUE >= 0)] severity [E_USER_WARNING] in [/var/www/Drupal/Postgres-Head/includes/database.pgsql.inc line 159] [PHP] Exception
Unexpected PHP error [pg_query() [function.pg-query]: Query failed: ERROR: type "bigint_unsigned" already exists] severity [E_WARNING] in [/var/www/Drupal/Postgres-Head/includes/database.pgsql.inc line 138] [PHP] Exception
Unexpected PHP error [ERROR: type "bigint_unsigned" already exists query: CREATE DOMAIN bigint_unsigned bigint CHECK (VALUE >= 0)] severity [E_USER_WARNING] in [/var/www/Drupal/Postgres-Head/includes/database.pgsql.inc line 159] [PHP] Exception

I'm not sure if this is a simpletest / SchemaAPI bug or a bug with that patch.

#9

bjaspan - May 15, 2008 - 16:03

It is not this patch that causes those exceptions; every test on pgsql throws those exceptions with or without this patch.

See http://drupal.org/node/257009 which fixes simpletest for pgsql.

#10

elyobo - September 8, 2008 - 18:24

Can I ask how this is coming along? I just ran into the same problem installing the development version of Ubercart with D6.4. The fix looks trivial, is there some holdup?

#11

linuxpoet - September 20, 2008 - 18:27

I am having problems determining what the actual problem here is. The errors listed on the page are obvious, it can't create the type because the type already exists. What is the actual problem?

#12

curtis - October 3, 2008 - 10:44

If this works for 6.x, I'd ask that it be implemented, and that development to fix the issues in 7.x be continued separately.

A lot of people are using 6.x now, and this is bug prohibits some of us from using certain modules (for example, the Amazon module).

#13

Anonymous (not verified) - November 11, 2008 - 06:35
Status:needs review» needs work

The last submitted patch failed testing.

#15

linuxpoet - March 10, 2009 - 20:09
Status:needs work» duplicate

I repeat the assertion tha this is a no-op problem. The errors that people mentioned are because the types already existed and it looks as if it has been solved in other tickets. I am going to close this ticket. Please re-open with an actual problem if there is one :)

#16

linuxpoet - May 12, 2009 - 19:45
Status:duplicate» closed

#17

stormsweeper - July 16, 2009 - 19:12
Version:7.x-dev» 6.x-dev
Status:closed» active

In D7, DBTNG apparently fixes this by adding in CHECK constraints instead of the D5/D6 solution of creating domains.

<?php
 
protected function createFieldSql($name, $spec) {
...
    if (!empty(
$spec['unsigned'])) {
     
$sql .= " CHECK ($name >= 0)";
    }
...

    return
$sql;
  }
?>

D5/D6 uses domains for the int types, but this won't work for numeric, since the precision and scale parameters need to be defined when the domain is created. I'd support back-porting the D7 solution as with schema api there isn't really any worry of breaking extant modules. The serial type is already handled this way.

The patch attached just changes the PGSQL driver function that generates the SQL for field definitions. It could be extended to take out the domain creation in system_install, although you would not want to drop the domains.

AttachmentSizeStatusTest resultOperations
pgsql-unsigned.D6.patch4.07 KBIgnoredNoneNone

#18

stormsweeper - July 16, 2009 - 19:16
Status:active» needs review

The latest offender here is the Twitter module, which just changed it's primary key to numeric from int. On Postgres, this severely borks your table on update.

Also, please use this patch. I accidentally did not revert some other edits I was working on.

AttachmentSizeStatusTest resultOperations
pgsql-unsigned.D6.patch773 bytesIgnoredNoneNone

#19

stormsweeper - July 17, 2009 - 19:18

Better yet, use this one. I had to move the unsigned bit after the length/precision bit. Going to file a new issue for D7, since the same error exists there.

#20

stormsweeper - July 17, 2009 - 19:19

Might help if I included the patch.

AttachmentSizeStatusTest resultOperations
pgsql-unsigned.D6.patch1.02 KBIgnoredNoneNone

#22

Darren Oh - July 29, 2009 - 15:04
Assigned to:bjaspan» Anonymous

Unsigned types are added to PostgreSQL during installation. The problem is that some are missing. Thanks to ccurvey for pointing this out in issue 359764.

AttachmentSizeStatusTest resultOperations
system.install-256001-22_D6.patch2.27 KBIgnoredNoneNone

#23

shunting - July 29, 2009 - 15:35

As a PG lurker, let me thank you all for doing this. It's been a constant source of frustration (and a source of secret shame to me when I hack install files and the odd function or two)...

#24

stormsweeper - July 29, 2009 - 16:21

Darren, you can't specify precision, length, or scale for domains after they are created, ie,

ALTER TABLE foo ADD COLUMN bar numeric_unsigned (20,0);

Will barf.

#25

Darren Oh - July 29, 2009 - 22:07

Thanks.

AttachmentSizeStatusTest resultOperations
system.install-256001-25_D6.patch1.88 KBIgnoredNoneNone

#26

stormsweeper - July 29, 2009 - 23:24

Now we're back to this issue's original post - the unsigned numeric type (specified in schema api) can't be created.

Also, I still think domains are an anti-pattern we don't need to use, and in fact are not used in D7. I presume the argument for them in D4 and D5 was to offer types that made it easier for people to just copy over mySql schema definitions, rather than using the CHECK clause in the column definition directly. I think we should just check for the unsigned flag on the appropriate types and add the CHECK clause rather than adding a bunch of domains and special casing things.

#27

stormsweeper - July 29, 2009 - 23:34

As a side point, the use of a CHECK clause is likely pointless since the unsigned types seem to be used in Drupal for the expanded range of values, which you don't get with Postgres and a CHECK clause. You could bump unsigned int to bigint, but that would leave you without any recourse for bigints except to then switch to a numeric column. Messy.

#28

Darren Oh - July 31, 2009 - 13:31
Status:needs review» reviewed & tested by the community

The patch in #20 works.

#29

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

#522786: Postgres numeric check needs to happen after precision check in schema generation is the very same patch as #20 is here (but with added tests), so marking postponed on that one getting into Drupal 7. I'm not supposed to commit changes before they land in D7, unless they do not apply to D7. As soon as that one is committed, please reopen this.

 
 

Drupal is a registered trademark of Dries Buytaert.