<?php
// $Id: audio_images.install,v 1.7 2008/05/25 17:21:26 drewish Exp $

function audio_images_install() {
  drupal_install_schema('audio_images');
}

function audio_images_uninstall() {
  drupal_uninstall_schema('audio_images');
  variable_del('audio_default_image_type');
  variable_del('audio_image_size');
  // TODO should we delete the image files too?
}

/**
 * Install the initial schema.
 */
function audio_images_schema() {
  $schema['audio_image'] = array(
    'description' => t('Associates an image (such as album artwork) with an audio file.'),
    'fields' => array(
      'pid' => array(
        'type' => 'serial',
        'size' => 'medium',
        'not null' => TRUE,
      ),
      'nid' => array(
        'type' => 'int',
        'size' => 'medium',
        'not null' => TRUE,
      ),
      'vid' => array(
        'type' => 'int',
        'size' => 'medium',
        'not null' => TRUE,
      ),
      'pictype' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'width' => array(
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0,
      ),
      'height' => array(
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0,
      ),
      'filemime' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => TRUE,
        'default' => '',
      ),
      'filepath' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'filesize' => array(
        'type' => 'int',
        'size' => 'medium',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('pid'),
    'indexes' => array(
      'audio_image_vid_pictype' => array('vid', 'pictype'),
    ),
  );
  return $schema;
}

/**
 * Remove the auto inc id field (we'll use drupal's sequences) and add an index.
 */
function audio_images_update_1() {
  $ret = array();

  db_add_field($ret, 'audio_image', 'nid',
    array(
      'type' => 'int',
      'size' => 'medium',
      'not null' => TRUE,
    )
  );
  db_change_field($ret, 'audio_image', 'pid', 'pid',
    array(
      'type' => 'serial',
      'size' => 'medium',
      'not null' => TRUE,
    )
  );
  db_add_index($ret, 'audio_image', 'audio_image_vid_pictype', array('vid', 'pictype'));

  return $ret;
}

/**
 * Add a PostgreSQL sequence.
 */
function audio_images_update_2() {
  $ret = array();
  db_change_field($ret, 'audio_image', 'pid', 'pid',
    array(
      'type' => 'serial',
      'size' => 'medium',
      'not null' => TRUE,
    )
  );
  return $ret;
}