function system_update_7016

Remove custom datatype *_unsigned in PostgreSQL.

Related topics

File

drupal/modules/system/system.install, line 2129
Install, update and uninstall functions for the system module.

Code

function system_update_7016() {

  // Only run these queries if the driver used is pgsql.
  if (db_driver() == 'pgsql') {
    $result = db_query("SELECT c.relname AS table, a.attname AS field,\n                        pg_catalog.format_type(a.atttypid, a.atttypmod) AS type\n                        FROM pg_catalog.pg_attribute a\n                        LEFT JOIN pg_class c ON (c.oid =  a.attrelid)\n                        WHERE pg_catalog.pg_table_is_visible(c.oid) AND c.relkind = 'r'\n                        AND pg_catalog.format_type(a.atttypid, a.atttypmod) LIKE '%unsigned%'");
    foreach ($result as $row) {
      switch ($row->type) {
        case 'smallint_unsigned':
          $datatype = 'int';
          break;
        case 'int_unsigned':
        case 'bigint_unsigned':
        default:
          $datatype = 'bigint';
          break;
      }
      db_query('ALTER TABLE ' . $row->table . ' ALTER COLUMN "' . $row->field . '" TYPE ' . $datatype);
      db_query('ALTER TABLE ' . $row->table . ' ADD CHECK ("' . $row->field . '" >= 0)');
    }
    db_query('DROP DOMAIN IF EXISTS smallint_unsigned');
    db_query('DROP DOMAIN IF EXISTS int_unsigned');
    db_query('DROP DOMAIN IF EXISTS bigint_unsigned');
  }
}