summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/mysql/mysql.install
blob: e4ddf8d10b6891708901f5d16533dd3bddec0e0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php

/**
 * @file
 * Install, update and uninstall functions for the mysql module.
 */

use Drupal\Core\Database\Database;
use Drupal\Core\Render\Markup;

/**
 * Implements hook_requirements().
 */
function mysql_requirements($phase) {
  $requirements = [];

  if ($phase === 'runtime') {
    // Test with MySql databases.
    if (Database::isActiveConnection()) {
      $connection = Database::getConnection();
      // Only show requirements when MySQL is the default database connection.
      if (!($connection->driver() === 'mysql' && $connection->getProvider() === 'mysql')) {
        return [];
      }

      $query = 'SELECT @@SESSION.tx_isolation';
      // The database variable "tx_isolation" has been removed in MySQL v8.0.3 and
      // has been replaced by "transaction_isolation".
      // @see https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_tx_isolation
      // @see https://dev.mysql.com/doc/refman/8.0/en/added-deprecated-removed.html
      if (!$connection->isMariaDb() && version_compare($connection->version(), '8.0.2-AnyName', '>')) {
        $query = 'SELECT @@SESSION.transaction_isolation';
      }

      $isolation_level = $connection->query($query)->fetchField();

      $tables_missing_primary_key = [];
      $tables = $connection->schema()->findTables('%');
      foreach ($tables as $table) {
        $primary_key_column = Database::getConnection()->query("SHOW KEYS FROM {" . $table . "} WHERE Key_name = 'PRIMARY'")->fetchAllAssoc('Column_name');
        if (empty($primary_key_column)) {
          $tables_missing_primary_key[] = $table;
        }
      }

      $description = [];
      if ($isolation_level == 'READ-COMMITTED') {
        if (empty($tables_missing_primary_key)) {
          $severity_level = REQUIREMENT_OK;
        }
        else {
          $severity_level = REQUIREMENT_ERROR;
        }
      }
      else {
        if ($isolation_level == 'REPEATABLE-READ') {
          $severity_level = REQUIREMENT_WARNING;
        }
        else {
          $severity_level = REQUIREMENT_ERROR;
          $description[] = t('This is not supported by Drupal.');
        }
        $description[] = t('The recommended level for Drupal is "READ COMMITTED".');
      }

      if (!empty($tables_missing_primary_key)) {
        $description[] = t('For this to work correctly, all tables must have a primary key. The following table(s) do not have a primary key: @tables.', ['@tables' => implode(', ', $tables_missing_primary_key)]);
      }

      $description[] = t('See the <a href=":performance_doc">setting MySQL transaction isolation level</a> page for more information.', [
        ':performance_doc' => 'https://www.drupal.org/docs/system-requirements/setting-the-mysql-transaction-isolation-level',
      ]);

      $requirements['mysql_transaction_level'] = [
        'title' => t('Transaction isolation level'),
        'severity' => $severity_level,
        'value' => $isolation_level,
        'description' => Markup::create(implode(' ', $description)),
      ];
    }
  }

  return $requirements;
}