You are here

public function ModuleCommands::setSchemaVersion in Helper 8

Sets the schema version for a module.

@command module:schema-version:set @aliases msvs @validate-module @usage drush module:schema-version:set system 8701 Sets the current schema version for the system module to 8701. @usage drush module:schema-version:set system current-3 Sets the current schema version for the system module to three minus the current schema. @usage drush module:schema-version:set system current+1 Sets the current schema version for the system module to one plus the current schema.

Parameters

string $module: The module name, for example "system".

int|string $version: The version to set or the string "current[+-]number" to set a relative value to the current version.

Throws

\InvalidArgumentException

\Drush\Exceptions\UserAbortException

\RuntimeException

File

src/Commands/ModuleCommands.php, line 74

Class

ModuleCommands
Drush commands for working with module schemas.

Namespace

Drupal\helper\Commands

Code

public function setSchemaVersion($module, $version) {
  module_load_install($module);
  $current_version = (int) drupal_get_installed_schema_version($module);
  $last_removed = $this->moduleHandler
    ->invoke($module, 'update_last_removed');
  $lowest = \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
  $args = [
    '!module' => $module,
    '!version' => &$version,
    '!current' => $current_version,
    '!removed' => $last_removed,
    '!lowest' => $lowest,
  ];
  if (preg_match('/(current([\\+\\-]))?(\\d+)/', $version, $matches)) {
    switch ($matches[2]) {
      case '+':
        $version = $current_version + (int) $matches[3];
        break;
      case '-':
        $version = $current_version - (int) $matches[3];
        break;
      case '':
        $version = (int) $matches[3];
        break;
    }
  }
  else {
    throw new \InvalidArgumentException(dt('The schema version !version is not valid.', $args));
  }
  if ($version < $lowest) {
    throw new \InvalidArgumentException(dt('The schema version !version cannot be lower than !lowest.', $args));
  }
  elseif ($version === $current_version) {
    return dt('The !module module schema is already at !current.', $args);
  }
  elseif (!empty($last_removed) && $version < $last_removed) {
    $this
      ->io()
      ->caution(dt('The schema version !version is lower than the !module_update_last_removed() value of !removed. This will prevent module updates from running.', $args));
  }
  if (!$this
    ->io()
    ->confirm(dt('Do you want to set the schema for !module module from !current to !version?', $args))) {
    throw new UserAbortException();
  }
  if (!$this
    ->getConfig()
    ->simulate()) {
    drupal_set_installed_schema_version($module, $version);
    if (drupal_get_installed_schema_version($module, TRUE) !== $version) {
      throw new \RuntimeException(dt('Unable to update schema for !module module from !current to !version.', $args));
    }
  }
  return dt('Updated schema for !module module from !current to !version.', $args);
}