? sites/default/files ? sites/default/settings.php Index: includes/database/pgsql/schema.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/database/pgsql/schema.inc,v retrieving revision 1.16 diff -u -p -r1.16 schema.inc --- includes/database/pgsql/schema.inc 9 Jul 2009 10:12:14 -0000 1.16 +++ includes/database/pgsql/schema.inc 31 Jul 2009 02:22:50 -0000 @@ -143,9 +143,6 @@ class DatabaseSchema_pgsql extends Datab if ($spec['type'] == 'serial') { unset($spec['not null']); } - if (!empty($spec['unsigned'])) { - $sql .= " CHECK ($name >= 0)"; - } if (in_array($spec['type'], array('varchar', 'char', 'text')) && isset($spec['length'])) { $sql .= '(' . $spec['length'] . ')'; @@ -154,6 +151,10 @@ class DatabaseSchema_pgsql extends Datab $sql .= '(' . $spec['precision'] . ', ' . $spec['scale'] . ')'; } + if (!empty($spec['unsigned'])) { + $sql .= " CHECK ($name >= 0)"; + } + if (isset($spec['not null']) && $spec['not null']) { $sql .= ' NOT NULL'; } Index: modules/simpletest/tests/schema.test =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/schema.test,v retrieving revision 1.8 diff -u -p -r1.8 schema.test --- modules/simpletest/tests/schema.test 13 Jul 2009 21:51:41 -0000 1.8 +++ modules/simpletest/tests/schema.test 31 Jul 2009 02:22:50 -0000 @@ -129,4 +129,55 @@ class SchemaTestCase extends DrupalWebTe $this->assertEqual($comment, $description, t('The comment matches the schema description.')); } } + + /** + * Tests creating unsigned columns and that those columns reject + * negative values on insert. + */ + function testUnsignedColumns() { + // set up unsigned schema with just serial column + $table_spec = array( + 'fields' => array('serial_col' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE)), + 'primary key' => array('serial_col'), + ); + $ret = array(); + db_create_table($ret, 'unsigned_table', $table_spec); + + // now set up cols for the other types + $types = array('int', 'float', 'numeric'); + foreach ($types as $type) { + $col_spec = array('type' => $type, 'unsigned'=> TRUE); + if ($type == 'numeric') { + $col_spec += array('precision' => 10, 'scale' => 0); + } + $table_spec['fields'][$type . '_col'] = $col_spec; + db_add_field($ret, 'unsigned_table', $type . '_col', $col_spec); + } + + // check each col and try to insert invalid values + foreach($table_spec['fields'] as $col => $def) { + $this->assertTrue(db_column_exists('unsigned_table', $col), t('Unsigned @type column was created.', array('@type' => $def['type']))); + $this->assertFalse($this->tryUnsignedInsert($col), t('Unsigned @type column rejected a negative value.', array('@type' => $def['type']))); + } + } + + /** + * Tries to insert a negative value into columns defined as unsigned. + * + * @param $col + * The column to insert + * @return + * TRUE if the insert succeeded, FALSE otherwise + */ + function tryUnsignedInsert($col) { + try { + db_insert('unsigned_test') + ->fields(array($col => -1)) + ->execute(); + return TRUE; + } + catch (Exception $e) { + return FALSE; + } + } }