You are here

hosting_task.install in Hosting 7.4

Define the database schema and update functions for the hosting_task module.

File

task/hosting_task.install
View source
<?php

/**
 * @file
 * Define the database schema and update functions for the hosting_task module.
 */

/**
 * Implements hook_schema().
 */
function hosting_task_schema() {
  $schema['hosting_task'] = array(
    'fields' => array(
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'task_type' => array(
        'type' => 'text',
        'size' => 'big',
        'not null' => FALSE,
      ),
      'rid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'task_status' => array(
        'type' => 'int',
        'not null' => FALSE,
      ),
      'executed' => array(
        'type' => 'int',
        'not null' => FALSE,
      ),
      'delta' => array(
        'type' => 'int',
        'not null' => FALSE,
      ),
    ),
    'indexes' => array(
      'nid' => array(
        'nid',
      ),
    ),
    'primary key' => array(
      'vid',
    ),
  );
  $schema['hosting_task_arguments'] = array(
    'fields' => array(
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'name' => array(
        'type' => 'text',
        'size' => 'big',
        'not null' => FALSE,
      ),
      'value' => array(
        'type' => 'text',
        'size' => 'big',
        'not null' => FALSE,
      ),
    ),
    'indexes' => array(
      'vid' => array(
        'vid',
      ),
      'nid' => array(
        'nid',
      ),
    ),
  );
  $schema['hosting_task_log'] = array(
    'fields' => array(
      'lid' => array(
        'type' => 'serial',
        'not null' => TRUE,
      ),
      'vid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 16,
        'not null' => TRUE,
        'default' => '',
      ),
      'message' => array(
        'type' => 'text',
        'size' => 'big',
        'not null' => TRUE,
      ),
      'error' => array(
        'type' => 'text',
        'size' => 'big',
        'not null' => TRUE,
      ),
      'timestamp' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'type' => array(
        'type',
      ),
      'vid_lid' => array(
        'vid',
        'lid',
      ),
      'nid' => array(
        'nid',
      ),
    ),
    'primary key' => array(
      'lid',
    ),
  );
  return $schema;
}

/**
 * Implements hook_update_N().
 *
 * Add the error column to hosting_task_log, so we can extract the
 * specific error codes, not just the messages.
 */
