<?php

/**
 * @file
 * Installation and uninstallation functions for the GeSHi node module.
 */

/**
 * Implementation of hook_schema()
 */
function geshinode_schema() {
  $schema['geshinode'] = array(
    'description' => t('The table for geshinodes.'),
    'fields' => array(
      'nid' => array(
        'description' => t('The primary identifier for a node.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'description' => t('The current {node_revisions}.vid version identifier.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'language' => array(
        'description' => t('The source code language of the node.'),
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('vid'),
    'indexes' => array('nid' => array('nid')),
  );
  return $schema;
}

/**
 * Create tables on install
 */
function geshinode_install() {
  // Create geshinode tables.
  drupal_install_schema('geshinode');
}

/**
 * Remove tables on uninstall.
 */
function geshinode_uninstall() {
  // Drop geshinode tables.
  drupal_uninstall_schema('geshinode');
}


/**
 * Implementation of hook_update_N().
 *
 * Fix the primary key and indices on the geshinode table.
 * See http://drupal.org/node/363770 .
 */
function geshinode_update_6001() {
  $ret = array();
  // Drop unique key on 'vid'.
  db_drop_unique_key($ret, 'geshinode', 'vid');
  // Change the 'nid' field to 'int' (from 'serial').
  db_change_field($ret, 'geshinode', 'nid', 'nid',
    array(
      'description' => t('The primary identifier for a node.'),
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
    )
  );
  // Drop primary key ('nid').
  db_drop_primary_key($ret, 'geshinode');
  // Add primary key on 'vid'.
  db_add_primary_key($ret, 'geshinode', array('vid'));
  // Add an index on 'nid'.
  db_add_index($ret, 'geshinode', 'nid', array('nid'));
  return $ret;
}
