<?php
// $Id: messaging_simple.install,v 1.1.2.1 2010/03/09 15:52:45 jareyero Exp $

/**
* Implementation of hook_schema().
*/
function messaging_simple_schema() {
  $schema['messaging_simple'] = array(
    'description' => 'Stores messages sent among site users.',
    'fields' => array(
      'msid'    => array(      
        'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE,
        'description' => 'Unique message id.',
      ),
      'mqid'    => array(      
        'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0,
        'description' => 'Unique message id.',
      ),
      'language' => array(
        'type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => '',
        'description' => 'Language code.',
      ),
      'uid'    => array(
        'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0,
        'description' => 'The {user}.uid for destination if it is a unique destination.',        
      ),
      'sender' => array(
        'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0,
        'description' => 'The {user}.uid who sent the message if any.',     
      ),     
      'subject' => array(
        'type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '',
        'description' => 'Message subject, single text line.',
      ),
      'body' => array(
        'type' => 'text', 'not null' => TRUE, 'size' => 'big',
        'description' => 'Message body, multiple text line.',
      ),
      'sent' => array(
        'type' => 'int', 'not null' => TRUE, 'default' => 0,
        'description' => 'Unix timestamp, when the message was sent.',
      ),
    ),    
    'primary key' => array('msid'),
  );
  return $schema;
} 
  
/**
 * Implementation of hook_install()
*/
function messaging_simple_install() {
  drupal_install_schema('messaging_simple');  
}

/**
 * Implementation of hook_uninstall()
*/
function messaging_simple_uninstall() {
  drupal_uninstall_schema('messaging_simple');  
}

/**
 * Update: Create table and move messages from messaging_store
 */
function messaging_simple_update_6001() {
  $ret = array();
  drupal_install_schema('messaging_simple');
  $ret[] = update_sql("INSERT INTO {messaging_simple}(mqid, language, uid, sender, subject, body, sent) SELECT mqid, language, uid, sender, subject, body, created FROM {messaging_store} WHERE method = 'simple'");
  $ret[] = update_sql("DELETE FROM {messaging_store} WHERE method = 'simple'");
  return $ret;
}