Skip to content

Database

DatabaseSize

Large databases can negatively impact your production site, and slow down things like database dumps.

Class: Drutiny\Audit\Database\DatabaseSize
Extends: Drutiny\Audit
Package: drutiny/drutiny

Policies

These are the policies that use this class:

Name Title
Database:Size Database size

Parameters

Name Type Description Default
max_size integer Fail the audit if the database size is greater than this value null
warning_size integer Issue a warning if the database size is greater than this value null

Tokens

Name Type Description Default
max_size integer Fail the audit if the database size is greater than this value null
warning_size integer Issue a warning if the database size is greater than this value null
db string The name of the database null
size integer The size of the database null
Source
  public function audit(Sandbox $sandbox) {
    $stat = $sandbox->drush(['format' => 'json'])
      ->status();

    $name = $stat['db-name'];
    $sql = "SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) 'DB Size in MB'
            FROM information_schema.tables
            WHERE table_schema='{$name}'
            GROUP BY table_schema;";

    $resultLines = $sandbox->drush()->sqlq($sql);
    $resultLines = array_filter($resultLines, function($line) {
      return $line !== 'DB Size in MB';
    });
    $size = (float) reset($resultLines);

    $sandbox->setParameter('db', $name)
            ->setParameter('size', $size);

    if ($sandbox->getParameter('max_size') < $size) {
      return FALSE;
    }

    if ($sandbox->getParameter('warning_size') < $size) {
      return Audit::WARNING;
    }

    return TRUE;
  }