Commit 2ec31ba3 authored by Yaacov Akiba Slama's avatar Yaacov Akiba Slama Committed by Merlin Axel Rutz
Browse files

Issue #3283573 by yaslama: Add a drush sqlite:vacuum command

parent 1d053166
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -17,5 +17,12 @@
    "require-dev": {
        "drupal/core": "^9.3"
    },
    "require": {}
    "require": {},
    "extra": {
        "drush": {
            "services": {
                "drush.services.yml": "^9"
            }
        }
    }
}

drush.services.yml

0 → 100644
+5 −0
Original line number Diff line number Diff line
services:
  sqlite_vacuum.commands:
    class: Drupal\sqlite_vacuum\Commands\SqliteVacuumCommands
    tags:
      - { name: drush.command }
+32 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\sqlite_vacuum\Commands;

use Drush\Drush;
use Drush\Commands\DrushCommands;
use Drupal\Core\Database\Database;

class SqliteVacuumCommands extends DrushCommands {
  /**
   * Vacuum a sqlite database.
   *
   * @param string $target
   *   The target database.
   * @param string $key
   *   The key of the database connection.
   *
   * @command sqlite:vacuum
   * @aliases sqlite-vacuum
   * @usage sqlite:vacuum dbname
   *   vacuum the database named dbname.
   */
  public function vacuum($target = 'default', $key = NULL) {
    $connectipn = Database::getConnection($target, $key);
    if ($connectipn->databaseType() === 'sqlite') {
      $connectipn->query('vacuum;');
    } else {
      Drush::logger()->error(dt('sql:vacuum works only on sqlite databases.'));
    }
  }
}