<?php
// $Id: uc_attribute.install,v 1.10.2.12 2010/03/19 21:27:17 tr Exp $

/**
 * @file
 * Install hooks for uc_attribute.module.
 */

/**
 * Implemenation of hook_schema()
 */
function uc_attribute_schema() {
  $schema = array();

  $schema['uc_attributes'] = array(
    'description' => 'Attributes: the decisions that need to be made about products.',
    'fields' => array(
      'aid' => array(
        'description' => 'Primary key: attribute ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'Name of the attribute.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => 'Label to use when attribute is displayed.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'ordering' => array(
        'description' => 'Determines the list position of attributes.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'required' => array(
        'description' => 'Flag that, if set, requires a user response for attributes (disables default options).',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'display' => array(
        'description' => 'Display type of the attribute options: 0 => text fields, 1 => select box (default), 2 => radio buttons.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
      'description' => array(
        'description' => 'Description of the attribute.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('aid'),
  );

  $schema['uc_attribute_options'] = array(
    'description' => 'The available choices for each attribute.',
    'fields' => array(
      'aid' => array(
        'description' => 'The {uc_attributes}.aid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'oid' => array(
        'description' => 'Primary key: the option ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'The name of the option.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'cost' => array(
        'description' => "The adjustment to a product's cost with the chosen option.",
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'price' => array(
        'description' => "The adjustment to a product's price with the chosen option.",
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'weight' => array(
        'description' => "The adjustment to a product's physical weight with the chosen option.",
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0,
      ),
      'ordering' => array(
        'description' => 'Affects the list position of the options.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('oid'),
  );

  $schema['uc_class_attributes'] = array(
    'description' => 'Attributes copied to a product of a certain class when it is created.',
    'fields' => array(
      'pcid' => array(
        'description' => 'Primary key: the product {node}.type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'aid' => array(
        'description' => 'The {uc_attributes}.aid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'label' => array(
        'description' => 'Label to use when attribute is displayed.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'ordering' => array(
        'description' => 'Determines the list position of attributes.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'default_option' => array(
        'description' => 'The default value of the attribute field on the add to cart form.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'required' => array(
        'description' => "A flag indicating that, if set, requires a user response for attributes (disables default options).",
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'display' => array(
        'description' => 'Display type of the attribute options: 0 => text fields, 1 => select box (default), 2 => radio buttons',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'primary key' => array('pcid', 'aid'),
  );

  $schema['uc_class_attribute_options'] = array(
    'description' => 'The available choices for each attribute.',
    'fields' => array(
      'pcid' => array(
        'description' => 'Primary key: the product {node}.type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'oid' => array(
        'description' => 'The {uc_attribute_options}.oid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'cost' => array(
        'description' => "The adjustment to a product's cost with the chosen option.",
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'price' => array(
        'description' => "The adjustment to a product's price with the chosen option.",
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'weight' => array(
        'description' => "The adjustment to a product's physical weight with the chosen option.",
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0,
      ),
      'ordering' => array(
        'description' => 'Affects the list position of the options.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('pcid', 'oid'),
  );

  $schema['uc_product_attributes'] = array(
    'description' => 'Attributes copied to a product.',
    'fields' => array(
      'nid' => array(
        'description' => 'The product {node}.nid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'aid' => array(
        'description' => 'The {uc_attributes}.aid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'label' => array(
        'description' => 'Label to use when attribute is displayed',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'ordering' => array(
        'description' => 'Determines the list position of attributes.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'default_option' => array(
        'description' => 'The default value of the attribute field on the add to cart form.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'required' => array(
        'description' => "Flag that, if set, requires a user response for attributes (disables default options).",
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'display' => array(
        'description' => 'Display type of the attribute options: 0 -- text fields, 1 -- select box (default), 2 -- radio buttons',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'primary key' => array('nid', 'aid'),
  );

  $schema['uc_product_options'] = array(
    'description' => 'The available choices for each attribute.',
    'fields' => array(
      'nid' => array(
        'description' => 'The product {node}.nid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'oid' => array(
        'description' => 'The {uc_attribute_options}.oid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'cost' => array(
        'description' => "The adjustment to a product's cost with the chosen option.",
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'price' => array(
        'description' => "The adjustment to a product's price with the chosen option.",
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0,
      ),
      'weight' => array(
        'description' => "The adjustment to a product's physical weight with the chosen option.",
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0,
      ),
      'ordering' => array(
        'description' => 'Affects the list position of the options.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('nid', 'oid'),
  );

  $schema['uc_product_adjustments'] = array(
    'description' => "Changes to a product's SKU based on the possible combination of chosen options.",
    'fields' => array(
      'nid' => array(
        'description' => 'The product {node}.nid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'combination' => array(
        'description' => 'A serialized array whose keys are attribute IDs and values are option IDs.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'model' => array(
        'description' => 'The SKU representing the product with the combination of options.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'indexes' => array(
      'nid' => array('nid'),
    ),
  );

  return $schema;
}

/**
 * Implmentation of hook_install().
 */
function uc_attribute_install() {
  drupal_install_schema('uc_attribute');
}

/**
 * Implementation of hook_uninstall().
 */
function uc_attribute_uninstall() {
  drupal_uninstall_schema('uc_attribute');

  variable_del('uc_attribute_option_price_format');
}

function uc_attribute_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_attributes} CHANGE name name varchar(255) NOT NULL");
      $ret[] = update_sql("ALTER TABLE {uc_attributes_options} CHANGE name name varchar(255) NOT NULL");
      $ret[] = update_sql("RENAME TABLE {uc_attributes_options} TO {uc_attribute_options}");
      $ret[] = update_sql("ALTER TABLE {uc_product_adjustments} CHANGE model model varchar(255) NOT NULL");
      $ret[] = update_sql("CREATE TABLE IF NOT EXISTS {uc_class_attributes} (
        `pcid` mediumint(9) NOT NULL,
        `aid` mediumint(9) NOT NULL,
        `default_option` mediumint(9) NOT NULL default '0',
        PRIMARY KEY (`pcid`, `aid`)
      );");
      $ret[] = update_sql("CREATE TABLE IF NOT EXISTS {uc_class_attribute_options} (
        `pcid` mediumint(9) NOT NULL,
        `oid` mediumint(9) NOT NULL,
        `price` decimal(10,2) NOT NULL,
        `weight` float NOT NULL,
        PRIMARY KEY (`pcid`,`oid`)
      );");
    break;
    case 'pgsql':
      db_change_column($ret, 'uc_attributes', 'name', 'name', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
      db_change_column($ret, 'uc_attributes_options', 'name', 'name', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
      $ret[] = update_sql("ALTER TABLE {uc_attributes_options} RENAME TO {uc_attribute_options}");
      db_change_column($ret, 'uc_product_adjustments', 'model', 'model', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
      $ret[] = update_sql("CREATE TABLE IF NOT EXISTS {uc_class_attributes} (
        pcid integer NOT NULL default '0',
        aid integer NOT NULL '0',
        default_option integer NOT NULL default '0',
        PRIMARY KEY (pcid, aid)
      );");
      $ret[] = update_sql("CREATE TABLE IF NOT EXISTS {uc_class_attribute_options} (
        pcid integer NOT NULL default '0',
        oid integer NOT NULL default '0',
        price decimal(10,2) NOT NULL,
        weight float NOT NULL,
        PRIMARY KEY (pcid,oid)
      );");
    break;
  }

  return $ret;
}

function uc_attribute_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_attributes} ADD COLUMN ordering tinyint(2) NOT NULL default 0 AFTER name");
      $ret[] = update_sql("ALTER TABLE {uc_attribute_options} ADD COLUMN ordering tinyint(2) NOT NULL default 0 AFTER weight");
      $ret[] = update_sql("ALTER TABLE {uc_class_attributes} ADD COLUMN ordering tinyint(2) NOT NULL default 0 AFTER aid");
      $ret[] = update_sql("ALTER TABLE {uc_class_attribute_options} ADD COLUMN ordering tinyint(2) NOT NULL default 0 AFTER weight");
      $ret[] = update_sql("ALTER TABLE {uc_product_attributes} ADD COLUMN ordering tinyint(2) NOT NULL default 0 AFTER aid");
      $ret[] = update_sql("ALTER TABLE {uc_product_options} ADD COLUMN ordering tinyint(2) NOT NULL default 0 AFTER weight");
    break;
    case 'pgsql':
      db_add_column($ret, 'uc_attributes', 'ordering', 'smallint', array('not null' => TRUE, 'default' => 0));
      db_add_column($ret, 'uc_attribute_options', 'ordering', 'smallint', array('not null' => TRUE, 'default' => 0));
      db_add_column($ret, 'uc_class_attributes', 'ordering', 'smallint', array('not null' => TRUE, 'default' => 0));
      db_add_column($ret, 'uc_class_attribute_options', 'ordering', 'smallint', array('not null' => TRUE, 'default' => 0));
      db_add_column($ret, 'uc_product_attributes', 'ordering', 'smallint', array('not null' => TRUE, 'default' => 0));
      db_add_column($ret, 'uc_product_options', 'ordering', 'smallint', array('not null' => TRUE, 'default' => 0));
  }
  return $ret;
}

function uc_attribute_update_3() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_class_attributes} CHANGE pcid pcid varchar(32) NOT NULL");
      $ret[] = update_sql("ALTER TABLE {uc_class_attribute_options} CHANGE pcid pcid varchar(32) NOT NULL");
    break;
    case 'pgsql':
      db_change_column($ret, 'uc_class_attributes', 'pcid', 'pcid', 'varchar(32)', array('not null' => TRUE, 'default' => "''"));
      db_change_column($ret, 'uc_class_attribute_options', 'pcid', 'pcid', 'varchar(32)', array('not null' => TRUE, 'default' => "''"));
    break;
  }
  return $ret;
}

function uc_attribute_update_4() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_product_adjustments} CHANGE combination combination varchar(255) NOT NULL");
      $ret[] = update_sql("ALTER TABLE {uc_product_adjustments} ADD PRIMARY KEY  (`nid`, `combination`)");
    break;
    case 'pgsql':
      db_change_column($ret, 'uc_product_adjustments', 'combination', 'combination', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
      $ret[] = update_sql("ALTER TABLE {uc_product_adjustments} ADD PRIMARY KEY  (nid, combiination)");
  }
  return $ret;
}

function uc_attribute_update_5() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_attribute_options} ADD COLUMN cost decimal(10,2) NOT NULL AFTER name");
      $ret[] = update_sql("ALTER TABLE {uc_class_attribute_options} ADD COLUMN cost decimal(10,2) NOT NULL AFTER oid");
      $ret[] = update_sql("ALTER TABLE {uc_product_options} ADD COLUMN cost decimal(10,2) NOT NULL AFTER oid");
    break;
    case 'pgsql':
      db_add_column($ret, 'uc_attribute_options', 'cost', 'decimal(10,2)', array('not null' => TRUE, 'default' => 0.0));
      db_add_column($ret, 'uc_class_attribute_options', 'cost', 'decimal(10,2)', array('not null' => TRUE, 'default' => 0.0));
      db_add_column($ret, 'uc_product_options', 'cost', 'decimal(10,2)', array('not null' => TRUE, 'default' => 0.0));
    break;
  }
  return $ret;
}

function uc_attribute_update_6() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_product_attributes} ADD COLUMN required tinyint(2) NOT NULL default 0 AFTER default_option");
    break;
    case 'pgsql':
      db_add_column($ret, 'uc_product_attributes', 'required', 'smallint', array('not null' => TRUE, 'default' => 0));
    break;
  }
  return $ret;
}

function uc_attribute_update_7() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_class_attributes} ADD COLUMN required tinyint(2) NOT NULL default 0 AFTER default_option");
    break;
    case 'pgsql':
      db_add_column($ret, 'uc_class_attributes', 'required', 'smallint', array('not null' => TRUE, 'default' => 0));
    break;
  }
  return $ret;
}

function uc_attribute_update_8() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {uc_attribute_options} SET WITHOUT OIDS");
      $ret[] = update_sql("ALTER TABLE {uc_class_attribute_options} SET WITHOUT OIDS");
      $ret[] = update_sql("ALTER TABLE {uc_product_options} SET WITHOUT OIDS");
    break;
  }
  return $ret;
}

function uc_attribute_update_9() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_attributes} ADD COLUMN required tinyint(2) NOT NULL default 1");
      $ret[] = update_sql("ALTER TABLE {uc_attributes} ADD COLUMN display tinyint(2) NOT NULL default 1");
      $ret[] = update_sql("ALTER TABLE {uc_class_attributes} ADD COLUMN display tinyint(2) NOT NULL default 1");
      $ret[] = update_sql("ALTER TABLE {uc_product_attributes} ADD COLUMN display tinyint(2) NOT NULL default 1");
    break;
    case 'pgsql':
      db_add_column($ret, 'uc_attributes', 'required', 'smallint', array('not null' => TRUE, 'default' => 1));
      db_add_column($ret, 'uc_attributes', 'display', 'smallint', array('not null' => TRUE, 'default' => 1));
      db_add_column($ret, 'uc_class_attributes', 'display', 'smallint', array('not null' => TRUE, 'default' => 1));
      db_add_column($ret, 'uc_product_attributes', 'display', 'smallint', array('not null' => TRUE, 'default' => 1));
    break;
  }
  return $ret;
}

function uc_attribute_update_10() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {uc_attributes} ADD COLUMN description varchar(255) NOT NULL");
    break;
    case 'pgsql':
      db_add_column($ret, 'uc_attributes', 'description', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
    break;
  }
  return $ret;
}

function uc_attribute_update_6000() {
  $ret = array();

  // Standardize database definitions during upgrade to Drupal 6.
  // ID fields are unsigned, regular-sized ints.
  // "Boolean" flags are unsigned tiny ints.
  // Postgres tables will have the necessary default values, and MySQL
  // doesn't need them, so the schema can just be mismatched for that.

  // Some id columns are already auto-incremented, so we can't remove the
  // primary key beforehand in MySQL. So we add it later for Postgres.

  db_change_field($ret, 'uc_attributes', 'aid', 'aid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE));
  db_change_field($ret, 'uc_attributes', 'required', 'required', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_attributes', 'display', 'display', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));
  if ($GLOBALS['db_type'] == 'pgsql') {
    db_add_primary_key($ret, 'uc_attributes', array('aid'));
  }

  db_change_field($ret, 'uc_attribute_options', 'aid', 'aid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_attribute_options', 'oid', 'oid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE));
  if ($GLOBALS['db_type'] == 'pgsql') {
    db_add_primary_key($ret, 'uc_attribute_options', array('oid'));
  }

  db_drop_primary_key($ret, 'uc_class_attributes');
  db_change_field($ret, 'uc_class_attributes', 'aid', 'aid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('primary key' => array('pcid', 'aid')));
  db_change_field($ret, 'uc_class_attributes', 'default_option', 'default_option', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_class_attributes', 'required', 'required', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_class_attributes', 'display', 'display', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));

  db_drop_primary_key($ret, 'uc_class_attribute_options');
  db_change_field($ret, 'uc_class_attribute_options', 'oid', 'oid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('primary key' => array('pcid', 'oid')));

  db_drop_primary_key($ret, 'uc_product_attributes');
  db_change_field($ret, 'uc_product_attributes', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_attributes', 'aid', 'aid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_attributes', 'default_option', 'default_option', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_attributes', 'required', 'required', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_attributes', 'display', 'display', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));
  db_add_primary_key($ret, 'uc_product_attributes', array('nid', 'aid'));

  db_drop_primary_key($ret, 'uc_product_options');
  db_change_field($ret, 'uc_product_options', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_options', 'oid', 'oid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_add_primary_key($ret, 'uc_product_options', array('nid', 'oid'));

  db_drop_primary_key($ret, 'uc_product_adjustments');
  db_change_field($ret, 'uc_product_adjustments', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('indexes' => array('nid' => array('nid'))));

  return $ret;
}

function uc_attribute_update_6001() {
  $ret = array();

  db_change_field($ret, 'uc_attribute_options', 'cost', 'cost', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_attribute_options', 'price', 'price', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_class_attribute_options', 'cost', 'cost', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_class_attribute_options', 'price', 'price', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_options', 'cost', 'cost', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_options', 'price', 'price', array('type' => 'numeric', 'precision' => 15, 'scale' => 3, 'not null' => TRUE, 'default' => 0));

  return $ret;
}

function uc_attribute_update_6002() {
  $ret = array();

  $result = db_query("SELECT * FROM {permission}");
  while($perm = db_fetch_object($result)) {
    $perm->perm = str_replace("adminster attributes", "administer attributes", $perm->perm);
    drupal_write_record('permission', $perm, 'pid');
  }
  $ret[] = array('success' => TRUE, 'query' => "Replaced all instances of 'adminster attributes' with 'administer attributes' in the permissions table.");

  return $ret;
}

/**
 * Add label to attributes to allow differently named attributes to show with
 * the same label on product add to cart forms.
 */
function uc_attribute_update_6003() {
  $ret = array();

  $spec = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  );
  db_add_field($ret, 'uc_attributes', 'label', $spec);
  db_add_field($ret, 'uc_class_attributes', 'label', $spec);
  db_add_field($ret, 'uc_product_attributes', 'label', $spec);

  $ret[] = array('success' => TRUE, 'query' => 'Added label to product attributes.');

  return $ret;
}

/**
 * Sort adjustment combination arrays by attribute id.
 */
function uc_attribute_update_6004() {
  $ret = array();

  $changes = 0;
  $result = db_query('SELECT * FROM {uc_product_adjustments}');
  while($row = db_fetch_object($result)) {
    $combination = unserialize($row->combination);
    ksort($combination);
    db_query("UPDATE {uc_product_adjustments} SET combination = '%s' WHERE nid = %d AND model = '%s'", serialize($combination), $row->nid, $row->model);
    $changes++;
  }

  $ret[] = array('success' => TRUE, 'query' => 'Sorted adjustment combination arrays by attribute id.');

  return $ret;
}

/**
 * Change currency fields to numeric(16,5).
 */
function uc_attribute_update_6005() {
  $ret = array();

  db_change_field($ret, 'uc_attribute_options', 'cost', 'cost', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_attribute_options', 'price', 'price', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_class_attribute_options', 'cost', 'cost', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_class_attribute_options', 'price', 'price', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_options', 'cost', 'cost', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));
  db_change_field($ret, 'uc_product_options', 'price', 'price', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0));

  return $ret;
}
