You are here

function apdqc_admin_semaphore_table_update_schema in Asynchronous Prefetch Database Query Cache 7

Update the semaphore table schema.

Parameters

bool $show_msg: Set to FALSE to not run drupal_set_message().

bool $reverse: Undo all the changes made to the semaphore table.

Return value

bool Returns TRUE if changes where made.

2 calls to apdqc_admin_semaphore_table_update_schema()
apdqc_disable in ./apdqc.install
Implements hook_disable().
drush_apdqc in ./apdqc.drush.inc
Drush command to all all the apdqc functions.
1 string reference to 'apdqc_admin_semaphore_table_update_schema'
apdqc_admin_operations_form in ./apdqc.admin.inc
Form builder; perform apdqc operations.

File

./apdqc.admin.inc, line 1000
Admin page callbacks for the apdqc module.

Code

function apdqc_admin_semaphore_table_update_schema($show_msg = TRUE, $reverse = FALSE) {
  $changed = FALSE;
  $table = 'semaphore';
  $table_name = Database::getConnection()
    ->prefixTables('{' . db_escape_table($table) . '}');
  $field = 'value';
  if ($reverse === TRUE) {
    variable_del('apdqc_semaphore_schema');

    // Unmodify semaphore table.
    $schema = drupal_get_schema_unprocessed('system', $table);
    db_change_field($table, $field, $field, $schema['fields'][$field]);
    $results = db_query("SHOW KEYS FROM {$table_name} WHERE Key_name = 'PRIMARY'")
      ->fetchAllAssoc('Column_name');
    if (count($results) == 3) {

      // Lock table and alter primary key.
      db_query("LOCK TABLES {$table_name} WRITE");
      db_query("ALTER TABLE {$table_name} DROP PRIMARY KEY, ADD PRIMARY KEY (name)");

      // Verify.
      $results = db_query("SHOW KEYS FROM {$table_name} WHERE Key_name = 'PRIMARY'")
        ->fetchAllAssoc('Column_name');
      if (empty($results) || empty($results['name'])) {
        db_drop_primary_key($table);
        db_add_primary_key($table, array(
          'name',
        ));
      }

      // Unlock tables.
      db_query('UNLOCK TABLES');
    }
    apdqc_admin_change_table_collation_queries($table, 'utf8_general_ci', TRUE, array(
      'name',
      'value',
    ));
    variable_del('apdqc_semaphore_schema');
    $changed = TRUE;
  }
  else {

    // Remove old junk.
    $results = db_delete($table)
      ->condition('expire', REQUEST_TIME - 600, '<')
      ->execute();

    // Modify semaphore table.
    $results = db_query("SELECT LENGTH(value) AS max_length FROM {$table_name} ORDER BY LENGTH(value) DESC LIMIT 1")
      ->fetchAssoc();
    $schema = apdqc_get_full_schema();
    if ($results['max_length'] <= 20) {
      db_change_field($table, $field, $field, $schema[$table]['fields'][$field]);
      $changed = TRUE;
    }
    elseif ($results['max_length'] <= 33) {
      $schema[$table]['fields'][$field]['length'] = 33;
      db_change_field($table, $field, $field, $schema[$table]['fields'][$field]);
      $changed = TRUE;
    }
    $results = db_query("SHOW KEYS FROM {$table_name} WHERE Key_name = 'PRIMARY'")
      ->fetchAllAssoc('Column_name');
    if (count($results) != 3) {

      // Lock table and alter primary key.
      db_query("LOCK TABLES {$table_name} WRITE");
      db_query("ALTER TABLE {$table_name} DROP PRIMARY KEY, ADD PRIMARY KEY (name, value, expire)");

      // Verify.
      $results = db_query("SHOW KEYS FROM {$table_name} WHERE Key_name = 'PRIMARY'")
        ->fetchAllAssoc('Column_name');
      if (empty($results) || empty($results['name']) || empty($results['value']) || empty($results['expire'])) {
        db_drop_primary_key($table);
        db_add_primary_key($table, array(
          'name',
          'value',
          'expire',
        ));
      }

      // Unlock tables.
      db_query('UNLOCK TABLES');
      $changed = TRUE;
    }
    $collation_changed = apdqc_admin_change_table_collation_queries($table, 'ascii_bin', TRUE, array(
      'name',
      'value',
    ));
    if ($collation_changed) {
      $changed = TRUE;
    }
    if ($show_msg !== FALSE) {
      drupal_set_message(t('APDQC: semaphore table schema has been updated.'));
    }
    variable_set('apdqc_semaphore_schema', TRUE);
  }
  return $changed;
}