function hosting_task_update_1() {
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {hosting_task_log} \n    ADD COLUMN error longtext NOT NULL default ''");
  $ret[] = update_sql("ALTER TABLE {hosting_task_log} \n    DROP COLUMN severity");
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Reset the task_status of all currently (according to us) in queue tasks
 *
 * This will force them to be re-evaluated if the queue status doesn't match the task status
 */
function hosting_task_update_2() {
  $ret = array();
  variable_set('hosting_dispatch_enabled', FALSE);

  // Put the queued items separately for a bit.
  db_query("UPDATE {hosting_task_queue} SET status = 100 WHERE status = 0");

  // Remove all the other items from the queue.
  db_query("UPDATE {hosting_task_queue} SET status = 0 WHERE status <> 100");

  // Add all the queued items back into the queue.
  db_query("UPDATE {hosting_task_queue} SET status = 1 WHERE status = 100");
  variable_set('hosting_dispatch_enabled', TRUE);
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Add a delta column to tasks so that we have a measure of duration of tasks
 */
function hosting_task_update_6000() {
  $ret = hosting_package_update_6002();

  // This update needs to be run before tasks can be added, in other update functions.
  if (!variable_get('hosting_task_update_6000_run', FALSE)) {
    db_add_field($ret, 'hosting_task', 'delta', array(
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ));
    variable_set('hosting_task_update_6000_run', TRUE);
  }
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * remove the redundant hosting_queue table
 */
function hosting_task_update_6001() {
  $ret = array();
  db_drop_table($ret, 'hosting_task_queue');
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Remove all the duplicate task nodes for previous task type / rid combinations
 *
 * This makes it more efficient to query and generate the data, and makes sure we don't
 * lose any history.
 */
function hosting_task_update_6002() {
  $ret = array();
  $result = db_query("select t.task_type, t.rid, max(t.nid) as max_nid from {hosting_task} t group by t.task_type, t.rid");
  while ($obj = db_fetch_object($result)) {
    db_query("UPDATE {hosting_task} SET nid = %d WHERE task_type='%s' AND rid=%d", $obj->max_nid, $obj->task_type, $obj->rid);
  }
  db_query("update {hosting_task_arguments} a, {hosting_task} t set a.nid=t.nid where a.vid=t.vid");
  db_query("update {node_revisions} r, {hosting_task} t set r.nid=t.nid where r.vid=t.vid");
  $result = db_query("select n.nid, count(t.vid) as vid_count from {node} n left join {hosting_task} t on n.nid = t.nid where n.type = 'task' group by n.nid having vid_count = 0");
  while ($obj = db_fetch_object($result)) {
    db_query("DELETE FROM {node} WHERE nid=%d", $obj->nid);
  }
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Processing state constant changed to ensure proper sorting
 */
function hosting_task_update_6003() {
  $ret = array();
  db_query("UPDATE {hosting_task} SET task_status = -1 WHERE task_status = 4");
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Add 'cancel own task' permission to 'aegir client' role
 */
function hosting_task_update_6004() {
  $ret = array();
  install_include(array(
    'user',
  ));
  install_add_permissions(install_get_rid('aegir client'), array(
    'cancel own tasks',
  ));
  node_access_rebuild();
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Remove DEFAULT value on a LONGTEXT fields.
 *
 * This was breaking in MariaDB and preventing installation. On MySQL
 * it was silently ignored, so the installation works, but the
 * defaults are not in the table anyway.
 *
 * See #969326
 */
function hosting_task_update_6005() {
  $ret = array();
  db_change_field($ret, 'hosting_task_log', 'error', 'error', array(
    'type' => 'text',
    'size' => 'big',
    'not null' => TRUE,
  ));
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Add nid field to hosting_task_log and remove orphaned log entries.
 */
function hosting_task_update_6006() {
  $ret = array();

  // Add nid field.
  $field = array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );

  // Add some useful indexes.
  $indexes = array(
    'indexes' => array(
      'vid_lid' => array(
        'vid',
        'lid',
      ),
      'nid' => array(
        'nid',
      ),
    ),
  );
  db_add_field($ret, 'hosting_task_log', 'nid', $field, $indexes);

  // Populate nid field.
  $query = "SELECT nid, vid FROM {hosting_task}";
  $result = db_query($query);
  while ($task = db_fetch_object($result)) {
    $query = "UPDATE {hosting_task_log} SET nid = %d WHERE vid = %d";
    db_query($query, $task->nid, $task->vid);
  }

  // Remove orphaned logs, which will have nid == 0 now.
  $query = "DELETE FROM {hosting_task_log} WHERE nid = %d";
  db_query($query, 0);
  return $ret;
}

/**
 * Add permission.
 */
function hosting_task_update_6200() {
  $ret = array();

  // Temporarily enable Install Profile API module and load includes.
  module_enable(array(
    'install_profile_api',
  ));
  module_load_include('inc', 'install_profile_api', 'core/user');
  install_add_permissions(install_get_rid('aegir administrator'), array(
    'update status of tasks',
  ));
  module_disable(array(
    'install_profile_api',
  ));
  return $ret;
}

/**
 * Clear cache for tasks view update.
 */
function hosting_task_update_7000() {
  drupal_flush_all_caches();
}

/**
 * Grant aegir clients 'create login-reset task' permission.
 */
function hosting_task_update_7001() {
  $role = user_role_load_by_name('aegir client');
  if ($role) {
    user_role_grant_permissions($role->rid, array(
      'create login-reset task',
    ));
  }
}

/**
 * Add indexes to the hosting_task and hosting_task_arguments tables.
 */
function hosting_task_update_7301() {
  if (!db_index_exists('hosting_task', 'hosting_task_nid_idx')) {
    db_add_index('hosting_task', 'hosting_task_nid_idx', array(
      'nid',
    ));
  }
  if (!db_index_exists('hosting_task_arguments', 'hosting_task_arguments_nid_idx')) {
    db_add_index('hosting_task_arguments', 'hosting_task_arguments_nid_idx', array(
      'nid',
    ));
  }
  if (!db_index_exists('hosting_task_arguments', 'hosting_task_arguments_vid_idx')) {
    db_add_index('hosting_task_arguments', 'hosting_task_arguments_vid_idx', array(
      'vid',
    ));
  }
}

Functions

Namesort descending Description
hosting_task_schema Implements hook_schema().
hosting_task_update_1 Implements hook_update_N().
hosting_task_update_2 Implements hook_update_N().
hosting_task_update_6000 Implements hook_update_N().
hosting_task_update_6001 Implements hook_update_N().
hosting_task_update_6002 Implements hook_update_N().
hosting_task_update_6003 Implements hook_update_N().
hosting_task_update_6004 Implements hook_update_N().
hosting_task_update_6005 Implements hook_update_N().
hosting_task_update_6006 Implements hook_update_N().
hosting_task_update_6200 Add permission.
hosting_task_update_7000 Clear cache for tasks view update.
hosting_task_update_7001 Grant aegir clients 'create login-reset task' permission.
hosting_task_update_7301 Add indexes to the hosting_task and hosting_task_arguments tables.