<?php
/**
 * @file
 * Variable API module install file
 */

/**
 * Implements hook_install()
 */
function variable_store_install() {
  // Set module weight for it to run before most modules and initialize variable realms
  db_query("UPDATE {system} SET weight = -1000 WHERE name = 'variable_store' AND type = 'module'");
}

/**
 * Implementation of hook_schema().
 */
function variable_store_schema() {
  $schema['variable_store'] = array(
    'description' => 'Named variable/value pairs created by modules using Variable API database storage. All variables are cached in memory at the start of every Drupal request so developers should not be careless about what is stored here.',
    'fields' => array(
      'realm' => array(
        'description' => 'The realm domain of this variable.',
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
        'default' => ''),
      'realm_key' => array(
        'description' => 'The realm key of this variable.',
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
        'default' => ''),
      'name' => array(
        'description' => 'The name of the variable.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => ''),
      'value' => array(
        'description' => 'The value of the variable.',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big'),
      'serialized' => array(
        'description' => 'A flag to indicate whether content is serialized (1) or not (0).',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 1),
    ),
    'primary key' => array('realm', 'realm_key', 'name'),
    'indexes' => array('realm_value' => array('realm', 'realm_key')),
  );
  return $schema;
}

/**
 * Reduce realm key field length so it can be used on the primary key
 */
function variable_store_update_7000() {
  $schema = variable_store_schema();
  db_change_field('variable_store', 'realm_key', 'realm_key', $schema['variable_store']['fields']['realm_key']);
}